1 // OpenSG example: callback.cpp
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.
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
17 // This examples renders a torus or if no model file is provided at the command
21 // a) mouse => standard navigator
31 #ifdef OSG_BUILD_ACTIVE
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>
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>
60 using namespace OSG
; // just for convenience but not recommended
63 // function forward declarations
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
;
94 static void cleanup(void)
105 static void display(void)
111 static void reshape(int w
, int h
)
117 static void mouse(int button
, int state
, int x
, int y
)
120 mgr
->mouseButtonRelease(button
, x
, y
);
122 mgr
->mouseButtonPress(button
, x
, y
);
127 static void motion(int x
, int y
)
129 mgr
->mouseMove(x
, y
);
133 static void keyboard(unsigned char k
, int, int)
140 std::exit(EXIT_SUCCESS
);
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
, "");
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
);
184 glMatrixMode(GL_MODELVIEW
);
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
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
);
246 glTranslatef(1.5f
,0.0f
,-7.0f
);
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
);
291 glMatrixMode(GL_PROJECTION
);
294 glMatrixMode(GL_MODELVIEW
);
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
);
322 static int doMain(int argc
, char *argv
[])
324 preloadSharedObject("OSGFileIO");
325 preloadSharedObject("OSGImageFileIO");
326 preloadSharedObject("OSGContribPLY");
330 int winid
= setupGLUT(&argc
, argv
);
332 win
= GLUTWindow::create();
333 win
->setGlutId(winid
);
336 root
= Node::create();
337 root
->setCore(Group::create());
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();
351 FWARNING(("%s\n", *it
));
354 scene
= makeTorus(.5, 2, 16, 16);
358 scene
= SceneFileHandler::the()->read(argv
[1]);
361 root
->addChild(scene
);
367 mgr
= SimpleSceneManager::create();
380 int main(int argc
, char *argv
[])
382 int ret
= doMain(argc
, argv
);