26 #include <Inventor/events/SoLocation2Event.h>
27 #include <Inventor/events/SoMouseButtonEvent.h>
28 #include <Inventor/nodes/SoDrawStyle.h>
29 #include <Inventor/nodes/SoLineSet.h>
30 #include <Inventor/nodes/SoMaterial.h>
31 #include <Inventor/nodes/SoPickStyle.h>
32 #include <Inventor/nodes/SoSeparator.h>
33 #include <Inventor/nodes/SoVertexProperty.h>
36 SoQtExaminerViewer(widget),
37 sceneRootNode(new SoSeparator),
38 contentRootNode(new SoSeparator),
39 camera(new SoPerspectiveCamera)
41 this->setBackgroundColor(SbColor(150 / 255.0f, 150 / 255.0f, 150 / 255.0f));
42 this->setAccumulationBuffer(this->getAccumulationBuffer());
43 this->setHeadlight(
true);
44 this->setViewing(
false);
45 this->setDecoration(
false);
48 this->setAntialiasing(
true, 4);
51 this->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
52 this->setFeedbackVisibility(
true);
56 this->setSceneGraph(sceneRootNode);
59 sceneRootNode->addChild(camera);
60 this->setCamera(camera);
63 camera->position.setValue(SbVec3f(10, -10, 5));
64 camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
67 SoSeparator* gridFloorRoot =
new SoSeparator();
68 SoPickStyle* unpickable =
new SoPickStyle();
69 unpickable->style = SoPickStyle::UNPICKABLE;
70 SoPickStyle* pickable =
new SoPickStyle();
71 pickable->style = SoPickStyle::SHAPE;
73 sceneRootNode->addChild(unpickable);
74 sceneRootNode->addChild(gridFloorRoot);
75 sceneRootNode->addChild(pickable);
87 const int GRIDSIZE = 3;
88 const int GRIDUNIT = 15;
89 const int GRIDSUBSIZE = 10;
90 const unsigned short GRIDPATTERN = 0xCCCC;
92 //Red material for X axis
93 SoMaterial* red = new SoMaterial;
94 red->diffuseColor = SbColor(1, 0, 0);
95 //Green material for Y axis
96 SoMaterial* green = new SoMaterial;
97 green->diffuseColor = SbColor(0, 1, 0);
98 //Blue material for Z axis
99 SoMaterial* blue = new SoMaterial;
100 blue->diffuseColor = SbColor(0, 0, 1);
103 SoDrawStyle* thinStyle = new SoDrawStyle;
104 thinStyle->lineWidth = 1;
105 thinStyle->linePattern = GRIDPATTERN;
107 SoDrawStyle* largeStyle = new SoDrawStyle;
108 largeStyle->lineWidth = 2;
110 //Our set of vertices for the grid lines
111 SoVertexProperty* vertices = new SoVertexProperty;
112 SoVertexProperty* subVertices = new SoVertexProperty;
113 SoVertexProperty* xVertices = new SoVertexProperty;
114 SoVertexProperty* yVertices = new SoVertexProperty;
115 SoVertexProperty* zVertices = new SoVertexProperty;
117 //Definition of which vertex belongs to which line and should be connected
118 SoLineSet* lines = new SoLineSet;
119 SoLineSet* subLines = new SoLineSet;
120 SoLineSet* xLines = new SoLineSet;
121 SoLineSet* yLines = new SoLineSet;
122 SoLineSet* zLines = new SoLineSet;
124 //Add 2 vertices for X axis
125 xVertices->vertex.set1Value(0, GRIDSIZE * GRIDUNIT, 0, 0);
126 xVertices->vertex.set1Value(1, -(GRIDSIZE * GRIDUNIT), 0, 0);
127 //Connect them to a line by adding '2' to numVertices at the correct position
128 xLines->numVertices.set1Value(0, 2);
131 yVertices->vertex.set1Value(0, 0, GRIDSIZE * GRIDUNIT, 0);
132 yVertices->vertex.set1Value(1, 0, -(GRIDSIZE * GRIDUNIT), 0);
133 yLines->numVertices.set1Value(0, 2);
136 zVertices->vertex.set1Value(0, 0, 0, GRIDSIZE * GRIDUNIT);
137 zVertices->vertex.set1Value(1, 0, 0, -(GRIDSIZE * GRIDUNIT));
138 zLines->numVertices.set1Value(0, 2);
140 //Counters to keep track of vertex index
141 int verticescounter = 0;
142 int subverticescounter = 0;
144 //Draw all lines parallel to the X and Y axis and all sublines, excepted axis itself
145 for (int i = -(GRIDSIZE * GRIDUNIT); i < GRIDUNIT * (GRIDSIZE + 1); i += GRIDUNIT)
149 vertices->vertex.set1Value(verticescounter++, GRIDSIZE * GRIDUNIT, i, 0);
150 vertices->vertex.set1Value(verticescounter++, -(GRIDSIZE * GRIDUNIT), i, 0);
151 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
153 vertices->vertex.set1Value(verticescounter++, i, GRIDSIZE * GRIDUNIT, 0);
154 vertices->vertex.set1Value(verticescounter++, i, -(GRIDSIZE * GRIDUNIT), 0);
155 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
158 //If this is not the last line to draw, draw sublines
159 if (i < GRIDUNIT * GRIDSIZE)
161 float delta = (float)GRIDUNIT / (float)GRIDSUBSIZE;
163 for (int n = 1; n < GRIDSUBSIZE; n++)
165 subVertices->vertex.set1Value(
166 subverticescounter++, GRIDSIZE * GRIDUNIT, (float)i + (float)n * delta, 0);
167 subVertices->vertex.set1Value(
168 subverticescounter++, -(GRIDSIZE * GRIDUNIT), (float)i + (float)n * delta, 0);
169 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
171 subVertices->vertex.set1Value(
172 subverticescounter++, (float)i + (float)n * delta, GRIDSIZE * GRIDUNIT, 0);
173 subVertices->vertex.set1Value(
174 subverticescounter++, (float)i + (float)n * delta, -(GRIDSIZE * GRIDUNIT), 0);
175 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
181 lines->vertexProperty.setValue(vertices);
182 subLines->vertexProperty.setValue(subVertices);
183 xLines->vertexProperty.setValue(xVertices);
184 yLines->vertexProperty.setValue(yVertices);
185 zLines->vertexProperty.setValue(zVertices);
187 gridFloorRoot->addChild(thinStyle);
188 gridFloorRoot->addChild(subLines);
189 gridFloorRoot->addChild(largeStyle);
190 gridFloorRoot->addChild(lines);
191 gridFloorRoot->addChild(red);
192 gridFloorRoot->addChild(xLines);
193 gridFloorRoot->addChild(green);
194 gridFloorRoot->addChild(yLines);
195 gridFloorRoot->addChild(blue);
196 gridFloorRoot->addChild(zLines);
201 //Create content root node
202 sceneRootNode->addChild(contentRootNode);
205 armarx::RobotViewer::~RobotViewer()
207 sceneRootNode->unref();
211 armarx::RobotViewer::getRootNode()
213 return this->contentRootNode;
217 armarx::RobotViewer::cameraViewAll()
219 camera->viewAll(this->contentRootNode, SbViewportRegion());
222 //Override the default navigation behaviour of the SoQtExaminerViewer
224 armarx::RobotViewer::processSoEvent(const SoEvent* const event)
226 const SoType type(event->getTypeId());
228 //Remapping mouse press events
229 if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId()))
231 SoMouseButtonEvent* const ev = (SoMouseButtonEvent*)event;
232 const int button = ev->getButton();
233 const SbBool press = ev->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;
236 if (button == SoMouseButtonEvent::BUTTON1)
238 SoQtExaminerViewer::processSoEvent(ev);
241 //MIDDLE MOUSE BUTTON
242 if (button == SoMouseButtonEvent::BUTTON3)
244 /*Middle mouse button now is used for all changes in camera perspective.
245 To also display the cool little rotation cursor, viewing mode is set to on
246 while button is down*/
251 if (!this->isViewing())
253 this->setViewing(
true);
258 if (this->isViewing())
260 this->setViewing(
false);
265 ev->setButton(SoMouseButtonEvent::BUTTON1);
271 if (this->isViewing())
273 return SoQtExaminerViewer::processSoEvent(ev);
278 if (button == SoMouseButtonEvent::BUTTON4 || button == SoMouseButtonEvent::BUTTON5)
284 ev->setButton(button == SoMouseButtonEvent::BUTTON4 ? SoMouseButtonEvent::BUTTON5
285 : SoMouseButtonEvent::BUTTON4);
289 if (!this->isViewing())
291 if (!this->isViewing())
293 this->setViewing(
true);
296 SoQtExaminerViewer::processSoEvent(ev);
298 if (this->isViewing())
300 this->setViewing(
false);
305 SoQtExaminerViewer::processSoEvent(ev);
313 if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId()))
315 const SoKeyboardEvent*
const ev = (
const SoKeyboardEvent*)event;
321 if (ev->getKey() == SoKeyboardEvent::ESCAPE || ev->getKey() == 65513)
325 else if (ev->getKey() == SoKeyboardEvent::S && ev->getState() == SoButtonEvent::DOWN)
327 if (!this->isSeekMode())
329 if (!this->isViewing())
331 this->setViewing(
true);
334 SoQtExaminerViewer::processSoEvent(ev);
335 this->setSeekTime(0.5);
336 this->seekToPoint(ev->getPosition());
338 if (this->isViewing())
340 this->setViewing(
false);
346 SoQtExaminerViewer::processSoEvent(ev);
349 SoQtExaminerViewer::processSoEvent(ev);
353 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId()))
355 return SoQtExaminerViewer::processSoEvent(event);
361 return SoQtExaminerViewer::processSoEvent(event);