![]() |
VR-Vantage 1.4.1 API Class Documentation
|
This example demonstrates how to use the vrvControlToolkit to draw objects remotely on an instance of VR-Vantage.
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.
VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.
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.
//******************************************************************** // Copyright (c) 1997 MaK Technologies, Inc. // All rights reserved. //******************************************************************** #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 <vrvControl/DtVrlinkDataInterMarshaller.h> #include <vrvControl/vrvControlToolkitDataTypes.h> #include <vrvDrawControl/DtCuboidObject.h> #include <vrvDrawControl/DtEllipsoidObject.h> #include <vrvDrawControl/DtText3DObject.h> #include <vrvDrawControl/DtText2DObject.h> #include <vrvDrawControl/DtLine3DObject.h> #include <vrvDrawControl/DtCylinderObject.h> #include <vrvDrawControl/DtRectangleObject.h> #include <vrvDrawControl/DtTriangle2DObject.h> #include <vrvDrawControl/DtEllipseObject.h> #include <vrvDrawControl/DtLine2DObject.h> #include <vrvDrawControl/DtAttributeGroupObject.h> #include <vrvDrawControl/DtAttributeGroupTemplateObject.h> #include <matrix/topoCoord.h> // #include <stealthControl/absoluteVcPdu.h> #include <stdio.h> #include <vlutil/vlKeyboard.h> using namespace vrvControlToolkit; DtExerciseConn* exConn; std::vector<DtDrawControlObject*> myObjects; DtText2DObject* myText = 0; DtVrlinkDataInterMarshaller<DtDataInteraction> myCommandMarshaller; // #if DtHLA // DtStealthControlInteraction goInter; // #endif int id = 0; // Updates the objects in our objects list. 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()); } } // Prints a messages in 2D text to the screen. 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) { myText = new DtText2DObject(exConn, 300, "", DtVdcVector2D(pos1[0], pos1[1])); 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(); // Process any incoming messages. exConn->drainInput(); DtSleep(0.1); } // Shows the specified set. void showSet(const DtString& setName) { DtRemoteGraphicSet_v1 showSetCommand; sprintf(showSetCommand.mySetName, "%s", setName.c_str()); showSetCommand.myState = true; DtDataInteraction dataInter; myCommandMarshaller.encode(&showSetCommand, &dataInter); exConn->sendStamped(dataInter); } // Hides the specified set. void hideSet(const DtString& setName) { DtRemoteGraphicSet_v1 showSetCommand; sprintf(showSetCommand.mySetName, "%s", setName.c_str()); showSetCommand.myState = false; DtDataInteraction dataInter; myCommandMarshaller.encode(&showSetCommand, &dataInter); exConn->sendStamped(dataInter); } // Runs the 2D demo void demo2d() { id = 100; showSet("2D Objects"); setMsg("2D Demo, Text...."); showSet("2D Objects::Text"); int i; unsigned pattern; double pos1[2], pos2[2]; DtVdcColor ucolor(DtVdcColor::DtVdcWhite); 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++) { DtText2DObject* text2D = new DtText2DObject(exConn, id++, texts[i], DtVdcVector2D(pos1[0], pos1[1])); text2D->setColor(ucolor); text2D->setFlashing(((i % 2) != 0)); text2D->setRemoteGraphicSet("2D Objects::Text"); 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++) { DtRectangleObject* square = new DtRectangleObject(exConn, id++, DtVdcVector2D(pos1[0], pos1[1]), DtVdcVector2D(scale[0] / 2, scale[1] / 2)); square->setColor(ucolor); square->setRotation(rot); square->setFlashing((((i+1)%3) == 0)); square->setFill((((i+1)%3) != 0)); square->setPattern(pattern); square->setRemoteGraphicSet("2D Objects::Squares"); 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++) { DtEllipseObject* circle = new DtEllipseObject(exConn, id++, DtVdcVector2D(pos1[0], pos1[1]), DtVdcVector2D(scale[0] / 2, scale[1] / 2)); circle->setFlashing((((i+1)%3) == 0)); circle->setColor(ucolor); circle->setRotation(rot); circle->setFill((((i+1)%3) != 0)); circle->setPattern(pattern); circle->setRemoteGraphicSet("2D Objects::Circles"); 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++) { DtTriangle2DObject* triangle = new DtTriangle2DObject(exConn, id++, DtVdcVector2D(points[0].x(), points[0].y()), DtVdcVector2D(points[1].x(), points[1].y()), DtVdcVector2D(points[2].x(), points[2].y())); triangle->setOffset(DtVdcVector2D(pos1[0], pos1[1])); triangle->setFlashing((((i+1)%3) == 0)); triangle->setColor(ucolor); triangle->setRotation(rot); triangle->setScale(DtVdcVector2D(scale[0], scale[1])); triangle->setFill((((i+1)%3) != 0)); triangle->setPattern(pattern); triangle->setRemoteGraphicSet("2D Objects::Triangles"); 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++) { DtLine2DObject* line = new DtLine2DObject(exConn, id++, DtVdcVector2D(pos1[0], pos1[1]), DtVdcVector2D(pos2[0], pos2[1])); line->setFlashing((((i+1)%3) == 0)); line->setArrowStyle((DtVdcLineSegment::DtVdcSegmentArrowStyle)(DtVdcLineSegment::DtVdcSegmentArrowStyleNoArrow + i)); line->setLineStyle((DtVdcLineSegment::DtVdcSegmentStyle)(DtVdcLineSegment::DtVdcSegmentStyleStraight + (((i+2)%3) == 0) * DtVdcLineSegment::DtVdcSegmentStyleLightning)); line->setThickness(thick); line->setColor(ucolor); line->setPattern(pattern); line->setRemoteGraphicSet("2D Objects::Lines"); 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; DtLine2DObject* segLine = new DtLine2DObject(exConn, id++, DtVdcVector2D(pos1[0], pos1[1]), DtVdcVector2D(pos2[0], pos2[1]), 4); segLine->setFlashing(0); segLine->setRemoteGraphicSet("2D Objects::Segmented Lines"); thick = 1; ucolor[0] = 255;ucolor[1] = 255; ucolor[2] = 255; ucolor[3] = 255; for (i = 0; i < 4; i++) { segLine->setColor(ucolor, i); segLine->setPattern(pattern, i); segLine->setLineStyle((DtVdcLineSegment::DtVdcSegmentStyle) (DtVdcLineSegment::DtVdcSegmentStyleStraight + (((i+2)%3) == 0) * DtVdcLineSegment::DtVdcSegmentStyleLightning), i); segLine->setArrowStyle((DtVdcLineSegment::DtVdcSegmentArrowStyle)(DtVdcLineSegment::DtVdcSegmentArrowStyleNoArrow + i), i); segLine->setThickness(thick, i); segLine->setPercentage(.25, 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++) { segLine->setPercentage((dt * 0.01), 0); for (i = 1; i < 3; i++) { segLine->setPercentage(0.25, i); } segLine->setPercentage((50 - dt) * 0.01, 3); 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++; DtEllipsoidObject* sphere = new DtEllipsoidObject(exConn, sph1, DtVdc3DAttachPoint(DtVector(3140149, 5440872, 1101333)), DtVector(20, 20, 20)); DtVdcColor clsp1(0, 255 ,0, 100); sphere->setFlashing(false); sphere->setColor(clsp1); sphere->setRemoteGraphicSet("3D Objects::Ellipsoids"); 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])); sphere->setColor(clsp1); 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]))); sphere->setColor(clsp1); 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++; DtVdcColor clsp3(0, 0, 255, 100); DtCuboidObject* cube = new DtCuboidObject(exConn, cubeId, DtVdc3DAttachPoint(sphere->globalId()), 10, 10, 10); cube->setColor(clsp3); cube->setRemoteGraphicSet("3D Objects::Cuboids"); 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; cube->setLength(scales[0]); cube->setWidth(scales[1]); cube->setHeight(scales[2]); cube->setColor(clsp1); 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]))); cube->setColor(clsp1); updateObjects(); if (scales[inc] > 60) inc++; DtSleep(0.1); } cube->setOffset(DtVector(150, 0, 0)); updateObjects(); hideSet("3D Objects::Cuboids"); setMsg("Cylinder..."); showSet("3D Objects::Cylinders"); int cyl1 = id++; DtCylinderObject* cylinder = new DtCylinderObject(exConn, cyl1, DtVdc3DAttachPoint(sphere->globalId()), 20, DtVdcVector2D(4, 4)); cylinder->setFlashing(false); DtVdcColor clrcyl1(255, 0, 255, 60); cylinder->setColor(clrcyl1); cylinder->setRotation(DtTaitBryan(0,0,0)); cylinder->setRemoteGraphicSet("3D Objects::Cylinders"); 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; cylinder->setLength(scales[0]); cylinder->setDiameters(DtVdcVector2D(scales[1], scales[2])); cylinder->setColor(clsp1); 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]))); cylinder->setColor(clsp1); updateObjects(); if (scales[inc] > 60) inc++; DtSleep(0.1); } cylinder->setRotation(DtTaitBryan(0,0,0)); // X is the length, YZ are the radius. Because we are linking between two spheres // the length is omited cylinder->setLength(120); cylinder->setDiameters(DtVdcVector2D(10, 10)); updateObjects(); setMsg("3D texts"); showSet("3D Objects::Text"); DtText3DObject* text = new DtText3DObject(exConn, id++, "Text One", DtVdc3DAttachPoint(cylinder->globalId())); DtVdcColor textcolor(255, 255, 0, 50); text->setFlashing(false); text->setColor(textcolor); text->setOffset(DtVector(0, 15, 0)); text->setRemoteGraphicSet("3D Objects::Text"); myObjects.push_back(text); updateObjects(2.0); DtText3DObject* text2 = new DtText3DObject(exConn, id++, "FLASHING TEXT", DtVdc3DAttachPoint(cylinder->globalId())); text2->setColor(textcolor); text2->setFlashing(true); text2->setOffset(DtVector(45, 0, 0)); text2->setRemoteGraphicSet("3D Objects::Text"); 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++; DtLine3DObject* line = new DtLine3DObject(exConn, line1, DtVdc3DAttachPoint(cylinder->globalId()), DtVdc3DAttachPoint(cube->globalId())); DtVdcColor linecolor1(255 , 255, 50, 255); line->setFlashing(false); line->setColor(linecolor1); line->setPattern(0xffffffff); line->setLineStyle(DtVdcLineSegment::DtVdcSegmentStyleLightning); line->setArrowStyle(DtVdcLineSegment::DtVdcSegmentArrowStyleArrowDouble); line->setThickness(3); line->setOffset(DtVector(0,0,50)); line->setRemoteGraphicSet("3D Objects::Lines"); myObjects.push_back(line); updateObjects(2.0); DtLine3DObject* line2 = new DtLine3DObject(exConn, id++, DtVdc3DAttachPoint(cylinder->globalId()), DtVdc3DAttachPoint(cube->globalId())); DtVdcColor linecolor2(255 , 50, 50, 255); line2->setFlashing(true); line2->setColor(linecolor2); line2->setPattern(0xffffffff); line2->setLineStyle(DtVdcLineSegment::DtVdcSegmentStyleStraight); line2->setArrowStyle(DtVdcLineSegment::DtVdcSegmentArrowStyleArrowDouble); line2->setThickness(1); line2->setOffset(DtVector(0, 0, 70)); line2->setRemoteGraphicSet("3D Objects::Lines"); myObjects.push_back(line2); updateObjects(2.0); DtSleep(4); hideSet("3D Objects::Lines"); setMsg("Segmented line...."); showSet("3D Objects::Segmented Lines"); DtLine3DObject* segLine = new DtLine3DObject(exConn, id++, DtVdc3DAttachPoint(cylinder->globalId()), DtVdc3DAttachPoint(cube->globalId()), 4); segLine->setFlashing(0); segLine->setOffset(DtVector(0,0,90)); segLine->setRemoteGraphicSet("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++) { segLine->setColor(linecolor2, i); segLine->setPattern(0xbbbbbbbb, i); segLine->setArrowStyle(DtVdcLineSegment::DtVdcSegmentArrowStyleArrowStart, i); segLine->setLineStyle(DtVdcLineSegment::DtVdcSegmentStyleStraight); segLine->setThickness(2, i); segLine->setPercentage(0.25, i); linecolor2[i%3] -= 100; } updateObjects(2.0); setMsg("Changing segmented Line...."); int dt = 25; inc = -1; for(int j = 0; j < 200; j++) { segLine->setPercentage(dt * 0.01, 0); for (i = 1; i < 3; i++) { segLine->setPercentage(0.25, i); } segLine->setPercentage((50 - dt) * 0.01, 3); 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) { DtVrlApplicationInitializer appInit(argc, argv, "drawObjects"); 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; }