26 #include <Inventor/nodes/SoSeparator.h>
27 #include <Inventor/nodes/SoSelection.h>
28 #include <Inventor/nodes/SoPickStyle.h>
29 #include <Inventor/nodes/SoMaterial.h>
30 #include <Inventor/nodes/SoDrawStyle.h>
31 #include <Inventor/nodes/SoVertexProperty.h>
32 #include <Inventor/nodes/SoLineSet.h>
34 #include <Inventor/events/SoMouseButtonEvent.h>
35 #include <Inventor/events/SoLocation2Event.h>
39 armarx::RobotViewer::RobotViewer(QWidget* widget) : SoQtExaminerViewer(widget), sceneRootNode(new SoSeparator), contentRootNode(new SoSelection), camera(new SoPerspectiveCamera)
41 this->setBackgroundColor(SbColor(150 / 255.0f, 150 / 255.0f, 150 / 255.0f));
42 this->setAccumulationBuffer(
true);
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(subverticescounter++, GRIDSIZE * GRIDUNIT, (float)i + (float)n * delta, 0);
166 subVertices->vertex.set1Value(subverticescounter++, -(GRIDSIZE * GRIDUNIT), (float)i + (float)n * delta, 0);
167 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
169 subVertices->vertex.set1Value(subverticescounter++, (float)i + (float)n * delta, GRIDSIZE * GRIDUNIT, 0);
170 subVertices->vertex.set1Value(subverticescounter++, (float)i + (float)n * delta, -(GRIDSIZE * GRIDUNIT), 0);
171 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
177 lines->vertexProperty.setValue(vertices);
178 subLines->vertexProperty.setValue(subVertices);
179 xLines->vertexProperty.setValue(xVertices);
180 yLines->vertexProperty.setValue(yVertices);
181 zLines->vertexProperty.setValue(zVertices);
183 gridFloorRoot->addChild(thinStyle);
184 gridFloorRoot->addChild(subLines);
185 gridFloorRoot->addChild(largeStyle);
186 gridFloorRoot->addChild(lines);
187 gridFloorRoot->addChild(red);
188 gridFloorRoot->addChild(xLines);
189 gridFloorRoot->addChild(green);
190 gridFloorRoot->addChild(yLines);
191 gridFloorRoot->addChild(blue);
192 gridFloorRoot->addChild(zLines);
197 //Create content root node
198 sceneRootNode->addChild(contentRootNode);
201 armarx::RobotViewer::~RobotViewer()
203 sceneRootNode->unref();
206 SoSelection* armarx::RobotViewer::getRootNode()
208 return this->contentRootNode;
211 void armarx::RobotViewer::cameraViewAll()
213 camera->viewAll(this->contentRootNode, SbViewportRegion());
218 //Override the default navigation behaviour of the SoQtExaminerViewer
219 SbBool armarx::RobotViewer::processSoEvent(const SoEvent* const event)
221 const SoType type(event->getTypeId());
223 //Remapping mouse press events
224 if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId()))
226 SoMouseButtonEvent* const ev = (SoMouseButtonEvent*) event;
227 const int button = ev->getButton();
228 const SbBool press = ev->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;
231 if (button == SoMouseButtonEvent::BUTTON1)
233 SoQtExaminerViewer::processSoEvent(ev);
236 //MIDDLE MOUSE BUTTON
237 if (button == SoMouseButtonEvent::BUTTON3)
239 /*Middle mouse button now is used for all changes in camera perspective.
240 To also display the cool little rotation cursor, viewing mode is set to on
241 while button is down*/
246 if (!this->isViewing())
248 this->setViewing(
true);
253 if (this->isViewing())
255 this->setViewing(
false);
260 ev->setButton(SoMouseButtonEvent::BUTTON1);
266 if (this->isViewing())
269 return SoQtExaminerViewer::processSoEvent(ev);
275 if ((button == SoMouseButtonEvent::BUTTON4 || button == SoMouseButtonEvent::BUTTON5))
281 ev->setButton(button == SoMouseButtonEvent::BUTTON4 ?
282 SoMouseButtonEvent::BUTTON5 : SoMouseButtonEvent::BUTTON4);
286 if (!this->isViewing())
288 if (!this->isViewing())
290 this->setViewing(
true);
293 SoQtExaminerViewer::processSoEvent(ev);
295 if (this->isViewing())
297 this->setViewing(
false);
302 SoQtExaminerViewer::processSoEvent(ev);
310 if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId()))
312 const SoKeyboardEvent*
const ev = (
const SoKeyboardEvent*) event;
318 if (ev->getKey() == SoKeyboardEvent::ESCAPE || ev->getKey() == 65513 || ev->getKey() == SoKeyboardEvent::LEFT_CONTROL)
343 SoQtExaminerViewer::processSoEvent(ev);
346 SoQtExaminerViewer::processSoEvent(ev);
350 if (type.isDerivedFrom(SoLocation2Event::getClassTypeId()))
353 return SoQtExaminerViewer::processSoEvent(event);
360 return SoQtExaminerViewer::processSoEvent(event);