1 // OpenSG Tutorial Example: Hierarchical Transformation
3 // This example demonstrates how transformations accumulate through the graph.
6 #ifdef OSG_BUILD_ACTIVE
10 #include <OSGSimpleGeometry.h>
11 #include <OSGGLUTWindow.h>
12 #include <OSGSimpleSceneManager.h>
13 #include <OSGBaseFunctions.h>
14 #include <OSGTransform.h>
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>
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
[] );
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
60 // Initialize GLUT & OpenSG and set up the scene
61 int main(int argc
, char **argv
)
64 OSG::osgInit(argc
,argv
);
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
);
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
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
107 m
.setTransform(OSG::Vec3f(0, .9f
, 0));
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
);
128 transnode
->addChild(last
);
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
156 // GLUT callback functions
159 // react to size changes
160 void reshape(int w
, int h
)
166 // react to mouse button presses
167 void mouse(int button
, int state
, int x
, int y
)
170 mgr
->mouseButtonRelease(button
, x
, y
);
172 mgr
->mouseButtonPress(button
, x
, y
);
177 // react to mouse motions with pressed buttons
178 void motion(int x
, int y
)
180 mgr
->mouseMove(x
, y
);
185 void keyboard(unsigned char k
, int x
, int y
)
191 // clean up global variables
202 mgr
->setStatistics(!mgr
->getStatistics());
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
);