26 #include <Inventor/nodes/SoSeparator.h>
27 #include <Inventor/nodes/SoPickStyle.h>
28 #include <Inventor/nodes/SoMaterial.h>
29 #include <Inventor/nodes/SoDrawStyle.h>
30 #include <Inventor/nodes/SoVertexProperty.h>
31 #include <Inventor/nodes/SoLineSet.h>
33 #include <Inventor/events/SoMouseButtonEvent.h>
34 #include <Inventor/events/SoLocation2Event.h>
36 armarx::RobotViewer::RobotViewer(QWidget* widget) : SoQtExaminerViewer(widget), sceneRootNode(new SoSeparator), contentRootNode(new SoSeparator), camera(new SoPerspectiveCamera)
38 this->setBackgroundColor(SbColor(150 / 255.0f, 150 / 255.0f, 150 / 255.0f));
39 this->setAccumulationBuffer(this->getAccumulationBuffer());
40 this->setHeadlight(
true);
41 this->setViewing(
false);
42 this->setDecoration(
false);
45 this->setAntialiasing(
true, 4);
48 this->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
49 this->setFeedbackVisibility(
true);
53 this->setSceneGraph(sceneRootNode);
56 sceneRootNode->addChild(camera);
57 this->setCamera(camera);
60 camera->position.setValue(SbVec3f(10, -10, 5));
61 camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
64 SoSeparator* gridFloorRoot =
new SoSeparator();
65 SoPickStyle* unpickable =
new SoPickStyle();
66 unpickable->style = SoPickStyle::UNPICKABLE;
67 SoPickStyle* pickable =
new SoPickStyle();
68 pickable->style = SoPickStyle::SHAPE;
70 sceneRootNode->addChild(unpickable);
71 sceneRootNode->addChild(gridFloorRoot);
72 sceneRootNode->addChild(pickable);
84 const int GRIDSIZE = 3;
85 const int GRIDUNIT = 15;
86 const int GRIDSUBSIZE = 10;
87 const unsigned short GRIDPATTERN = 0xCCCC;
89 //Red material for X axis
90 SoMaterial* red = new SoMaterial;
91 red->diffuseColor = SbColor(1, 0, 0);
92 //Green material for Y axis
93 SoMaterial* green = new SoMaterial;
94 green->diffuseColor = SbColor(0, 1, 0);
95 //Blue material for Z axis
96 SoMaterial* blue = new SoMaterial;
97 blue->diffuseColor = SbColor(0, 0, 1);
100 SoDrawStyle* thinStyle = new SoDrawStyle;
101 thinStyle->lineWidth = 1;
102 thinStyle->linePattern = GRIDPATTERN;
104 SoDrawStyle* largeStyle = new SoDrawStyle;
105 largeStyle->lineWidth = 2;
107 //Our set of vertices for the grid lines
108 SoVertexProperty* vertices = new SoVertexProperty;
109 SoVertexProperty* subVertices = new SoVertexProperty;
110 SoVertexProperty* xVertices = new SoVertexProperty;
111 SoVertexProperty* yVertices = new SoVertexProperty;
112 SoVertexProperty* zVertices = new SoVertexProperty;
114 //Definition of which vertex belongs to which line and should be connected
115 SoLineSet* lines = new SoLineSet;
116 SoLineSet* subLines = new SoLineSet;
117 SoLineSet* xLines = new SoLineSet;
118 SoLineSet* yLines = new SoLineSet;
119 SoLineSet* zLines = new SoLineSet;
121 //Add 2 vertices for X axis
122 xVertices->vertex.set1Value(0, GRIDSIZE * GRIDUNIT, 0, 0);
123 xVertices->vertex.set1Value(1, -(GRIDSIZE * GRIDUNIT), 0, 0);
124 //Connect them to a line by adding '2' to numVertices at the correct position
125 xLines->numVertices.set1Value(0, 2);
128 yVertices->vertex.set1Value(0, 0, GRIDSIZE * GRIDUNIT, 0);
129 yVertices->vertex.set1Value(1, 0, -(GRIDSIZE * GRIDUNIT), 0);
130 yLines->numVertices.set1Value(0, 2);
133 zVertices->vertex.set1Value(0, 0, 0, GRIDSIZE * GRIDUNIT);
134 zVertices->vertex.set1Value(1, 0, 0, -(GRIDSIZE * GRIDUNIT));
135 zLines->numVertices.set1Value(0, 2);
137 //Counters to keep track of vertex index
138 int verticescounter = 0;
139 int subverticescounter = 0;
141 //Draw all lines parallel to the X and Y axis and all sublines, excepted axis itself
142 for (int i = -(GRIDSIZE * GRIDUNIT); i < GRIDUNIT * (GRIDSIZE + 1); i += GRIDUNIT)
146 vertices->vertex.set1Value(verticescounter++, GRIDSIZE * GRIDUNIT, i, 0);
147 vertices->vertex.set1Value(verticescounter++, -(GRIDSIZE * GRIDUNIT), i, 0);
148 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
150 vertices->vertex.set1Value(verticescounter++, i, GRIDSIZE * GRIDUNIT, 0);
151 vertices->vertex.set1Value(verticescounter++, i, -(GRIDSIZE * GRIDUNIT), 0);
152 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
155 //If this is not the last line to draw, draw sublines
156 if (i < GRIDUNIT * GRIDSIZE)
158 float delta = (float)GRIDUNIT / (float)GRIDSUBSIZE;
160 for (int n = 1; n < GRIDSUBSIZE; n++)
162 subVertices->vertex.set1Value(subverticescounter++, GRIDSIZE * GRIDUNIT, (float)i + (float)n * delta, 0);
163 subVertices->vertex.set1Value(subverticescounter++, -(GRIDSIZE * GRIDUNIT), (float)i + (float)n * delta, 0);
164 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
166 subVertices->vertex.set1Value(subverticescounter++, (float)i + (float)n * delta, GRIDSIZE * GRIDUNIT, 0);
167 subVertices->vertex.set1Value(subverticescounter++, (float)i + (float)n * delta, -(GRIDSIZE * GRIDUNIT), 0);
168 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
174 lines->vertexProperty.setValue(vertices);
175 subLines->vertexProperty.setValue(subVertices);
176 xLines->vertexProperty.setValue(xVertices);
177 yLines->vertexProperty.setValue(yVertices);
178 zLines->vertexProperty.setValue(zVertices);
180 gridFloorRoot->addChild(thinStyle);
181 gridFloorRoot->addChild(subLines);
182 gridFloorRoot->addChild(largeStyle);
183 gridFloorRoot->addChild(lines);
184 gridFloorRoot->addChild(red);
185 gridFloorRoot->addChild(xLines);
186 gridFloorRoot->addChild(green);
187 gridFloorRoot->addChild(yLines);
188 gridFloorRoot->addChild(blue);
189 gridFloorRoot->addChild(zLines);
194 //Create content root node
195 sceneRootNode->addChild(contentRootNode);
198 armarx::RobotViewer::~RobotViewer()
200 sceneRootNode->unref();
203 SoSeparator* armarx::RobotViewer::getRootNode()
205 return this->contentRootNode;
208 void armarx::RobotViewer::cameraViewAll()
210 camera->viewAll(this->contentRootNode, SbViewportRegion());
213 //Override the default navigation behaviour of the SoQtExaminerViewer
214 SbBool armarx::RobotViewer::processSoEvent(const SoEvent* const event)
216 const SoType type(event->getTypeId());
218 //Remapping mouse press events
219 if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId()))
221 SoMouseButtonEvent* const ev = (SoMouseButtonEvent*) event;
222 const int button = ev->getButton();
223 const SbBool press = ev->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;
226 if (button == SoMouseButtonEvent::BUTTON1)
228 SoQtExaminerViewer::processSoEvent(ev);
231 //MIDDLE MOUSE BUTTON
232 if (button == SoMouseButtonEvent::BUTTON3)
234 /*Middle mouse button now is used for all changes in camera perspective.
235 To also display the cool little rotation cursor, viewing mode is set to on
236 while button is down*/
241 if (!this->isViewing())
243 this->setViewing(
true);
248 if (this->isViewing())
250 this->setViewing(
false);
255 ev->setButton(SoMouseButtonEvent::BUTTON1);
261 if (this->isViewing())
263 return SoQtExaminerViewer::processSoEvent(ev);
268 if (button == SoMouseButtonEvent::BUTTON4 || button == SoMouseButtonEvent::BUTTON5)
274 ev->setButton(button == SoMouseButtonEvent::BUTTON4 ?
275 SoMouseButtonEvent::BUTTON5 : SoMouseButtonEvent::BUTTON4);
279 if (!this->isViewing())
281 if (!this->isViewing())
283 this->setViewing(
true);
286 SoQtExaminerViewer::processSoEvent(ev);
288 if (this->isViewing())
290 this->setViewing(
false);
295 SoQtExaminerViewer::processSoEvent(ev);
303 if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId()))
305 const SoKeyboardEvent*
const ev = (
const SoKeyboardEvent*) event;
311 if (ev->getKey() == SoKeyboardEvent::ESCAPE || ev->getKey() == 65513)
315 else if (ev->getKey() == SoKeyboardEvent::S && ev->getState() == SoButtonEvent::DOWN)
317 if (!this->isSeekMode())
319 if (!this->isViewing())
321 this->setViewing(
true);
324 SoQtExaminerViewer::processSoEvent(ev);
325 this->setSeekTime(0.5);
326 this->seekToPoint(ev->getPosition());
328 if (this->isViewing())
330 this->setViewing(
false);
336 SoQtExaminerViewer::processSoEvent(ev);
339 SoQtExaminerViewer::processSoEvent(ev);
343 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId()))
345 return SoQtExaminerViewer::processSoEvent(event);
351 return SoQtExaminerViewer::processSoEvent(event);