changed: gcc8 base update
[opensg.git] / Examples / Simple / occlusionculling.cpp
blob4a6b6aff30ebabaf3111dd9f446a9290e61312a0
1 // OpenSG Tutorial Example: Occlusion Culling
2 //
4 // Three algorithms are implemented "stop an wait", "multi frame"
5 // and "hierarchical multi frame"
7 // --- stop and wait ---
8 // Renders the scene in front to back order. For each object (except for the
9 // front most object) a bounding box is drawn with an occlusion query.
10 // The result is fetched immediately afterwards and if the box was visible
11 // the corresponding object is drawn.
12 // This is quite slow because of the front to back sorted rendering and
13 // the occlusions queries are stalling the graphics pipeline.
15 // --- multi frame ---
16 // Renders the whole scene first (with state sorting) and keeps
17 // the depth buffer. For each object a bounding box is drawn
18 // with an occlusion query. The results are fetched in the next frame,
19 // if the box was visible the corresponding object is drawn.
20 // This is really fast but can lead to render errors on fast camera movements.
22 // --- hierarchical multi frame ---
23 // Similar to multi frame but it tries to reduce the number of
24 // occlusion tests by doing hierarchical occlusion tests.
26 #include <fstream>
28 #ifdef OSG_BUILD_ACTIVE
29 // GLUT is used for window handling
30 #include <OSGGLUT.h>
32 // General OpenSG configuration, needed everywhere
33 #include <OSGConfig.h>
35 // The GLUT-OpenSG connection class
36 #include <OSGGLUTWindow.h>
38 // A little helper to simplify scene management and interaction
39 #include <OSGSimpleSceneManager.h>
41 // Methods to create simple geometry: boxes, spheres, tori etc.
42 #include <OSGSimpleGeometry.h>
44 #include <OSGGradientBackground.h>
46 #include <OSGImageFileHandler.h>
47 #include <OSGPathHandler.h>
49 #include <OSGSceneFileHandler.h>
50 #include <OSGGraphOpSeq.h>
51 #include <OSGVerifyGeoGraphOp.h>
52 #include <OSGStripeGraphOp.h>
53 #include <OSGRenderAction.h>
54 #else
55 // GLUT is used for window handling
56 #include <OpenSG/OSGGLUT.h>
58 // General OpenSG configuration, needed everywhere
59 #include <OpenSG/OSGConfig.h>
61 // The GLUT-OpenSG connection class
62 #include <OpenSG/OSGGLUTWindow.h>
64 // A little helper to simplify scene management and interaction
65 #include <OpenSG/OSGSimpleSceneManager.h>
67 // Methods to create simple geometry: boxes, spheres, tori etc.
68 #include <OpenSG/OSGSimpleGeometry.h>
70 #include <OpenSG/OSGGradientBackground.h>
72 #include <OpenSG/OSGImageFileHandler.h>
73 #include <OpenSG/OSGPathHandler.h>
75 #include <OpenSG/OSGSceneFileHandler.h>
76 #include <OpenSG/OSGGraphOpSeq.h>
77 #include <OpenSG/OSGVerifyGeoGraphOp.h>
78 #include <OpenSG/OSGStripeGraphOp.h>
79 #include <OpenSG/OSGRenderAction.h>
80 #endif
82 OSG::SimpleSceneManagerRefPtr mgr;
83 OSG::NodeRefPtr scene;
85 // Standard GLUT callback functions
86 void display( void )
88 mgr->redraw();
91 void reshape( int w, int h )
93 mgr->resize( w, h );
94 glutPostRedisplay();
97 void
98 motion(int x, int y)
100 mgr->mouseMove( x, y );
101 glutPostRedisplay();
104 void
105 mouse(int button, int state, int x, int y)
107 if ( state )
108 mgr->mouseButtonRelease( button, x, y );
109 else
110 mgr->mouseButtonPress( button, x, y );
111 glutPostRedisplay();
114 void
115 key(unsigned char key, int , int )
117 switch(key)
119 case 27: mgr = NULL;
120 scene = NULL;
121 OSG::osgExit();
122 exit(1);
123 case 'a': mgr->turnHeadlightOn();
124 break;
125 case 's': mgr->turnHeadlightOff();
126 break;
127 case 'l': mgr->useOpenSGLogo();
128 break;
129 case 'f': mgr->setNavigationMode(OSG::Navigator::FLY);
130 break;
131 case 't': mgr->setNavigationMode(OSG::Navigator::TRACKBALL);
132 break;
133 case 'q': mgr->setStatistics(true);
134 break;
135 case 'w': mgr->setStatistics(false);
136 break;
137 case 'o': OSG::SceneFileHandler::the()->write(scene, "out.osb");
138 break;
139 case 'c':
141 OSG::RenderAction *ract = mgr->getRenderAction();
142 ract->setOcclusionCulling(!ract->getOcclusionCulling());
143 printf("Occlusion culling %s.\n", ract->getOcclusionCulling() ? "enabled" : "disabled");
145 break;
147 glutPostRedisplay();
150 // Initialize GLUT & OpenSG and set up the scene
151 int main (int argc, char **argv)
153 // OSG init
154 OSG::osgInit(argc,argv);
156 // GLUT init
157 glutInit(&argc, argv);
158 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
159 int winid = glutCreateWindow("OpenSG");
160 glutReshapeFunc(reshape);
161 glutDisplayFunc(display);
162 glutMouseFunc(mouse);
163 glutMotionFunc(motion);
164 glutKeyboardFunc(key);
166 // open a new scope, because the pointers below should go out of scope
167 // before entering glutMainLoop.
168 // Otherwise OpenSG will complain about objects being alive after shutdown.
170 // the connection between GLUT and OpenSG
171 OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
172 gwin->setGlutId(winid);
173 gwin->init();
175 const char *fileName = (argc > 1) ? argv[1] : NULL;
177 if(fileName != NULL)
178 scene = OSG::SceneFileHandler::the()->read(fileName, NULL);
180 if(scene == NULL)
182 printf("No filename given creating a default scene.\n");
183 // ok we create some spheres and one big torus around them
184 // so we can see the occlusion culling.
186 scene = OSG::makeCoredNode<OSG::Group>();
188 OSG::NodeRefPtr spheres = OSG::makeCoredNode<OSG::Group>();
190 // create spheres
191 for(OSG::Real32 y=-0.5f;y<0.5f;y+=0.1f)
193 for(OSG::Real32 x=-0.5f;x<0.5f;x+=0.1f)
195 OSG::Matrix m;
196 m.setTranslate(x, y, 0.0f);
197 OSG::TransformRefPtr sphere_trans;
198 OSG::NodeRefPtr sphere_trans_node = OSG::makeCoredNode<OSG::Transform>(&sphere_trans);
199 sphere_trans->setMatrix(m);
201 OSG::NodeRefPtr sphere = OSG::makeSphere(3, 0.1f);
202 sphere_trans_node->addChild(sphere);
204 spheres->addChild(sphere_trans_node);
208 // create torus.
209 OSG::NodeRefPtr torus = OSG::makeTorus(0.5f, 2.0f, 32, 32);
211 scene->addChild(spheres);
212 scene->addChild(torus);
215 // create the SimpleSceneManager helper
216 mgr = OSG::SimpleSceneManager::create();
218 mgr->setWindow( gwin );
219 mgr->setRoot( scene );
220 mgr->setStatistics(true);
222 mgr->showAll();
224 // create a gradient background.
225 OSG::GradientBackgroundRefPtr gback = OSG::GradientBackground::create();
226 gback->clearLines();
227 gback->addLine(OSG::Color3f(0.7f, 0.7f, 0.8f), 0);
228 gback->addLine(OSG::Color3f(0.0f, 0.1f, 0.3f), 1);
230 OSG::WindowRefPtr win = mgr->getWindow();
231 for(OSG::UInt32 i = 0; i < win->getMFPort()->size(); ++i)
233 OSG::ViewportRefPtr vp = win->getPort(i);
234 vp->setBackground(gback);
237 OSG::commitChanges();
240 // enable occlusion culling.
241 OSG::RenderAction *ract = mgr->getRenderAction();
242 ract->setOcclusionCulling(true);
244 printf("Occlusion culling enabled.\n");
245 printf("Press 'c' to toggle occlusion culling.\n");
247 // GLUT main loop
248 glutMainLoop();
250 return 0;