1 // OpenSG Tutorial Example: Move An Object
3 // This example shows how to use Transform nodes to move objects aroung
6 #ifdef OSG_BUILD_ACTIVE
10 #include <OSGSimpleGeometry.h>
11 #include <OSGGLUTWindow.h>
12 #include <OSGSimpleSceneManager.h>
16 // some wrappers for standard functions for platform independence, e.g.
17 // osgSin, osgCos, osgTan
18 #include <OSGBaseFunctions.h>
20 // the transformation node core
21 #include <OSGTransform.h>
24 #include <OpenSG/OSGGLUT.h>
25 #include <OpenSG/OSGConfig.h>
26 #include <OpenSG/OSGSimpleGeometry.h>
27 #include <OpenSG/OSGGLUTWindow.h>
28 #include <OpenSG/OSGSimpleSceneManager.h>
32 // some wrappers for standard functions for platform independence, e.g.
33 // osgSin, osgCos, osgTan
34 #include <OpenSG/OSGBaseFunctions.h>
36 // the transformation node core
37 #include <OpenSG/OSGTransform.h>
40 // The pointer to the transformation
41 OSG::TransformRefPtr trans
;
43 // The SimpleSceneManager to manage simple applications
44 OSG::SimpleSceneManagerRefPtr mgr
;
46 // forward declaration so we can have the interesting stuff upfront
47 int setupGLUT( int *argc
, char *argv
[] );
54 OSG::Real32 t
= glutGet(GLUT_ELAPSED_TIME
);
56 m
.setTransform(OSG::Vec3f( OSG::osgSin(t
/ 1000.f
),
57 OSG::osgCos(t
/ 1000.f
),
58 OSG::osgSin(t
/ 1000.f
)),
59 OSG::Quaternion( OSG::Vec3f(0,1,0),
62 // set the transform's matrix
71 // Initialize GLUT & OpenSG and set up the scene
72 int main(int argc
, char **argv
)
75 OSG::osgInit(argc
,argv
);
78 int winid
= setupGLUT(&argc
, argv
);
80 // open a new scope, because the pointers gwin, torus and scene below should
81 // go out of scope before entering glutMainLoop.
82 // Otherwise OpenSG will complain about objects being alive after shutdown.
84 // the connection between GLUT and OpenSG
85 OSG::GLUTWindowRefPtr gwin
= OSG::GLUTWindow::create();
86 gwin
->setGlutId(winid
);
91 OSG::NodeRefPtr torus
= OSG::makeTorus( .5, 2, 16, 32 );
93 // create the transformation node
94 // scenegraph nodes are split into 2 parts: the node and its core
97 OSG::NodeRefPtr scene
= OSG::Node::create();
100 trans
= OSG::Transform::create();
102 // 3. associate the core with the node
103 scene
->setCore(trans
);
105 // add the torus as a child
106 scene
->addChild(torus
);
108 OSG::commitChanges();
110 // create the SimpleSceneManager helper
111 mgr
= OSG::SimpleSceneManager::create();
113 // tell the manager what to manage
114 mgr
->setWindow(gwin
);
115 mgr
->setRoot (scene
);
117 // show the whole scene
128 // GLUT callback functions
131 // react to size changes
132 void reshape(int w
, int h
)
138 // react to mouse button presses
139 void mouse(int button
, int state
, int x
, int y
)
142 mgr
->mouseButtonRelease(button
, x
, y
);
144 mgr
->mouseButtonPress(button
, x
, y
);
149 // react to mouse motions with pressed buttons
150 void motion(int x
, int y
)
152 mgr
->mouseMove(x
, y
);
157 void keyboard(unsigned char k
, int x
, int y
)
163 // clean up global variables
174 mgr
->setStatistics(!mgr
->getStatistics());
180 // setup the GLUT library which handles the windows for us
181 int setupGLUT(int *argc
, char *argv
[])
183 glutInit(argc
, argv
);
184 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
186 int winid
= glutCreateWindow("OpenSG");
188 glutReshapeFunc(reshape
);
189 glutDisplayFunc(display
);
190 glutMouseFunc(mouse
);
191 glutMotionFunc(motion
);
192 glutKeyboardFunc(keyboard
);
194 // call the redraw function whenever there's nothing else to do
195 glutIdleFunc(display
);