fixed: compile issue
[opensg.git] / Source / Contrib / Manipulators / testManipulators.cpp
blobde67e1ca178aae1f47718ac848a35e217adc8fae
1 #include "OSGGLUT.h"
2 #include "OSGConfig.h"
3 #include "OSGLog.h"
4 #include "OSGGLUTWindow.h"
5 #include "OSGSimpleSceneManager.h"
6 #include "OSGGroup.h"
7 #include "OSGSimpleMaterial.h"
8 #include "OSGTransform.h"
9 #include "OSGSimpleGeometry.h"
10 #include "OSGBaseFunctions.h"
11 #include "OSGSceneFileHandler.h"
12 #include "OSGTypedGeoIntegralProperty.h"
14 #include "OSGManipulatorManager.h"
17 OSG::NodeRefPtr scene = NULL;
18 OSG::TransformRefPtr interTC = NULL;
19 OSG::NodeRefPtr interN = NULL;
20 OSG::NodeRefPtr maniN = NULL;
22 OSG::ManipulatorManager *mama;
23 OSG::SimpleSceneManagerRefPtr mgr;
25 int setupGLUT( int *argc, char *argv[] );
26 OSG::NodeTransitPtr makeCoordAxes(void);
28 int main(int argc, char **argv)
30 OSG::osgInit(argc,argv);
31 int winid = setupGLUT(&argc, argv);
33 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
34 gwin->setGlutId(winid);
35 gwin->init();
37 mgr = OSG::SimpleSceneManager::create();
38 mama = new OSG::ManipulatorManager;
40 OSG::GroupUnrecPtr g = OSG::Group::create();
41 scene = OSG::Node::create();
43 OSG::TransformUnrecPtr offsetTC = OSG::Transform::create();
44 OSG::NodeUnrecPtr offsetN = OSG::Node::create();
46 OSG::Matrix m;
47 m.setIdentity();
48 m.setTransform( OSG::Vec3f(2,1,0),
49 OSG::Quaternion(OSG::Vec3f(1,0,0), OSG::osgDegree2Rad(0)),
50 OSG::Vec3f(1,1,1) );
51 offsetTC->setMatrix(m);
52 offsetN->setCore(offsetTC);
54 interTC = OSG::Transform::create();
55 interN = OSG::Node::create();
57 m.setIdentity();
58 m.setTransform( OSG::Vec3f(1.0f, 1.0f, 1.0f) );
59 interTC->setMatrix(m);
61 interN->setCore(interTC);
62 interN->addChild(OSG::makeBox(1, 0.2f, 0.2f, 1, 1, 1));
64 scene->setCore(g);
65 scene->addChild(makeCoordAxes());
66 scene->addChild(offsetN);
67 offsetN->addChild(interN);
69 maniN = mama->createManipulator(OSG::ManipulatorManager::TRANSLATE);
71 interN->addChild(maniN);
73 mgr->setWindow(gwin );
74 mgr->setRoot (scene);
75 mgr->setClickCenter( false );
76 mgr->showAll();
78 mama->setTarget( interN );
79 mama->setViewport( mgr->getWindow()->getPort(0) );
81 OSG::commitChanges();
83 glutReshapeWindow(600, 600);
84 glutMainLoop();
86 return 0;
90 void display(void)
92 mgr->redraw();
96 void reshape(int w, int h)
98 mgr->resize(w, h);
99 glutPostRedisplay();
103 void mouse(int button, int state, int x, int y)
105 if (state)
107 if( mama->isActive() )
109 mama->mouseButtonRelease(button, x, y);
112 mgr->mouseButtonRelease(button, x, y);
114 else
116 OSG::Line l;
118 l = mgr->calcViewRay(x, y);
120 OSG::IntersectActionRefPtr act = OSG::IntersectAction::create();
122 act->setLine( l );
123 act->apply( scene );
125 if ( (act->didHit()) && (mama->activate( act->getHitObject()) ) )
127 mama->mouseButtonPress(button, x, y);
130 act = NULL;
132 mgr->mouseButtonPress(button, x, y);
135 glutPostRedisplay();
139 void motion(int x, int y)
141 if( mama->isActive() )
143 mama->mouseMove(x, y);
145 else
147 mgr->mouseMove(x, y);
150 glutPostRedisplay();
154 void keyboard(unsigned char k, int x, int y)
156 switch(k)
158 case 27:
159 delete mama;
160 mgr = NULL;
161 scene = NULL;
162 interTC = NULL;
163 interN = NULL;
164 maniN = NULL;
165 OSG::osgExit();
166 exit(1);
167 case ' ':
169 maniN->setTravMask(OSG::TypeTraits<OSG::UInt32>::getZeroElement());
170 glutPostRedisplay();
171 break;
173 case 'r':
174 case 't':
175 case 's':
176 case 'p':
177 case 'P':
179 maniN->setTravMask(OSG::TypeTraits<OSG::UInt32>::getMax());
180 if (k == 't')
182 mama->changeManipulator(OSG::ManipulatorManager::TRANSLATE);
184 else if (k == 'r')
186 mama->changeManipulator(OSG::ManipulatorManager::ROTATE);
188 else if (k == 's')
190 mama->changeManipulator(OSG::ManipulatorManager::SCALE);
192 else if (k == 'p')
194 mama->changeManipulator(OSG::ManipulatorManager::PLANE);
196 else if (k == 'P')
198 mama->changeEnablePivot(! mama->getCurrentEnablePivot());
200 glutPostRedisplay();
201 break;
203 case 'w':
204 OSG::SceneFileHandler::the()->write(scene, "scene.osb");
205 break;
210 int setupGLUT(int *argc, char *argv[])
212 glutInit(argc, argv);
213 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
215 int winid = glutCreateWindow("Manipulator Demo");
217 glutReshapeFunc(reshape);
218 glutDisplayFunc(display);
219 glutMouseFunc(mouse);
220 glutMotionFunc(motion);
221 glutKeyboardFunc(keyboard);
223 return winid;
226 OSG::GeometryTransitPtr makeCoordAxesGeo(void)
228 OSG::GeometryTransitPtr axesG = OSG::Geometry::create();
230 OSG::GeoUInt8PropertyUnrecPtr type = OSG::GeoUInt8Property::create();
231 type->addValue(GL_LINES );
232 type->addValue(GL_TRIANGLES);
234 OSG::GeoUInt32PropertyUnrecPtr lens = OSG::GeoUInt32Property::create();
235 lens->addValue(6);
236 lens->addValue(9);
238 OSG::GeoPnt3fPropertyUnrecPtr pnts = OSG::GeoPnt3fProperty::create();
239 // the 6 points of the three Lines
240 pnts->addValue(OSG::Pnt3f(0, 0, 0));
241 pnts->addValue(OSG::Pnt3f(1, 0, 0));
243 pnts->addValue(OSG::Pnt3f(0, 0, 0));
244 pnts->addValue(OSG::Pnt3f(0, 1, 0));
246 pnts->addValue(OSG::Pnt3f(0, 0, 0));
247 pnts->addValue(OSG::Pnt3f(0, 0, 1));
249 // the 9 points of the three Triangles
250 pnts->addValue(OSG::Pnt3f( 1, 0, 0));
251 pnts->addValue(OSG::Pnt3f(0.8f, 0.2f, 0.2f));
252 pnts->addValue(OSG::Pnt3f(0.8f, -0.2f, -0.2f));
254 pnts->addValue(OSG::Pnt3f( 0, 1, 0));
255 pnts->addValue(OSG::Pnt3f( 0.2f, 0.8f, 0.2f));
256 pnts->addValue(OSG::Pnt3f(-0.2f, 0.8f, -0.2f));
258 pnts->addValue(OSG::Pnt3f( 0, 0, 1));
259 pnts->addValue(OSG::Pnt3f( 0.2f, 0.2f, 0.8f));
260 pnts->addValue(OSG::Pnt3f(-0.2f, -0.2f, 0.8f));
262 OSG::GeoColor3fPropertyUnrecPtr colors = OSG::GeoColor3fProperty::create();
263 colors->addValue(OSG::Color3f(1, 0, 0));
264 colors->addValue(OSG::Color3f(1, 0, 0));
266 colors->addValue(OSG::Color3f(0, 1, 0));
267 colors->addValue(OSG::Color3f(0, 1, 0));
269 colors->addValue(OSG::Color3f(0, 0, 1));
270 colors->addValue(OSG::Color3f(0, 0, 1));
272 colors->addValue(OSG::Color3f(1, 0, 0));
273 colors->addValue(OSG::Color3f(1, 0, 0));
274 colors->addValue(OSG::Color3f(1, 0, 0));
276 colors->addValue(OSG::Color3f(0, 1, 0));
277 colors->addValue(OSG::Color3f(0, 1, 0));
278 colors->addValue(OSG::Color3f(0, 1, 0));
280 colors->addValue(OSG::Color3f(0, 0, 1));
281 colors->addValue(OSG::Color3f(0, 0, 1));
282 colors->addValue(OSG::Color3f(0, 0, 1));
284 axesG->setTypes (type);
285 axesG->setLengths (lens);
286 axesG->setPositions(pnts);
287 axesG->setColors (colors);
288 axesG->setMaterial (OSG::getDefaultUnlitMaterial());
290 return axesG;
293 OSG::NodeTransitPtr makeCoordAxes(void)
295 OSG::NodeTransitPtr axesN = OSG::Node::create();
297 axesN->setCore(makeCoordAxesGeo());
299 return axesN;