changed: gcc8 base update
[opensg.git] / Source / System / Statistics / Foregrounds / testStatisticsRender.cpp
blobb734988690e8d59c86d124a0c444e9bfe9395119
2 #include "OSGGLUT.h"
3 #include "OSGConfig.h"
4 #include "OSGSimpleGeometry.h"
5 #include "OSGPassiveWindow.h"
6 #include "OSGSimpleSceneManager.h"
7 #include "OSGSceneFileHandler.h"
9 #include "OSGDrawable.h"
10 #include "OSGSimpleStatisticsForeground.h"
11 #include "OSGStatElemTypes.h"
12 #include "OSGStatCollector.h"
13 #include "OSGRenderAction.h"
14 #include "OSGTextureObjChunk.h"
15 #include "OSGMaterialChunk.h"
16 #include "OSGSimpleSHLChunk.h"
19 OSG::SimpleSceneManagerRefPtr mgr = NULL;
20 OSG::RenderActionRefPtr ract = NULL;
22 OSG::PassiveWindowRecPtr pwin;
23 OSG::SimpleStatisticsForegroundRecPtr statfg;
25 OSG::StatCollector *collector(NULL);
27 bool show = true;
28 bool bGLFinish = false;
30 // Enum to track which stat method we are using
31 enum StatMethod
32 { USE_CUSTOM, USE_SIMPLE, USE_NONE };
34 StatMethod gStatMethod(USE_CUSTOM);
36 // redraw the window
37 void display(void)
39 mgr->redraw();
41 // all done, swap
42 glutSwapBuffers();
45 // react to size changes
46 void reshape(int w, int h)
48 glViewport(0, 0, w, h);
50 mgr->resize(w,h);
51 glutPostRedisplay();
54 // react to mouse button presses
55 void mouse(int button, int state, int x, int y)
57 if (state)
58 mgr->mouseButtonRelease(button, x, y);
59 else
60 mgr->mouseButtonPress(button, x, y);
62 glutPostRedisplay();
65 // react to mouse motions with pressed buttons
66 void motion(int x, int y)
68 mgr->mouseMove(x, y);
69 glutPostRedisplay();
72 void initElements(void)
74 if(collector != NULL)
76 collector->clearElems();
79 // add optional elements
80 // collector->getElem(Drawable::statNTriangles);
83 void setStatMethod(StatMethod method)
85 // Disable old method
86 if (gStatMethod == USE_CUSTOM)
88 pwin->getPort(0)->removeObjFromForegrounds(statfg);
90 else if(gStatMethod == USE_SIMPLE)
92 mgr->setStatistics(false);
95 // Enable new method
96 if(method == USE_CUSTOM)
98 std::cerr << "Setting to custom stats.\n";
99 pwin->getPort(0)->addForeground(statfg);
100 ract->setStatCollector(collector);
102 else if(method == USE_SIMPLE)
104 std::cerr << "Setting to ssm stats.\n";
105 mgr->setStatistics(true);
107 else
109 std::cerr << "Setting to no stats.\n";
111 gStatMethod = method;
115 // react to keys
116 void keyboard(unsigned char k, int, int)
118 switch(k)
120 case 27:
122 mgr = NULL;
123 pwin = NULL;
124 statfg = NULL;
126 OSG::osgExit();
127 exit(0);
130 // Output help about the controls
131 // - If you add an option, please add it here too.
132 case '?':
133 case '/':
134 case 'h':
136 std::cerr << "\nControls:"
137 << "v: Toggle drawing of volumes.\n"
138 << "z: Toggle zwrite on rendering action.\n"
139 << "r: switch to render action.\n"
140 << "t: switch to traversal action.\n"
141 << "n: toggle state sorting on action.\n"
142 << "m: set keygen to 0.\n"
143 << "s: set keygen for shaders.\n"
144 << "g: toggle using gl finish.\n"
145 << "x: toggle stat mode.\n"
146 << std::endl;
148 break;
150 case 'v':
152 mgr->getRenderAction()->setVolumeDrawing(
153 !mgr->getRenderAction()->getVolumeDrawing());
154 std::cerr << "Volume Drawing: "
155 << (mgr->getRenderAction()->getVolumeDrawing() ?
156 "on":"off")
157 << std::endl;
159 break;
161 case 'x':
163 if(USE_CUSTOM == gStatMethod)
164 { setStatMethod(USE_SIMPLE); }
165 else if(USE_SIMPLE == gStatMethod)
166 { setStatMethod(USE_NONE); }
167 else
168 { setStatMethod(USE_CUSTOM); }
170 break;
172 case 'r':
173 initElements();
174 mgr->setUseTraversalAction(false);
175 break;
176 case 't':
177 initElements();
178 mgr->setUseTraversalAction(true);
179 break;
181 case 'm':
182 ract->setKeyGen(0);
183 break;
185 case 's':
187 OSG::UInt32 uiSId =
188 OSG::SimpleSHLChunk ::getStaticClassId() & 0x000003FF;
189 OSG::UInt32 uiTId =
190 OSG::TextureBaseChunk::getStaticClassId() & 0x000003FF;
191 OSG::UInt32 uiMId =
192 OSG::MaterialChunk ::getStaticClassId() & 0x000003FF;
195 OSG::UInt32 uiKeyGen = (uiSId) | (uiTId << 10) | (uiMId << 20);
197 ract->setKeyGen(uiKeyGen);
199 break;
201 case 'g':
202 bGLFinish = !bGLFinish;
203 ract->setUseGLFinish(bGLFinish);
204 //act->setUseGLFinish(bGLFinish);
205 std::cerr << "Set use gl finish to: " << bGLFinish << std::endl;
206 break;
211 int main(int argc, char **argv)
213 OSG::osgInit(argc,argv);
215 // GLUT init
216 glutInit(&argc, argv);
218 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
220 glutInitWindowSize(500, 500);
221 glutCreateWindow("OpenSG");
223 glutReshapeFunc(reshape);
224 glutDisplayFunc(display);
225 glutIdleFunc(display);
226 glutMouseFunc(mouse);
227 glutMotionFunc(motion);
228 glutKeyboardFunc(keyboard);
230 pwin = OSG::PassiveWindow::create();
231 pwin->init();
234 // create the scene
235 OSG::NodeUnrecPtr scene;
237 if(argc > 1 && !strcmp(argv[1],"-s"))
239 gStatMethod = USE_NONE;
240 argv++;
241 argc--;
244 OSG::NodeUnrecPtr file;
246 if(argc > 1)
248 scene = OSG::Node::create();
249 OSG::GroupUnrecPtr g = OSG::Group::create();
251 scene->setCore(g);
254 for(OSG::UInt16 i = 1; i < argc; ++i)
256 file = OSG::SceneFileHandler::the()->read(argv[i]);
257 scene->addChild(file);
260 else
262 scene = OSG::makeTorus(.5, 3, 16, 16);
265 // create the SimpleSceneManager helper
266 mgr = OSG::SimpleSceneManager::create();
268 // create the window and initial camera/viewport
269 mgr->setWindow(pwin );
270 // tell the manager what to manage
271 mgr->setRoot (scene);
273 OSG::Thread::getCurrentChangeList()->commitChanges();
275 // show the whole scene
276 mgr->showAll();
278 // add the statistics forground
280 statfg = OSG::SimpleStatisticsForeground::create();
282 statfg->setSize(25);
283 statfg->setColor(OSG::Color4f(0,1,0,0.7f));
285 statfg->addElement(OSG::RenderAction::statDrawTime, "Draw FPS: %r.3f");
286 statfg->addElement(OSG::RenderAction::statDrawTime, "DrawTime: %.3f s");
287 statfg->addElement(OSG::RenderAction::statTravTime, "TravTime: %.3f s");
289 statfg->addElement(OSG::RenderPartition::statCullTestedNodes,
290 "%d nodes cull tested");
291 statfg->addElement(OSG::RenderPartition::statCulledNodes,
292 "%d nodes culled");
294 statfg->addElement(OSG::RenderAction::statNMatrices, "%d matrix changes");
295 statfg->addElement(OSG::RenderAction::statNStates, "%d state changes");
296 statfg->addElement(OSG::RenderAction::statNChunks, "%d state chunk changes");
297 statfg->addElement(OSG::RenderAction::statNShaders, "%d shaders");
298 statfg->addElement(OSG::RenderAction::statNShaders, "%d shader parameters");
300 statfg->addElement(OSG::Drawable::statNVertices, "%d vertices drawn");
301 statfg->addElement(OSG::Drawable::statNPrimitives, "%d primitives drawn");
302 statfg->addElement(OSG::Drawable::statNTriangles, "%d triangles drawn");
303 statfg->addElement(OSG::Drawable::statNLines, "%d lines drawn");
304 statfg->addElement(OSG::Drawable::statNPoints, "%d points drawn");
306 statfg->addElement(OSG::TextureObjChunk::statNTextures, "%d textures");
307 statfg->addElement(OSG::TextureObjChunk::statNTexBytes, "%d bytes of textures");
309 collector = statfg->getCollector();
311 ract = OSG::RenderAction::create();
312 ract->setStatCollector(collector);
314 mgr->setAction(ract);
316 setStatMethod(gStatMethod);
319 // GLUT main loop
320 glutMainLoop();
322 return 0;