// react to keys void keyboard(unsigned char k, int , int ) { switch(k) { case 27: onExit(); OSG::osgExit(); exit(0); break; case 'f': g_mgr->setNavigationMode(OSG::Navigator::FLY); break; case 't': g_mgr->setNavigationMode(OSG::Navigator::TRACKBALL); break; case 'q': g_mgr->setStatistics(true); break; case 'w': g_mgr->setStatistics(false); break; case 'r': { bool useTrav = !g_mgr->getUseTraversalAction(); g_mgr->setUseTraversalAction(useTrav); printf("Using %s action.\n", useTrav ? "render traversal" : "render"); } break; } }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG g_win= OSG::GLUTWindow::create(); g_win->setGlutId(winid); g_win->init(); g_scene = createScene(); // create the SimpleSceneManager helper g_mgr = OSG::SimpleSceneManager::create(); g_mgr->setUseTraversalAction(true); // tell the manager what to manage g_mgr->setWindow(g_win ); g_mgr->setRoot (g_scene); // add LabelForeground!!! g_labelForeground = OSG::LabelForeground::create(); g_mgr->addForeground(g_labelForeground); // show the whole scene g_mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }
int main(int argc, char **argv) { OSG::osgInit(argc,argv); if(argc > 1 && !strcmp(argv[1],"-s")) { show = false; argv++; argc--; } if(argc > 1 && !strcmp(argv[1],"-d")) { debug = true; argv++; argc--; } if(argc > 1) { scene = OSG::Node::create(); OSG::GroupUnrecPtr g = OSG::Group::create(); scene->setCore(g); for(OSG::UInt16 i = 1; i < argc; ++i) scene->addChild(OSG::SceneFileHandler::the()->read(argv[i])); } else { scene = OSG::makeTorus(.5, 3, 16, 16); } // GLUT init glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(1024, 768); mainwinid = glutCreateWindow("OpenSG"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); OSG::GLUTWindowUnrecPtr mainwin=OSG::GLUTWindow::create(); mainwin->setGlutId(mainwinid); mainwin->init(); // create the SimpleSceneManager helper mgr = OSG::SimpleSceneManager::create(); // create the window and initial camera/viewport mgr->setWindow(mainwin); // tell the manager what to manage mgr->setRoot (scene); OSG::commitChanges(); // show the whole scene mgr->showAll(); mgr->setUseTraversalAction(true); tact = OSG::RenderAction::create(); #ifdef OSG_OLD_RENDER_ACTION act = OSG::RenderAction::create(); #endif debugact = OSG::RenderAction::create(); tact->setOcclusionCulling(true); // Open the debug window if(debug) { OSG::traverse(scene, initMask); glutInitWindowSize(800, 400); debugwinid = glutCreateWindow("OpenSG Occlusion Debugging"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(display); glutKeyboardFunc(keyboard); debugwin=OSG::GLUTWindow::create(); debugwin->setGlutId(debugwinid); debugwin->init(); OSG::ViewportUnrecPtr vp = mainwin->getPort(0); OSG::ViewportUnrecPtr newvp = OSG::Viewport::create(); newvp->setLeft(0); newvp->setRight(0.5); newvp->setBottom(0); newvp->setTop(1); newvp->setRoot(vp->getRoot()); newvp->setCamera(vp->getCamera()); newvp->setBackground(vp->getBackground()); newvp->setTravMask(0x1); debugwin->addPort(newvp); newvp = OSG::Viewport::create(); newvp->setLeft(0.5); newvp->setRight(1); newvp->setBottom(0); newvp->setTop(1); newvp->setRoot(vp->getRoot()); newvp->setCamera(vp->getCamera()); newvp->setBackground(vp->getBackground()); newvp->setTravMask(0x2); debugwin->addPort(newvp); tact->setOcclusionCullingDebug(true); tact->setOcclusionDebugMasks(0x1, 0x2, 0x4); } // add the statistics forground statfg = OSG::SimpleStatisticsForeground::create(); statfg->setSize(25); statfg->setColor(OSG::Color4f(0,1,0,0.7f)); statfg->addElement(OSG::RenderAction::statDrawTime, "Draw FPS: %r.3f"); statfg->addElement(OSG::RenderAction::statNMatrices, "Matrix Changes: %d"); statfg->addElement(OSG::RenderAction::statNStates, "State Changes: %d"); statfg->addElement(OSG::RenderPartition::statCullTestedNodes, "Cull-tested Nodes: %d"); statfg->addElement(OSG::RenderPartition::statCulledNodes, "Culled Nodes: %d"); statfg->addElement(OSG::OcclusionCullingTreeBuilder::statNOccNodes, "Nodes in DrawTree: %d"); statfg->addElement(OSG::OcclusionCullingTreeBuilder::statNOccTests, "Occ Tests: %d"); statfg->addElement(OSG::OcclusionCullingTreeBuilder::statNOccInvisible, "Invisible Nodes: %d"); statfg->addElement(OSG::OcclusionCullingTreeBuilder::statNOccSuccessTestPer, "OCC Success rate: %per%%"); statfg->addElement(OSG::OcclusionCullingTreeBuilder::statNOccTriangles, "Triangles culled: %d"); collector = statfg->getCollector(); tact->setStatCollector(collector); #ifdef OSG_OLD_RENDER_ACTION act ->setStatCollector(collector); #endif mgr->setAction(tact); #ifdef OSG_OLD_RENDER_ACTION mgr->setAction( act); #endif //tact->setOcclusionCullingMinimumFeatureSize(15); //tact->setOcclusionCullingVisibilityThreshold(15); //tact->setScreenLODCoverageThreshold(0.005); if(show) { mainwin->getPort(0)->addForeground(statfg); } #ifdef OSG_WITH_NVPERFSDK NVPMRESULT status; status = NVPMInit(); if (status != NVPM_OK) { FFATAL(("NVPerfSDK failed to initialize - no GPU data will be available")); } else { nvDataProvider->add("gpu_idle"); nvDataProvider->add("pixel_shader_busy"); nvDataProvider->add("vertex_shader_busy"); nvDataProvider->add("shader_waits_for_texture"); if (!nvDataProvider->add("OGL FPS")) FLOG(("nvDataProvider::add: 'OGL FPS' failed!\n")); statfg->addElement(GPUIdleStat); statfg->addElement(PSBusyStat); statfg->addElement(VSBusyStat); statfg->addElement(TextureWaitStat); statfg->addElement(OGLFPSStat); } #endif // GLUT main loop glutMainLoop(); return 0; }
// react to keys void keyboard(unsigned char k, int, int) { switch(k) { case 27: { #ifdef OSG_WITH_NVPERFSDK NVPMShutdown(); #endif mgr = NULL; tact = NULL; debugact = NULL; scene = NULL; mainwin = NULL; debugwin = NULL; statfg = NULL; OSG::osgExit(); exit(0); } #ifdef OSG_OLD_RENDER_ACTION case 'v': { mgr->getAction()->setVolumeDrawing( !mgr->getAction()->getVolumeDrawing()); std::cerr << "Volume Drawing: " << (mgr->getAction()->getVolumeDrawing()?"on":"off") << std::endl; } case 'z': { OSG::RenderAction *ract = dynamic_cast<OSG::RenderAction *>(mgr->getAction()); ract->setZWriteTrans(!ract->getZWriteTrans()); std::cerr << "Switch TransZWrite to " << (ract->getZWriteTrans()?"on":"off") << std::endl; } break; #endif case 'r': { initElements(); bool buseTrav = mgr->getUseTraversalAction(); buseTrav = !buseTrav; mgr->setUseTraversalAction(buseTrav); } break; #ifdef OSG_OLD_RENDER_ACTION case 'n': fprintf(stderr, "1) set s sorting to %d\n", act->getStateSorting()); act->setStateSorting(!act->getStateSorting()); fprintf(stderr, "2) set s sorting to %d\n", act->getStateSorting()); break; #endif case 'k': tact->setKeyGen(0); break; case 's': { OSG::UInt32 uiSId = OSG::SimpleSHLChunk ::getStaticClassId() & 0x000003FF; OSG::UInt32 uiTId = OSG::TextureBaseChunk::getStaticClassId() & 0x000003FF; OSG::UInt32 uiMId = OSG::MaterialChunk ::getStaticClassId() & 0x000003FF; OSG::UInt32 uiKeyGen = (uiSId) | (uiTId << 10) | (uiMId << 20); tact->setKeyGen(uiKeyGen); } break; case 'g': bGLFinish = !bGLFinish; tact->setUseGLFinish(bGLFinish); #ifdef OSG_OLD_RENDER_ACTION act->setUseGLFinish(bGLFinish); #endif break; case 'C': { OSG::Real32 cov = tact->getOcclusionCullingCoveredThreshold(); cov+=0.1f; tact->setOcclusionCullingCoveredThreshold(cov); std::cout << "Covered Threshold now: " << cov << std::endl; } break; case 'c': { OSG::Real32 cov1 = tact->getOcclusionCullingCoveredThreshold(); cov1-=0.1f; tact->setOcclusionCullingCoveredThreshold(cov1); std::cout << "Covered Threshold now: " << cov1 << std::endl; } break; case 'M': { OSG::UInt32 minFSize = tact->getOcclusionCullingMinimumFeatureSize(); minFSize+=1; tact->setOcclusionCullingMinimumFeatureSize(minFSize); std::cout << "Minimum Feature Size now: " << minFSize << std::endl; } break; case 'm': { OSG::UInt32 small1 = tact->getOcclusionCullingMinimumFeatureSize(); small1-=1; tact->setOcclusionCullingMinimumFeatureSize(small1); std::cout << "Minimum Feature Size now: " << small1 << std::endl; } break; case 'I': { OSG::UInt32 visT = tact->getOcclusionCullingVisibilityThreshold(); visT+=1; tact->setOcclusionCullingVisibilityThreshold(visT); std::cout << "Visibility Threshold now: " << visT << std::endl; } break; case 'i': { OSG::UInt32 visTa = tact->getOcclusionCullingVisibilityThreshold(); visTa-=1; tact->setOcclusionCullingVisibilityThreshold(visTa); std::cout << "Visibility Threshold now: " << visTa << std::endl; } break; case 'l': { OSG::UInt32 numLev = tact->getScreenLODNumLevels(); numLev-=1; tact->setScreenLODNumLevels(numLev); std::cout << "LOD's in use now: " << numLev << std::endl; } break; case 'L': { OSG::UInt32 numLeva = tact->getScreenLODNumLevels(); numLeva+=1; tact->setScreenLODNumLevels(numLeva); std::cout << "LOD's in use now: " << numLeva << std::endl; } break; case 'B': { OSG::UInt32 bfsz = tact->getOcclusionCullingQueryBufferSize(); bfsz+=200; tact->setOcclusionCullingQueryBufferSize(bfsz); std::cout << "Query Buffer Size now: " << bfsz << std::endl; } break; case 'b': { OSG::UInt32 bfsza = tact->getOcclusionCullingQueryBufferSize(); bfsza-=200; tact->setOcclusionCullingQueryBufferSize(bfsza); std::cout << "Query Buffer Size now: " << bfsza << std::endl; } break; case 't': { OSG::UInt32 tcount = tact->getOcclusionCullingMinimumTriangleCount(); tcount-=50; tact->setOcclusionCullingMinimumTriangleCount(tcount); std::cout << "Minimum Triangle Count now: " << tcount << std::endl; } break; case 'T': { OSG::UInt32 tcounta = tact->getOcclusionCullingMinimumTriangleCount(); tcounta+=50; tact->setOcclusionCullingMinimumTriangleCount(tcounta); std::cout << "Minimum Triangle Count now: " << tcounta << std::endl; } break; case 'H': { OSG::UInt32 mfsa = 0; OSG::Real32 sfta = 0.0f; tact->setOcclusionCullingMinimumFeatureSize(mfsa); tact->setOcclusionCullingVisibilityThreshold(mfsa); tact->setScreenLODCoverageThreshold(sfta); std::cout << "High Resolution Mode" << std::endl; } break; case 'h': { OSG::UInt32 mfs = 1000; OSG::Real32 sft = 0.15f; tact->setOcclusionCullingMinimumFeatureSize(mfs); tact->setOcclusionCullingVisibilityThreshold(mfs); tact->setScreenLODCoverageThreshold(sft); std::cout << "Low Resolution Mode" << std::endl; } break; case 'P': { OSG::Real32 cover = tact->getScreenLODCoverageThreshold(); cover+=0.001f; tact->setScreenLODCoverageThreshold(cover); std::cout << "ScreenLOD Coverage Threshold now: " << cover << std::endl; } break; case 'p': { OSG::Real32 covera = tact->getScreenLODCoverageThreshold(); covera-=0.001f; tact->setScreenLODCoverageThreshold(covera); std::cout << "ScreenLOD Coverage Threshold now: " << covera << std::endl; } break; case 'D': { OSG::Real32 user_dega = tact->getScreenLODDegradationFactor(); user_dega+=0.01f; tact->setScreenLODDegradationFactor(user_dega); std::cout << "ScreenLOD User Degradation Factor now: " << user_dega << std::endl; } break; case 'd': { OSG::Real32 user_deg = tact->getScreenLODDegradationFactor(); user_deg-=0.01f; tact->setScreenLODDegradationFactor(user_deg); std::cout << "ScreenLOD User Degradation Factor now: " << user_deg << std::endl; } break; case 'N': { OSG::Real32 coverb = tact->getScreenLODCoverageThreshold(); coverb=0.0; tact->setScreenLODCoverageThreshold(coverb); std::cout << "ScreenLOD Coverage Threshold now: " << coverb << std::endl; } break; case 'o': { tact->setOcclusionCulling(false); std::cout << "OCC Off" << std::endl; } break; case 'O': { tact->setOcclusionCulling(true); std::cout << "OCC On" << std::endl; } break; case 'f': { std::cout << "Freeze Occlusion result" << std::endl; //initElements(); mgr->setUseTraversalAction(false); // Render stuff that is visible and tested mgr->getWindow()->getPort(0)->setTravMask(0x5); } break; case 'F': { std::cout << "Unfreeze Occlusion result" << std::endl; //initElements(); mgr->setUseTraversalAction(true); mgr->getWindow()->getPort(0)->setTravMask(0xffffffff); } break; case 'W': { std::string sss = "out.osb"; OSG::SceneFileHandler::the()->write(scene, sss.c_str()); } break; #ifdef OSG_WITH_NVPERFSDK case 'e': runExperiment = true; break; #endif } redisplay(); }