fixed: compile issue
[opensg.git] / Examples / Simple / 04hiertransform.cpp
blob78fb82f40f8b54a77d0b6fbc556669649629baa6
1 // OpenSG Tutorial Example: Hierarchical Transformation
2 //
3 // This example demonstrates how transformations accumulate through the graph.
4 //
6 #ifdef OSG_BUILD_ACTIVE
7 // Headers
8 #include <OSGGLUT.h>
9 #include <OSGConfig.h>
10 #include <OSGSimpleGeometry.h>
11 #include <OSGGLUTWindow.h>
12 #include <OSGSimpleSceneManager.h>
13 #include <OSGBaseFunctions.h>
14 #include <OSGTransform.h>
15 #include <OSGGroup.h>
16 #else
17 // Headers
18 #include <OpenSG/OSGGLUT.h>
19 #include <OpenSG/OSGConfig.h>
20 #include <OpenSG/OSGSimpleGeometry.h>
21 #include <OpenSG/OSGGLUTWindow.h>
22 #include <OpenSG/OSGSimpleSceneManager.h>
23 #include <OpenSG/OSGBaseFunctions.h>
24 #include <OpenSG/OSGTransform.h>
25 #include <OpenSG/OSGGroup.h>
26 #endif
29 // number of copies to create
30 const OSG::UInt16 ncopies = 10;
32 // just use a single transformation that is shared
33 OSG::TransformRefPtr trans;
36 // The SimpleSceneManager to manage simple applications
37 OSG::SimpleSceneManagerRefPtr mgr;
39 // forward declaration so we can have the interesting stuff upfront
40 int setupGLUT( int *argc, char *argv[] );
42 // redraw the window
43 void display( void )
45 OSG::Matrix m;
46 OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME );
48 m.setTransform(OSG::Vec3f(0, .9f, 0),
49 OSG::Quaternion( OSG::Vec3f(1,1,0),
50 OSG::osgSin(t / 1000.f) / 2.f));
52 // set the transform's matrix
53 trans->setMatrix(m);
55 OSG::commitChanges();
57 mgr->redraw();
60 // Initialize GLUT & OpenSG and set up the scene
61 int main(int argc, char **argv)
63 // OSG init
64 OSG::osgInit(argc,argv);
66 // GLUT init
67 int winid = setupGLUT(&argc, argv);
69 // open a new scope, because the pointers below should go out of scope
70 // before entering glutMainLoop.
71 // Otherwise OpenSG will complain about objects being alive after shutdown.
73 // the connection between GLUT and OpenSG
74 OSG::GLUTWindowRefPtr gwin= OSG::GLUTWindow::create();
75 gwin->setGlutId(winid);
76 gwin->init();
78 // create the scene
81 Transformation accumulate through the graph, i.e. all nodes below
82 a Transformation are influenced by it, even other Transformations.
84 This can be used to create models of objects that move together and
85 in relation to each other, the prime examples being a robot arm and
86 a planetary system. This example does something not quite unlike a
87 robot arm.
88 */
90 // create the scene
93 This time the graph is not wide, but deep, i.e. every Transformation
94 only has two children, a Geometry and another transformation.
95 The end resulting motion of the geometry is the accumulation of
96 all the Transformations above it.
99 // use a cylinder this time
100 OSG::GeometryRefPtr cyl = OSG::makeCylinderGeo( 1, .3f, 8, true, true, true );
102 // the single transformation Core used
103 trans = OSG::Transform::create();
105 // setup an intial transformation
106 OSG::Matrix m;
107 m.setTransform(OSG::Vec3f(0, .9f, 0));
109 trans->setMatrix(m);
111 OSG::NodeRefPtr last = NULL;
113 // create the copied transformations and their geometry nodes
114 for(OSG::UInt16 i = 1; i < ncopies; ++i)
116 // create the shared Geometry
117 OSG::NodeRefPtr geonode = OSG::Node::create();
118 geonode->setCore(cyl);
120 // add a transformation to the Geometry
121 OSG::NodeRefPtr transnode = OSG::Node::create();
123 transnode->setCore (trans );
124 transnode->addChild(geonode);
126 if(last != NULL)
128 transnode->addChild(last);
131 last = transnode;
134 OSG::NodeRefPtr scene = last;
136 OSG::commitChanges();
138 // create the SimpleSceneManager helper
139 mgr = OSG::SimpleSceneManager::create();
141 // tell the manager what to manage
142 mgr->setWindow(gwin );
143 mgr->setRoot (scene);
145 // show the whole scene
146 mgr->showAll();
149 // GLUT main loop
150 glutMainLoop();
152 return 0;
156 // GLUT callback functions
159 // react to size changes
160 void reshape(int w, int h)
162 mgr->resize(w, h);
163 glutPostRedisplay();
166 // react to mouse button presses
167 void mouse(int button, int state, int x, int y)
169 if (state)
170 mgr->mouseButtonRelease(button, x, y);
171 else
172 mgr->mouseButtonPress(button, x, y);
174 glutPostRedisplay();
177 // react to mouse motions with pressed buttons
178 void motion(int x, int y)
180 mgr->mouseMove(x, y);
181 glutPostRedisplay();
184 // react to keys
185 void keyboard(unsigned char k, int x, int y)
187 switch(k)
189 case 27:
191 // clean up global variables
192 trans = NULL;
193 mgr = NULL;
195 OSG::osgExit();
196 exit(0);
198 break;
200 case 's':
202 mgr->setStatistics(!mgr->getStatistics());
204 break;
208 // setup the GLUT library which handles the windows for us
209 int setupGLUT(int *argc, char *argv[])
211 glutInit(argc, argv);
212 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
214 int winid = glutCreateWindow("OpenSG");
216 glutReshapeFunc(reshape);
217 glutDisplayFunc(display);
218 glutMouseFunc(mouse);
219 glutMotionFunc(motion);
220 glutKeyboardFunc(keyboard);
222 // call the redraw function whenever there's nothing else to do
223 glutIdleFunc(display);
225 return winid;