Scene.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package MemoryX::gui-plugins::SceneEditor
19 * @date 2015
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24#include "Scene.h"
25
26#include <Inventor/nodes/SoCone.h>
27#include <Inventor/nodes/SoDrawStyle.h>
28#include <Inventor/nodes/SoLineSet.h>
29#include <Inventor/nodes/SoMaterial.h>
30#include <Inventor/nodes/SoPerspectiveCamera.h>
31#include <Inventor/nodes/SoPickStyle.h>
32#include <Inventor/nodes/SoVertexProperty.h>
33
36{
37 scene3D::ScenePtr scene(new Scene(controller));
38
39 scene->sceneObjectManager.reset(new SceneObjectManager(scene));
40 scene->sceneGroupManager.reset(new SceneGroupManager);
41 scene->sceneSelectionManager.reset(new SceneSelectionManager(scene));
42 scene->sceneManipulatorManager.reset(new SceneManipulatorManager(scene));
43 scene->scenePreviewGenerator.reset(new PreviewGenerator);
44
45 return scene;
46}
47
48scene3D::Scene::Scene(controller::ControllerPtr controller) :
49 viewerMode(false), controller(controller)
50{
51 selectionRootNode = new SoSelection;
52 selectionRootNode->setName("selectionRootNode");
53 selectionRootNode->ref();
54
55 //Add gridfloor root and make it unpickable
56 SoPickStyle* unpickable = new SoPickStyle();
57 unpickable->style = SoPickStyle::UNPICKABLE;
58 SoSeparator* gridFloorRoot = new SoSeparator;
59 SoPickStyle* pickable = new SoPickStyle();
60 pickable->style = SoPickStyle::SHAPE;
61
62 selectionRootNode->addChild(unpickable);
63 selectionRootNode->addChild(gridFloorRoot);
64 selectionRootNode->addChild(pickable);
65
66 /*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 Here we draw our grid floor. To change the way the grid floor is drawn change the following constants.
68 GRIDSIZE: How many lines to display for each direction of each axis.
69 E. g.: 2 would lead to 2 lines in negative and positive direction respectively, plus indicator for axis.
70 5 lines in sum.
71 GRIDUNIT: Factor for coin units, e.g. 2 means between 2 lines there is space for 2 coin units.
72 GRIDSUBSIZE: How many subunits to display between the larger lines. So 5 would lead to 3 lines between 2 large lines for 5
73 subunits.
74 GRIDPATTERN: Pattern for sublines. Bitpattern, where 1 is line and 0 is no line.
75 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
76 const int GRIDSIZE = 5;
77 const int GRIDUNIT = 5;
78 const int GRIDSUBSIZE = 5;
79 const unsigned short GRIDPATTERN = 0xCCCC;
80
81 //Red material for X axis
82 SoMaterial* red = new SoMaterial;
83 red->diffuseColor = SbColor(1, 0, 0);
84 //Green material for Y axis
85 SoMaterial* green = new SoMaterial;
86 green->diffuseColor = SbColor(0, 1, 0);
87 //Blue material for Z axis
88 SoMaterial* blue = new SoMaterial;
89 blue->diffuseColor = SbColor(0, 0, 1);
90
91 //Small lines
92 SoDrawStyle* thinStyle = new SoDrawStyle;
93 thinStyle->lineWidth = 1;
94 thinStyle->linePattern = GRIDPATTERN;
95 //Larger lines
96 SoDrawStyle* largeStyle = new SoDrawStyle;
97 largeStyle->lineWidth = 2;
98
99 //Our set of vertices for the grid lines
100 SoVertexProperty* vertices = new SoVertexProperty;
101 SoVertexProperty* subVertices = new SoVertexProperty;
102 SoVertexProperty* xVertices = new SoVertexProperty;
103 SoVertexProperty* yVertices = new SoVertexProperty;
104 SoVertexProperty* zVertices = new SoVertexProperty;
105
106 //Definition of which vertex belongs to which line and should be connected
107 SoLineSet* lines = new SoLineSet;
108 SoLineSet* subLines = new SoLineSet;
109 SoLineSet* xLines = new SoLineSet;
110 SoLineSet* yLines = new SoLineSet;
111 SoLineSet* zLines = new SoLineSet;
112
113 //Add 2 vertices for X axis
114 xVertices->vertex.set1Value(0, GRIDSIZE * GRIDUNIT, 0, 0);
115 xVertices->vertex.set1Value(1, -(GRIDSIZE * GRIDUNIT), 0, 0);
116 //Connect them to a line by adding '2' to numVertices at the correct position
117 xLines->numVertices.set1Value(0, 2);
118
119 //Y axis
120 yVertices->vertex.set1Value(0, 0, GRIDSIZE * GRIDUNIT, 0);
121 yVertices->vertex.set1Value(1, 0, -(GRIDSIZE * GRIDUNIT), 0);
122 yLines->numVertices.set1Value(0, 2);
123
124 //Z axis
125 zVertices->vertex.set1Value(0, 0, 0, GRIDSIZE * GRIDUNIT);
126 zVertices->vertex.set1Value(1, 0, 0, -(GRIDSIZE * GRIDUNIT));
127 zLines->numVertices.set1Value(0, 2);
128
129 //Counters to keep track of vertex index
130 int verticescounter = 0;
131 int subverticescounter = 0;
132
133 //Draw all lines parallel to the X and Y axis and all sublines, excepted axis itself
134 for (int i = -(GRIDSIZE * GRIDUNIT); i < GRIDUNIT * (GRIDSIZE + 1); i += GRIDUNIT)
135 {
136 if (i != 0)
137 {
138 vertices->vertex.set1Value(verticescounter++, GRIDSIZE * GRIDUNIT, i, 0);
139 vertices->vertex.set1Value(verticescounter++, -(GRIDSIZE * GRIDUNIT), i, 0);
140 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
141
142 vertices->vertex.set1Value(verticescounter++, i, GRIDSIZE * GRIDUNIT, 0);
143 vertices->vertex.set1Value(verticescounter++, i, -(GRIDSIZE * GRIDUNIT), 0);
144 lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
145 }
146
147 //If this is not the last line to draw, draw sublines
148 if (i < GRIDUNIT * GRIDSIZE)
149 {
150 float delta = (float)GRIDUNIT / (float)GRIDSUBSIZE;
151
152 for (int n = 1; n < GRIDSUBSIZE; n++)
153 {
154 subVertices->vertex.set1Value(
155 subverticescounter++, GRIDSIZE * GRIDUNIT, (float)i + (float)n * delta, 0);
156 subVertices->vertex.set1Value(
157 subverticescounter++, -(GRIDSIZE * GRIDUNIT), (float)i + (float)n * delta, 0);
158 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
159
160 subVertices->vertex.set1Value(
161 subverticescounter++, (float)i + (float)n * delta, GRIDSIZE * GRIDUNIT, 0);
162 subVertices->vertex.set1Value(
163 subverticescounter++, (float)i + (float)n * delta, -(GRIDSIZE * GRIDUNIT), 0);
164 subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
165 }
166 }
167 }
168
169
170 lines->vertexProperty.setValue(vertices);
171 subLines->vertexProperty.setValue(subVertices);
172 xLines->vertexProperty.setValue(xVertices);
173 yLines->vertexProperty.setValue(yVertices);
174 zLines->vertexProperty.setValue(zVertices);
175
176 gridFloorRoot->addChild(thinStyle);
177 gridFloorRoot->addChild(subLines);
178 gridFloorRoot->addChild(largeStyle);
179 gridFloorRoot->addChild(lines);
180 gridFloorRoot->addChild(red);
181 gridFloorRoot->addChild(xLines);
182 gridFloorRoot->addChild(green);
183 gridFloorRoot->addChild(yLines);
184 gridFloorRoot->addChild(blue);
185 gridFloorRoot->addChild(zLines);
186
187 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
188 //Gridfloor done
189
190 objectRootNode = new SoSeparator;
191 objectRootNode->setName("objectRootNode");
192 selectionRootNode->addChild(objectRootNode);
193}
194
195scene3D::Scene::~Scene()
196{
197 selectionRootNode->unref();
198}
199
200void
201scene3D::Scene::makeCameraViewAll(SoCamera* camera, const SbViewportRegion& region)
202{
203 camera->viewAll(this->objectRootNode, region);
204}
205
206SoSeparator*
207scene3D::Scene::registerCamera(SoCamera* camera)
208{
209 SoSeparator* cameraRoot = new SoSeparator;
210 cameraRoot->addChild(camera);
211 cameraRoot->addChild(selectionRootNode);
212
213 return cameraRoot;
214}
215
216scene3D::SceneObjectManagerPtr
217scene3D::Scene::getObjectManager()
218{
219 return sceneObjectManager;
220}
221
222scene3D::SceneGroupManagerPtr
223scene3D::Scene::getGroupManager()
224{
225 return sceneGroupManager;
226}
227
228scene3D::SceneSelectionManagerPtr
229scene3D::Scene::getSelectionManager()
230{
231 return sceneSelectionManager;
232}
233
234scene3D::SceneManipulatorManagerPtr
235scene3D::Scene::getManipulatorManager()
236{
237 return sceneManipulatorManager;
238}
239
240scene3D::PreviewGeneratorPtr
241scene3D::Scene::getPreviewGenerator()
242{
243 return scenePreviewGenerator;
244}
245
246controller::ControllerWeakPtr
247scene3D::Scene::getController()
248{
249 return controller;
250}
251
252void
253scene3D::Scene::enterEditorMode()
254{
255 if (this->viewerMode)
256 {
257 this->selectionRootNode->policy = SoSelection::SHIFT;
258 this->sceneSelectionManager->deselectAll();
259 this->sceneSelectionManager->restoreHistory();
260 this->sceneSelectionManager->setCreateOperations(true);
261 this->viewerMode = false;
262 }
263}
264
265void
266scene3D::Scene::enterViewerMode()
267{
268 if (!this->viewerMode)
269 {
270 this->sceneSelectionManager->setCreateOperations(false);
271 this->selectionRootNode->policy = SoSelection::SINGLE;
272 scene3D::SceneObjectPtr activeObject = this->sceneSelectionManager->getLastSelected();
273 this->sceneSelectionManager->storeHistory();
274 this->sceneSelectionManager->deselectAll();
275
276 if (activeObject)
277 {
278 this->sceneSelectionManager->addToSelection(activeObject);
279 }
280
281 this->viewerMode = true;
282 }
283}
friend class SceneObjectManager
Definition Scene.h:63
friend class SceneSelectionManager
Definition Scene.h:65
static ScenePtr create(controller::ControllerPtr controller)
Creates a new instance of the Scene.
Definition Scene.cpp:35
friend class SceneManipulatorManager
Definition Scene.h:67
std::shared_ptr< Controller > ControllerPtr
std::shared_ptr< Scene > ScenePtr