fixed: push transparency enforcement to state so the backend is aware of it
[opensg.git] / Examples / Simple / 02move.cpp
blobc4aa8239d58c3ffa42b842cfa3dd904ff2b0e390
1 // OpenSG Tutorial Example: Move An Object
2 //
3 // This example shows how to use Transform nodes to move objects aroung
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>
14 // new headers:
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>
22 #else
23 // Headers
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>
30 // new headers:
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>
38 #endif
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[] );
49 // redraw the window
50 void display( void )
52 // create the matrix
53 OSG::Matrix m;
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),
60 t / 1000.f));
62 // set the transform's matrix
64 trans->setMatrix(m);
66 OSG::commitChanges();
68 mgr->redraw();
71 // Initialize GLUT & OpenSG and set up the scene
72 int main(int argc, char **argv)
74 // OSG init
75 OSG::osgInit(argc,argv);
77 // GLUT init
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);
87 gwin->init();
89 // create the scene
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
96 // 1. create the Node
97 OSG::NodeRefPtr scene = OSG::Node::create();
99 // 2. create the core
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
118 mgr->showAll();
121 // GLUT main loop
122 glutMainLoop();
124 return 0;
128 // GLUT callback functions
131 // react to size changes
132 void reshape(int w, int h)
134 mgr->resize(w, h);
135 glutPostRedisplay();
138 // react to mouse button presses
139 void mouse(int button, int state, int x, int y)
141 if (state)
142 mgr->mouseButtonRelease(button, x, y);
143 else
144 mgr->mouseButtonPress(button, x, y);
146 glutPostRedisplay();
149 // react to mouse motions with pressed buttons
150 void motion(int x, int y)
152 mgr->mouseMove(x, y);
153 glutPostRedisplay();
156 // react to keys
157 void keyboard(unsigned char k, int x, int y)
159 switch(k)
161 case 27:
163 // clean up global variables
164 trans = NULL;
165 mgr = NULL;
167 OSG::osgExit();
168 exit(0);
170 break;
172 case 's':
174 mgr->setStatistics(!mgr->getStatistics());
176 break;
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);
197 return winid;