Upon running this example, any running instances of VR-Vantage that are connected to the appropriate connection (DIS/HLA13/HLA1516) will display the objects rendered by the example. The example will run through a series of 3D objects (sphere/cube/cylinder/line), then move on to 2D objects (text/line/circle/triangle/square). Please note that in order to see the 3D objects, a terrain must be loaded.
This example is an application. You can run it by running ./bin/exampleDrawObjects<protocol>.exe (on Windows) or ./bin/exampleDrawObjects<protocol> (on Linux). For more information about running examples, please see Running Applications and Examples.
#include <vl/exerciseConn.h>
#include <vl/exConnInit.h>
#include <vlutil/vlProcessControl.h>
#include <matrix/utmCoord.h>
#include <matrix/vlTaitBryan.h>
#include <vl/dataInter.h>
#include <matrix/topoCoord.h>
#include <stdio.h>
#include <vlutil/vlKeyboard.h>
using namespace vrvControlToolkit;
DtExerciseConn* exConn;
std::vector<DtDrawControlObject*> myObjects;
int id = 0;
void updateObjects(double time = 0.0)
{
if (time > 0.0)
{
DtTime tickTime = exConn->clock()->elapsedRealTime();
while (exConn->clock()->simTime() <= tickTime + time)
{
exConn->drainInput();
for (unsigned int i = 0; i < myObjects.size(); ++i)
{
myObjects[i]->update();
}
exConn->clock()->setSimTime(exConn->clock()->elapsedRealTime());
DtSleep(0.1);
}
}
else
{
exConn->drainInput();
for (unsigned int i = 0; i < myObjects.size(); ++i)
{
myObjects[i]->update();
}
exConn->clock()->setSimTime(exConn->clock()->elapsedRealTime());
}
}
void setMsg(char *msg)
{
DtNetU8 ucolor[4] = {255, 255, 255, 255};
double pos1[2];
pos1[0] = (-40 / 200.) + .5;
pos1[1] = (70 / 200.) + .5;
if (!myText)
{
exConn->drainInput();
myText->setFlashing(0);
DtVdcColor colorToSet(ucolor[0], ucolor[1], ucolor[2], ucolor[3]);
myText->setColor(colorToSet);
myObjects.push_back(myText);
}
myText->setText(msg);
myText->update();
exConn->drainInput();
DtSleep(0.1);
}
void showSet(const DtString& setName)
{
sprintf(showSetCommand.
mySetName,
"%s", setName.c_str());
DtDataInteraction dataInter;
myCommandMarshaller.encode(&showSetCommand, &dataInter);
exConn->sendStamped(dataInter);
}
void hideSet(const DtString& setName)
{
sprintf(showSetCommand.
mySetName,
"%s", setName.c_str());
DtDataInteraction dataInter;
myCommandMarshaller.encode(&showSetCommand, &dataInter);
exConn->sendStamped(dataInter);
}
void demo2d()
{
id = 100;
showSet("2D Objects");
setMsg("2D Demo, Text....");
showSet("2D Objects::Text");
int i;
unsigned pattern;
double pos1[2], pos2[2];
double scale[3];
double rot;
pos1[0] = .1;
pos1[1] = .3;
ucolor[0] = 255;ucolor[1] = 255; ucolor[2] = 255; ucolor[3] = 255;
char texts[3][256] ={"2DText1", "2DText2", "2DText with spaces 3"};
for (i = 0; i < 3; i++)
{
myObjects.push_back(text2D);
updateObjects();
ucolor[i] -= 50;
pos1[1] += 0.2;
DtSleep(1);
}
DtSleep(2);
hideSet("2D Objects::Text");
setMsg("2D Demo, Squares....");
showSet("2D Objects::Squares");
pos1[0] = .3;
pos1[1] = .2;
rot = 0;
scale[0] = 0.15;
scale[1] = 0.15;
pattern = 0xffffffff;
ucolor[0] = 255;ucolor[1] = 255; ucolor[2] = 255; ucolor[3] = 255;
for (i = 0; i < 4; i++)
{
myObjects.push_back(square);
updateObjects();
pattern -= 678;
ucolor[i%3] -= 120;
pos1[1] += .15;
rot += DtDeg2Rad(35);
scale[i%2] += 0.05 *(i+1);
DtSleep(1);
}
DtSleep(2);
hideSet("2D Objects::Squares");
setMsg("2D Demo, Circles....");
showSet("2D Objects::Circles");
pos1[0] = 0.5;
pos1[1] = 0.1;
rot = 0;
scale[0] = 0.15;
scale[1] = 0.15;
pattern = 0xbbbbbbbb;
ucolor[0] = 255;ucolor[1] = 255; ucolor[2] = 255; ucolor[3] = 255;
for (i = 0; i < 4; i++)
{
myObjects.push_back(circle);
updateObjects();
pattern -= 678;
ucolor[i%3] -= 80;
pos1[1] += 0.2;
rot += DtDeg2Rad(55);
scale[i%2] += 0.05 *(i+1);
DtSleep(1);
}
DtSleep(2);
hideSet("2D Objects::Circles");
setMsg("2D Demo, Triangles....");
showSet("2D Objects::Triangles");
pos1[0] = 0.7;
pos1[1] = 0.2;
rot = 0;
scale[0] = 0.05;
scale[1] = 0.05;
pattern = 0xbbbbbbbb;
ucolor[0] = 255;ucolor[1] = 255; ucolor[2] = 255; ucolor[3] = 255;
DtVector points[3];
points[0] = DtVector(0, 0, .5);
points[1] = DtVector(1, 0, .5);
points[2] = DtVector(.5, 1, .5);
for (i = 0; i < 4; i++)
{
triangle->
setFill((((i+1)%3) != 0));
myObjects.push_back(triangle);
updateObjects();
pattern -= 678;
ucolor[i%3] -= 80;
pos1[1] += 0.2;
rot += DtDeg2Rad(55);
scale[i%2] += 0.05 *(i+1);
DtSleep(1);
}
DtSleep(2);
hideSet("2D Objects::Triangles");
setMsg("2D Demo, Lines....");
showSet("2D Objects::Lines");
pos2[0] = 0.75;
pos2[1] = 0.2;
pos1[0] = 0.25;
pos1[1] = 0.5;
rot = 0;
scale[0] = 0.05;
scale[1] = 0.05;
pattern = 0xffffffff;
ucolor[0] = 255;ucolor[1] = 255; ucolor[2] = 80; ucolor[3] = 255;
int thick = 1;
for (i = 0; i < 4; i++)
{
myObjects.push_back(line);
updateObjects();
thick += 1;
pattern -= 678;
ucolor[i%3] -= 80;
pos1[1] += .075;
pos2[1] += .075;
DtSleep(1);
}
DtSleep(2);
hideSet("2D Objects::Lines");
pattern = 0xffffffff;
setMsg("2D Demo, Segmented Line....");
showSet("2D Objects::Segmented Lines");
pos2[0] = 0.9;
pos2[1] = 0.1;
pos1[0] = 0.1;
pos1[1] = 0.9;
4);
thick = 1;
ucolor[0] = 255;ucolor[1] = 255; ucolor[2] = 255; ucolor[3] = 255;
for (i = 0; i < 4; i++)
{
pattern -= 0x11111111;
ucolor[i%3] -= 100;
thick += 2;
}
myObjects.push_back(segLine);
updateObjects(2.0);
setMsg("2D Demo, Changing Segmented Line....");
int dt = 25;
int inc = -1;
for(int j = 0; j < 200; j++)
{
for (i = 1; i < 3; i++)
{
}
dt += inc;
if (dt == 25 || dt == 1)
inc *= -1;
updateObjects();
DtSleep(0.1);
}
DtSleep(2);
showSet("2D Objects::Text");
showSet("2D Objects::Squares");
showSet("2D Objects::Circles");
showSet("2D Objects::Triangles");
showSet("2D Objects::Lines");
DtSleep(3);
hideSet("2D Objects");
}
void demo3d()
{
int i = 0;
id = 1000;
showSet("3D Objects");
setMsg("Sphere...");
showSet("3D Objects::Ellipsoids");
unsigned sph1 = id++;
DtVector(20, 20, 20));
myObjects.push_back(sphere);
updateObjects(2.0);
setMsg("Scaling sphere...");
int scales[3];
scales[0] = 20;
scales[1] = 20;
scales[2] = 20;
int inc = 0;
inc = 0;
int clplus = 5;
for (i = 0; inc < 3; i++)
{
scales[inc] += 4;
clsp1[2] += clplus;
sphere->
setDiameters(DtVector(scales[0], scales[1], scales[2]));
updateObjects();
if (scales[inc] > 60)
inc++;
DtSleep(0.1);
}
setMsg("Rotating sphere...");
inc = 0;
scales[0] = 10;
scales[1] = 10;
scales[2] = 10;
for (i = 0; inc < 3; i++)
{
scales[inc] += 5;
clsp1[2] += clplus;
if (clsp1[2] == 250 || clsp1[2] == 50)
clplus *= -1;
sphere->
setRotation(DtTaitBryan(DtDeg2Rad(scales[0]), DtDeg2Rad(scales[1]), DtDeg2Rad(scales[2])));
DtTime tickTime = exConn->clock()->elapsedRealTime();
updateObjects();
if (scales[inc] > 60)
inc++;
DtSleep(0.1);
}
hideSet("3D Objects::Ellipsoids");
setMsg("Cube...");
showSet("3D Objects::Cuboids");
unsigned cubeId = id++;
myObjects.push_back(cube);
updateObjects(2.0);
setMsg("Scaling cube...");
scales[0] = 10;
scales[1] = 10;
scales[2] = 10;
inc = 0;
clplus = 5;
for (i = 0; inc < 3; i++)
{
scales[inc] += 4;
clsp1[3] += clplus;
if (clsp1[3] == 250 || clsp1[3] == 50)
clplus *= -1;
updateObjects();
if (scales[inc] > 60)
inc++;
DtSleep(0.1);
}
setMsg("Rotating cube...");
inc = 0;
scales[0] = 10;
scales[1] = 10;
scales[2] = 10;
for (i = 0; inc < 3; i++)
{
scales[inc] += 5;
clsp1[2] += clplus;
if (clsp1[2] == 250 || clsp1[2] == 50)
clplus *= -1;
cube->
setRotation(DtTaitBryan(DtDeg2Rad(scales[0]), DtDeg2Rad(scales[1]), DtDeg2Rad(scales[2])));
updateObjects();
if (scales[inc] > 60)
inc++;
DtSleep(0.1);
}
updateObjects();
hideSet("3D Objects::Cuboids");
setMsg("Cylinder...");
showSet("3D Objects::Cylinders");
int cyl1 = id++;
myObjects.push_back(cylinder);
updateObjects(2.0);
setMsg("Scaling cylinder...");
scales[0] = 20;
scales[1] = 4;
scales[2] = 4;
inc = 0;
clplus = 5;
int size = 60;
for (i = 0; inc < 3; i++)
{
scales[inc] += 4;
clsp1[3] += clplus;
if (clsp1[3] == 250 || clsp1[3] == 50)
clplus *= -1;
updateObjects();
if (scales[inc] > size)
{
inc++;
size = 15;
}
DtSleep(0.1);
}
setMsg("Rotating cylinder...");
inc = 0;
scales[0] = 10;
scales[1] = 10;
scales[2] = 10;
for (i = 0; inc < 3; i++)
{
scales[inc] += 5;
clsp1[2] += clplus;
if (clsp1[2] == 250 || clsp1[2] == 50)
clplus *= -1;
cylinder->
setRotation(DtTaitBryan(DtDeg2Rad(scales[0]), DtDeg2Rad(scales[1]), DtDeg2Rad(scales[2])));
updateObjects();
if (scales[inc] > 60)
inc++;
DtSleep(0.1);
}
updateObjects();
setMsg("3D texts");
showSet("3D Objects::Text");
myObjects.push_back(text);
updateObjects(2.0);
myObjects.push_back(text2);
updateObjects(2.0);
DtSleep(4);
hideSet("3D Objects::Cylinders");
hideSet("3D Objects::Text");
setMsg("3D lines");
showSet("3D Objects::Lines");
int line1 = id++;
myObjects.push_back(line);
updateObjects(2.0);
myObjects.push_back(line2);
updateObjects(2.0);
DtSleep(4);
hideSet("3D Objects::Lines");
setMsg("Segmented line....");
showSet("3D Objects::Segmented Lines");
myObjects.push_back(segLine);
linecolor2[0] = 255;linecolor2[1] = 255; linecolor2[2] = 255; linecolor2[3] = 255;
for (i = 0; i < 4; i++)
{
linecolor2[i%3] -= 100;
}
updateObjects(2.0);
setMsg("Changing segmented Line....");
int dt = 25;
inc = -1;
for(int j = 0; j < 200; j++)
{
for (i = 1; i < 3; i++)
{
}
dt +=inc;
if (dt == 1 || dt == 25)
inc *= -1;
updateObjects();
DtSleep(0.1);
}
DtSleep(2);
showSet("3D Objects::Cuboids");
showSet("3D Objects::Ellipsoids");
showSet("3D Objects::Cylinders");
showSet("3D Objects::Text");
showSet("3D Objects::Lines");
DtSleep(3);
hideSet("3D Objects");
return;
}
int main(int argc, char** argv)
{
appInit.parseCmdLine();
exConn = new DtExerciseConn(appInit);
exConn->setApplicationId(3, 5);
#if DtHLA
exConn->ignoreAdvisories();
#endif
setMsg("start demo 3D..");
demo3d();
setMsg("End demo 3D..");
DtSleep(2);
setMsg("Start demo 2D..");
demo2d();
setMsg("End demo 2D..");
for (unsigned int i = 0; i < myObjects.size(); ++i)
{
delete myObjects[i];
}
delete exConn;
return 0;
}