28 #include <Inventor/events/SoLocation2Event.h>
29 #include <Inventor/events/SoMouseButtonEvent.h>
30 #include <Inventor/nodes/SoDrawStyle.h>
31 #include <Inventor/nodes/SoLineSet.h>
32 #include <Inventor/nodes/SoMaterial.h>
33 #include <Inventor/nodes/SoPickStyle.h>
34 #include <Inventor/nodes/SoSelection.h>
35 #include <Inventor/nodes/SoSeparator.h>
36 #include <Inventor/nodes/SoVertexProperty.h>
39 SoQtExaminerViewer(widget),
40 sceneRootNode(new SoSeparator),
41 contentRootNode(new SoSelection),
42 camera(new SoPerspectiveCamera)
44 this->setBackgroundColor(SbColor(150 / 255.0f, 150 / 255.0f, 150 / 255.0f));
45 this->setAccumulationBuffer(
true);
46 this->setHeadlight(
true);
47 this->setViewing(
false);
48 this->setDecoration(
false);
51 this->setAntialiasing(
true, 4);
54 this->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
55 this->setFeedbackVisibility(
true);
59 this->setSceneGraph(sceneRootNode);
62 sceneRootNode->addChild(camera);
63 this->setCamera(camera);
66 camera->position.setValue(SbVec3f(10, -10, 5));
67 camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
70 SoSeparator* gridFloorRoot =
new SoSeparator();
71 SoPickStyle* unpickable =
new SoPickStyle();
72 unpickable->style = SoPickStyle::UNPICKABLE;
73 SoPickStyle* pickable =
new SoPickStyle();
74 pickable->style = SoPickStyle::SHAPE;
76 sceneRootNode->addChild(unpickable);
77 sceneRootNode->addChild(gridFloorRoot);
78 sceneRootNode->addChild(pickable);
90 const int GRIDSIZE = 3;
91 const int GRIDUNIT = 15;
92 const int GRIDSUBSIZE = 10;
93 const unsigned short GRIDPATTERN = 0xCCCC;
95 //Red material for X axis
96 SoMaterial* red = new SoMaterial;
97 red->diffuseColor = SbColor(1, 0, 0);
98 //Green material for Y axis
99 SoMaterial* green = new SoMaterial;
100 green->diffuseColor = SbColor(0, 1, 0);
101 //Blue material for Z axis
102 SoMaterial* blue = new SoMaterial;
103 blue->diffuseColor = SbColor(0, 0, 1);
106 SoDrawStyle* thinStyle = new SoDrawStyle;
107 thinStyle->lineWidth = 1;
108 thinStyle->linePattern = GRIDPATTERN;
110 SoDrawStyle* largeStyle = new SoDrawStyle;
111 largeStyle->lineWidth = 2;
113 //Our set of vertices for the grid lines
114 SoVertexProperty* vertices = new SoVertexProperty;
115 SoVertexProperty* subVertices = new SoVertexProperty;
116 SoVertexProperty* xVertices = new SoVertexProperty;
117 SoVertexProperty* yVertices = new SoVertexProperty;
118 SoVertexProperty* zVertices = new SoVertexProperty;
120 //Definition of which vertex belongs to which line and should be connected
121 SoLineSet* lines = new SoLineSet;
122 SoLineSet* subLines = new SoLineSet;
123 SoLineSet* xLines = new SoLineSet;
124 SoLineSet* yLines = new SoLineSet;
125 SoLineSet* zLines = new SoLineSet;
127 //Add 2 vertices for X axis
128 xVertices->vertex.set1Value(0, GRIDSIZE * GRIDUNIT, 0, 0);
129 xVertices->vertex.set1Value(1, -(GRIDSIZE * GRIDUNIT), 0, 0);
130 //Connect them to a line by adding '2' to numVertices at the correct position
131 xLines->numVertices.set1Value(0, 2);
134 yVertices->vertex.set1Value(0, 0, GRIDSIZE * GRIDUNIT, 0);
135 yVertices->vertex.set1Value(1, 0, -(GRIDSIZE * GRIDUNIT), 0);
136 yLines->numVertices.set1Value(0, 2);
139 zVertices->vertex.set1Value(0, 0, 0, GRIDSIZE * GRIDUNIT);
140 zVertices->vertex.set1Value(1, 0, 0, -(GRIDSIZE * GRIDUNIT));
141 zLines->numVertices.set1Value(0, 2);
143 //Counters to keep track of vertex index
144 int verticescounter = 0;
145 int subverticescounter = 0;
147 //Draw all lines parallel to the X and Y axis and all sublines, excepted axis itself
148 for (int i = -(GRIDSIZE * GRIDUNIT); i < GRIDUNIT * (GRIDSIZE + 1); i += GRIDUNIT)
152 vertices->vertex.set1Value(verticescounter++, GRIDSIZE * GRIDUNIT, i, 0);
153 vertices->vertex.set1Value(verticescounter++, -(GRIDSIZE * GRIDUNIT), i, 0);
154 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
156 vertices->vertex.set1Value(verticescounter++, i, GRIDSIZE * GRIDUNIT, 0);
157 vertices->vertex.set1Value(verticescounter++, i, -(GRIDSIZE * GRIDUNIT), 0);
158 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
161 //If this is not the last line to draw, draw sublines
162 if (i < GRIDUNIT * GRIDSIZE)
164 float delta = (float)GRIDUNIT / (float)GRIDSUBSIZE;
166 for (int n = 1; n < GRIDSUBSIZE; n++)
168 subVertices->vertex.set1Value(
169 subverticescounter++, GRIDSIZE * GRIDUNIT, (float)i + (float)n * delta, 0);
170 subVertices->vertex.set1Value(
171 subverticescounter++, -(GRIDSIZE * GRIDUNIT), (float)i + (float)n * delta, 0);
172 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
174 subVertices->vertex.set1Value(
175 subverticescounter++, (float)i + (float)n * delta, GRIDSIZE * GRIDUNIT, 0);
176 subVertices->vertex.set1Value(
177 subverticescounter++, (float)i + (float)n * delta, -(GRIDSIZE * GRIDUNIT), 0);
178 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
184 lines->vertexProperty.setValue(vertices);
185 subLines->vertexProperty.setValue(subVertices);
186 xLines->vertexProperty.setValue(xVertices);
187 yLines->vertexProperty.setValue(yVertices);
188 zLines->vertexProperty.setValue(zVertices);
190 gridFloorRoot->addChild(thinStyle);
191 gridFloorRoot->addChild(subLines);
192 gridFloorRoot->addChild(largeStyle);
193 gridFloorRoot->addChild(lines);
194 gridFloorRoot->addChild(red);
195 gridFloorRoot->addChild(xLines);
196 gridFloorRoot->addChild(green);
197 gridFloorRoot->addChild(yLines);
198 gridFloorRoot->addChild(blue);
199 gridFloorRoot->addChild(zLines);
204 //Create content root node
205 sceneRootNode->addChild(contentRootNode);
208 armarx::RobotViewer::~RobotViewer()
210 sceneRootNode->unref();
214 armarx::RobotViewer::getRootNode()
216 return this->contentRootNode;
220 armarx::RobotViewer::cameraViewAll()
222 camera->viewAll(this->contentRootNode, SbViewportRegion());
225 //Override the default navigation behaviour of the SoQtExaminerViewer
227 armarx::RobotViewer::processSoEvent(const SoEvent* const event)
229 const SoType type(event->getTypeId());
231 //Remapping mouse press events
232 if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId()))
234 SoMouseButtonEvent* const ev = (SoMouseButtonEvent*)event;
235 const int button = ev->getButton();
236 const SbBool press = ev->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;
239 if (button == SoMouseButtonEvent::BUTTON1)
241 SoQtExaminerViewer::processSoEvent(ev);
244 //MIDDLE MOUSE BUTTON
245 if (button == SoMouseButtonEvent::BUTTON3)
247 /*Middle mouse button now is used for all changes in camera perspective.
248 To also display the cool little rotation cursor, viewing mode is set to on
249 while button is down*/
254 if (!this->isViewing())
256 this->setViewing(
true);
261 if (this->isViewing())
263 this->setViewing(
false);
268 ev->setButton(SoMouseButtonEvent::BUTTON1);
274 if (this->isViewing())
277 return SoQtExaminerViewer::processSoEvent(ev);
283 if ((button == SoMouseButtonEvent::BUTTON4 || button == SoMouseButtonEvent::BUTTON5))
289 ev->setButton(button == SoMouseButtonEvent::BUTTON4 ? SoMouseButtonEvent::BUTTON5
290 : SoMouseButtonEvent::BUTTON4);
294 if (!this->isViewing())
296 if (!this->isViewing())
298 this->setViewing(
true);
301 SoQtExaminerViewer::processSoEvent(ev);
303 if (this->isViewing())
305 this->setViewing(
false);
310 SoQtExaminerViewer::processSoEvent(ev);
318 if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId()))
320 const SoKeyboardEvent*
const ev = (
const SoKeyboardEvent*)event;
326 if (ev->getKey() == SoKeyboardEvent::ESCAPE || ev->getKey() == 65513 ||
327 ev->getKey() == SoKeyboardEvent::LEFT_CONTROL)
352 SoQtExaminerViewer::processSoEvent(ev);
355 SoQtExaminerViewer::processSoEvent(ev);
359 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId()))
362 return SoQtExaminerViewer::processSoEvent(event);
369 return SoQtExaminerViewer::processSoEvent(event);