fixed: compile issue
[opensg.git] / Examples / Simple / openglcallback.cpp
blob59f5eced67a7d2ca908a7fcc7aa8039080a82a6a
1 // OpenSG example: callback.cpp
2 //
3 // This example shows how to establish a callback which is executed once on
4 // each render traversal. This can be useful for applications which have to
5 // render native OpenGL code, for various reasons.
6 //
7 // To be absolutely clear, this callback technique for native OpenGL rendering
8 // is not applicable in a cluster setup and it should only be exceptionally
9 // used at special circumstances.
11 // Especially demonstrated is the use of the following classes:
12 // - CallbackAlgorithm
13 // - VisitSubTree,
14 // - AlgorithmStage
15 // - DrawEnv
17 // This examples renders a torus or if no model file is provided at the command
18 // line.
20 // User interface:
21 // a) mouse => standard navigator
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string>
27 #include <sstream>
28 #include <fstream>
29 #include <vector>
31 #ifdef OSG_BUILD_ACTIVE
32 // Headers
33 #include <OSGGLUT.h>
34 #include <OSGConfig.h>
35 #include <OSGSimpleGeometry.h>
36 #include <OSGGLUTWindow.h>
37 #include <OSGSimpleSceneManager.h>
38 #include <OSGSceneFileHandler.h>
39 #include <OSGAction.h>
40 #include <OSGCallbackAlgorithm.h>
41 #include <OSGAlgorithmStage.h>
42 #include <OSGVisitSubTree.h>
43 #include <OSGDrawEnv.h>
44 #else
45 // Headers
46 #include <OpenSG/OSGGLUT.h>
47 #include <OpenSG/OSGConfig.h>
48 #include <OpenSG/OSGSimpleGeometry.h>
49 #include <OpenSG/OSGGLUTWindow.h>
50 #include <OpenSG/OSGSimpleSceneManager.h>
51 #include <OpenSG/OSGSceneFileHandler.h>
52 #include <OpenSG/OSGAction.h>
53 #include <OpenSG/OSGCallbackAlgorithm.h>
54 #include <OpenSG/OSGAlgorithmStage.h>
55 #include <OpenSG/OSGSimpleStage.h>
56 #include <OpenSG/OSGVisitSubTree.h>
57 #include <OpenSG/OSGDrawEnv.h>
58 #endif
60 using namespace OSG; // just for convenience but not recommended
63 // function forward declarations
65 static void onExit();
66 static void cleanup(void);
67 static void display(void);
68 static void reshape(int w, int h);
69 static void mouse(int button, int state, int x, int y);
70 static void motion(int x, int y);
71 static void keyboard(unsigned char k, int, int);
72 static int setupGLUT(int *argc, char *argv[]);
73 static int doMain(int argc, char *argv[]);
74 static void addStage();
75 static void theCallback(DrawEnv* env);
78 // global state of example
80 SimpleSceneManagerRefPtr mgr;
81 NodeRefPtr root;
82 NodeRefPtr scene;
83 GLUTWindowRefPtr win;
86 static void onExit()
88 commitChanges();
90 cleanup();
91 osgExit();
94 static void cleanup(void)
96 mgr->setWindow(NULL);
97 mgr->setRoot(NULL);
99 mgr = NULL;
100 root = NULL;
101 scene = NULL;
102 win = NULL;
105 static void display(void)
107 commitChanges();
108 mgr->redraw();
111 static void reshape(int w, int h)
113 mgr->resize(w,h);
114 glutPostRedisplay();
117 static void mouse(int button, int state, int x, int y)
119 if (state)
120 mgr->mouseButtonRelease(button, x, y);
121 else
122 mgr->mouseButtonPress(button, x, y);
124 glutPostRedisplay();
127 static void motion(int x, int y)
129 mgr->mouseMove(x, y);
130 glutPostRedisplay();
133 static void keyboard(unsigned char k, int, int)
135 switch(k)
137 case 27:
138 case 'q':
140 std::exit(EXIT_SUCCESS);
142 break;
146 static void addStage()
149 // We would like to render the scene but won't detach it from its parent.
150 // The VisitSubTree allows just that.
152 VisitSubTreeUnrecPtr visitor = VisitSubTree::create();
153 visitor->setSubTreeRoot(scene);
154 NodeUnrecPtr visit_node = makeNodeFor(visitor);
157 // Algorithm object: CallbackAlgorithm is kind of Algorithm
159 CallbackAlgorithmUnrecPtr algo = CallbackAlgorithm::create();
160 algo->setCallback(theCallback, "");
162 // Stage object
164 AlgorithmStageUnrecPtr stage = AlgorithmStage::create();
165 stage->setAlgorithm(algo);
168 // Give the stage core a place to live
170 NodeUnrecPtr stage_node = makeNodeFor(stage);
171 stage_node->addChild(visit_node);
173 root->addChild(stage_node);
176 static void theCallback(DrawEnv* env)
179 // Save the current state
181 glMatrixMode(GL_PROJECTION);
182 glPushMatrix();
184 glMatrixMode(GL_MODELVIEW);
185 glPushMatrix();
187 glPushAttrib(GL_ALL_ATTRIB_BITS);
190 // Setup current viewport
192 glMatrixMode(GL_PROJECTION);
193 glLoadMatrixf(env->getVPCameraFullProjection().getValues());
195 glMatrixMode(GL_MODELVIEW);
196 glLoadMatrixf(env->getVPCameraViewing().getValues());
199 // State initialization
201 glShadeModel(GL_SMOOTH);
203 glEnable(GL_DEPTH_TEST);
204 glDepthFunc(GL_LEQUAL);
206 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
209 // Some model geometry
211 glPushMatrix();
212 glTranslatef(-1.5f,0.0f,-6.0f);
213 glBegin(GL_TRIANGLES);
214 glColor3f(1.0f,0.0f,0.0f);
215 glVertex3f( 0.0f, 1.0f, 0.0f);
216 glColor3f(0.0f,1.0f,0.0f);
217 glVertex3f(-1.0f,-1.0f, 1.0f);
218 glColor3f(0.0f,0.0f,1.0f);
219 glVertex3f( 1.0f,-1.0f, 1.0f);
221 glColor3f(1.0f,0.0f,0.0f);
222 glVertex3f( 0.0f, 1.0f, 0.0f);
223 glColor3f(0.0f,0.0f,1.0f);
224 glVertex3f( 1.0f,-1.0f, 1.0f);
225 glColor3f(0.0f,1.0f,0.0f);
226 glVertex3f( 1.0f,-1.0f, -1.0f);
228 glColor3f(1.0f,0.0f,0.0f);
229 glVertex3f( 0.0f, 1.0f, 0.0f);
230 glColor3f(0.0f,1.0f,0.0f);
231 glVertex3f( 1.0f,-1.0f, -1.0f);
232 glColor3f(0.0f,0.0f,1.0f);
233 glVertex3f(-1.0f,-1.0f, -1.0f);
235 glColor3f(1.0f,0.0f,0.0f);
236 glVertex3f( 0.0f, 1.0f, 0.0f);
237 glColor3f(0.0f,0.0f,1.0f);
238 glVertex3f(-1.0f,-1.0f,-1.0f);
239 glColor3f(0.0f,1.0f,0.0f);
240 glVertex3f(-1.0f,-1.0f, 1.0f);
241 glEnd();
242 glPopMatrix();
245 glPushMatrix();
246 glTranslatef(1.5f,0.0f,-7.0f);
247 glBegin(GL_QUADS);
248 glColor3f(0.0f,1.0f,0.0f);
249 glVertex3f( 1.0f, 1.0f,-1.0f);
250 glVertex3f(-1.0f, 1.0f,-1.0f);
251 glVertex3f(-1.0f, 1.0f, 1.0f);
252 glVertex3f( 1.0f, 1.0f, 1.0f);
254 glColor3f(1.0f,0.5f,0.0f);
255 glVertex3f( 1.0f,-1.0f, 1.0f);
256 glVertex3f(-1.0f,-1.0f, 1.0f);
257 glVertex3f(-1.0f,-1.0f,-1.0f);
258 glVertex3f( 1.0f,-1.0f,-1.0f);
260 glColor3f(1.0f,0.0f,0.0f);
261 glVertex3f( 1.0f, 1.0f, 1.0f);
262 glVertex3f(-1.0f, 1.0f, 1.0f);
263 glVertex3f(-1.0f,-1.0f, 1.0f);
264 glVertex3f( 1.0f,-1.0f, 1.0f);
266 glColor3f(1.0f,1.0f,0.0f);
267 glVertex3f( 1.0f,-1.0f,-1.0f);
268 glVertex3f(-1.0f,-1.0f,-1.0f);
269 glVertex3f(-1.0f, 1.0f,-1.0f);
270 glVertex3f( 1.0f, 1.0f,-1.0f);
272 glColor3f(0.0f,0.0f,1.0f);
273 glVertex3f(-1.0f, 1.0f, 1.0f);
274 glVertex3f(-1.0f, 1.0f,-1.0f);
275 glVertex3f(-1.0f,-1.0f,-1.0f);
276 glVertex3f(-1.0f,-1.0f, 1.0f);
278 glColor3f(1.0f,0.0f,1.0f);
279 glVertex3f( 1.0f, 1.0f,-1.0f);
280 glVertex3f( 1.0f, 1.0f, 1.0f);
281 glVertex3f( 1.0f,-1.0f, 1.0f);
282 glVertex3f( 1.0f,-1.0f,-1.0f);
283 glEnd();
284 glPopMatrix();
287 // Restore the state
289 glPopAttrib();
291 glMatrixMode(GL_PROJECTION);
292 glPopMatrix();
294 glMatrixMode(GL_MODELVIEW);
295 glPopMatrix();
299 // initialize GLUT
301 static int setupGLUT(int *argc, char *argv[])
303 glutInit(argc, argv);
305 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL | GLUT_DOUBLE);
307 int winid = glutCreateWindow("OpenSG");
309 glutReshapeFunc(reshape);
310 glutDisplayFunc(display);
311 glutIdleFunc(display);
312 glutMouseFunc(mouse);
313 glutMotionFunc(motion);
314 glutKeyboardFunc(keyboard);
316 return winid;
320 // setup scene
322 static int doMain(int argc, char *argv[])
324 preloadSharedObject("OSGFileIO");
325 preloadSharedObject("OSGImageFileIO");
326 preloadSharedObject("OSGContribPLY");
328 osgInit(argc,argv);
330 int winid = setupGLUT(&argc, argv);
332 win = GLUTWindow::create();
333 win->setGlutId(winid);
334 win->init();
336 root = Node::create();
337 root->setCore(Group::create());
339 if(argc < 2)
341 FWARNING(("No file given!\n"));
342 FWARNING(("Supported file formats:\n"));
344 std::list<const char*> suffixes;
345 SceneFileHandler::the()->getSuffixList(suffixes);
347 for(std::list<const char*>::iterator it = suffixes.begin();
348 it != suffixes.end();
349 ++it)
351 FWARNING(("%s\n", *it));
354 scene = makeTorus(.5, 2, 16, 16);
356 else
358 scene = SceneFileHandler::the()->read(argv[1]);
361 root->addChild(scene);
363 addStage();
365 commitChanges();
367 mgr = SimpleSceneManager::create();
369 mgr->setWindow(win);
370 mgr->setRoot (root);
372 mgr->showAll();
374 return 0;
378 // main entry point
380 int main(int argc, char *argv[])
382 int ret = doMain(argc, argv);
384 atexit(onExit);
386 glutMainLoop();
388 cleanup();
390 osgExit();
392 return ret;