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
);
28 bool bGLFinish
= false;
30 // Enum to track which stat method we are using
32 { USE_CUSTOM
, USE_SIMPLE
, USE_NONE
};
34 StatMethod
gStatMethod(USE_CUSTOM
);
45 // react to size changes
46 void reshape(int w
, int h
)
48 glViewport(0, 0, w
, h
);
54 // react to mouse button presses
55 void mouse(int button
, int state
, int x
, int y
)
58 mgr
->mouseButtonRelease(button
, x
, y
);
60 mgr
->mouseButtonPress(button
, x
, y
);
65 // react to mouse motions with pressed buttons
66 void motion(int x
, int y
)
72 void initElements(void)
76 collector
->clearElems();
79 // add optional elements
80 // collector->getElem(Drawable::statNTriangles);
83 void setStatMethod(StatMethod method
)
86 if (gStatMethod
== USE_CUSTOM
)
88 pwin
->getPort(0)->removeObjFromForegrounds(statfg
);
90 else if(gStatMethod
== USE_SIMPLE
)
92 mgr
->setStatistics(false);
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);
109 std::cerr
<< "Setting to no stats.\n";
111 gStatMethod
= method
;
116 void keyboard(unsigned char k
, int, int)
130 // Output help about the controls
131 // - If you add an option, please add it here too.
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"
152 mgr
->getRenderAction()->setVolumeDrawing(
153 !mgr
->getRenderAction()->getVolumeDrawing());
154 std::cerr
<< "Volume Drawing: "
155 << (mgr
->getRenderAction()->getVolumeDrawing() ?
163 if(USE_CUSTOM
== gStatMethod
)
164 { setStatMethod(USE_SIMPLE
); }
165 else if(USE_SIMPLE
== gStatMethod
)
166 { setStatMethod(USE_NONE
); }
168 { setStatMethod(USE_CUSTOM
); }
174 mgr
->setUseTraversalAction(false);
178 mgr
->setUseTraversalAction(true);
188 OSG::SimpleSHLChunk ::getStaticClassId() & 0x000003FF;
190 OSG::TextureBaseChunk::getStaticClassId() & 0x000003FF;
192 OSG::MaterialChunk ::getStaticClassId() & 0x000003FF;
195 OSG::UInt32 uiKeyGen
= (uiSId
) | (uiTId
<< 10) | (uiMId
<< 20);
197 ract
->setKeyGen(uiKeyGen
);
202 bGLFinish
= !bGLFinish
;
203 ract
->setUseGLFinish(bGLFinish
);
204 //act->setUseGLFinish(bGLFinish);
205 std::cerr
<< "Set use gl finish to: " << bGLFinish
<< std::endl
;
211 int main(int argc
, char **argv
)
213 OSG::osgInit(argc
,argv
);
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();
235 OSG::NodeUnrecPtr scene
;
237 if(argc
> 1 && !strcmp(argv
[1],"-s"))
239 gStatMethod
= USE_NONE
;
244 OSG::NodeUnrecPtr file
;
248 scene
= OSG::Node::create();
249 OSG::GroupUnrecPtr g
= OSG::Group::create();
254 for(OSG::UInt16 i
= 1; i
< argc
; ++i
)
256 file
= OSG::SceneFileHandler::the()->read(argv
[i
]);
257 scene
->addChild(file
);
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
278 // add the statistics forground
280 statfg
= OSG::SimpleStatisticsForeground::create();
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
,
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
);