void TroenGame::fixCulling(osg::ref_ptr<osgViewer::View> view) { double fovy, aspect, znear, zfar; view->getCamera()->getProjectionMatrixAsPerspective(fovy, aspect, znear, zfar); view->getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); znear = 1.0; view->getCamera()->setProjectionMatrixAsPerspective(fovy, aspect, znear, zfar); }
int _tmain(int argc, _TCHAR* argv[]) { // construct the viewer rViewer = new osgViewer::Viewer; // set up the camera manipulators. { osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); rViewer->setCameraManipulator( keyswitchManipulator.get() ); } // make the viewer create a 800x600 window and position it at 32, 32 rViewer->setUpViewInWindow( 32, 32, 800, 600 ); // create a console osg::ref_ptr<CSulConsoleDisplay> rConsoleDisplay = new CSulConsoleDisplay( rViewer.get() ); rConsoleDisplay->Init(); osg::Group* pGroup = new osg::Group; pGroup->addChild( CreateScene() ); pGroup->addChild( rConsoleDisplay->GetNode() ); // create object to receive console commands osg::ref_ptr<CMyConsoleClass> rTest = new CMyConsoleClass( rConsoleDisplay.get() ); // FIXME: grass still gets culled rViewer->getCamera()->setCullingMode( osg::CullSettings::NO_CULLING ); //////////////////////////////////////////////////////////////////////////////// // set the scene-graph data the viewer will render //////////////////////////////////////////////////////////////////////////////// rViewer->setSceneData( pGroup ); // setup an event handle for statistics rViewer->addEventHandler( new osgViewer::StatsHandler ); // execute main loop return rViewer->run(); }
// main int main(int argc, char** argv) { osg::ArgumentParser arguments(&argc, argv); // Let's use the convenience system from ALVAR for capturing. // We initialize Highgui through the CaptureFactory (see manual for other options like AVI) alvar::CaptureFactory *factory = alvar::CaptureFactory::instance(); alvar::CaptureFactory::CaptureDeviceVector devices = factory->enumerateDevices("highgui"); // Check to ensure that a device was found if (devices.size() > 0) { capture = factory->createCapture(devices.front()); } // Capture is central feature, so if we fail, we get out of here. if (capture && capture->start()) { // Let's capture one frame to get video resolution IplImage *tempImg = capture->captureImage(); videoXRes = tempImg->width; videoYRes = tempImg->height; // Calibration. See manual and ALVAR internal samples how to calibrate your camera // Calibration will make the marker detecting and marker pose calculation more accurate. if (! camera.SetCalib("calib.xml", videoXRes, videoYRes)) { camera.SetRes(videoXRes, videoYRes); } //Create the osg::Image for the video videoImage = new osg::Image; //Create the osg::Image for the texture (marker hiding) texImage = new osg::Image; //IplImage for the texture generation. markerHiderImage=cvCreateImage(cvSize(64, 64), 8, 4); // construct the viewer viewer = new osgViewer::Viewer(arguments); // Let's use window size of the video (approximate). viewer->setUpViewInWindow (200, 200, videoXRes, videoYRes); // Viewport is the same viewer->getCamera()->setViewport(0,0,videoXRes,videoYRes); viewer->setLightingMode(osg::View::HEADLIGHT); // Attach our own event handler to the system so we can catch the resizing events viewer->addEventHandler(new CSimpleWndSizeHandler(videoXRes,videoYRes )); // Set projection matrix as ALVAR recommends (based on the camera calibration) double p[16]; camera.GetOpenglProjectionMatrix(p,videoXRes,videoYRes); viewer->getCamera()->setProjectionMatrix(osg::Matrix(p)); // Create main root for everything arRoot = new osg::Group; arRoot->setName("ALVAR stuff (c) VTT"); // Init the video background class and add it to the graph videoBG.Init(videoXRes,videoYRes,(tempImg->origin?true:false)); arRoot->addChild(videoBG.GetOSGGroup()); // Create model switch and add it the to graph modelSwitch = new osg::Switch; arRoot->addChild(modelSwitch.get()); // Create model transformation for the markers and add them under the switch mtForMarkerFive = new osg::MatrixTransform; mtForMarkerTen = new osg::MatrixTransform; modelSwitch->addChild(mtForMarkerFive.get()); modelSwitch->addChild(mtForMarkerTen.get()); // add the texture under the marker transformation node mtForMarkerFive->addChild(texOnMarker.GetDrawable()); // All models off modelSwitch->setAllChildrenOff(); // load the data (models). modelForMarkerFive = osgDB::readNodeFile("grid.osg"); modelForMarkerTen = osgDB::readNodeFile("axes.osg"); // If loading ok, add models under the matrixtransformation nodes. if(modelForMarkerFive) mtForMarkerFive->addChild(modelForMarkerFive.get()); if(modelForMarkerTen) mtForMarkerTen->addChild(modelForMarkerTen.get()); // Tell the ALVAR the markers' size (same to all) // You can also specify different size for each individual markers markerDetector.SetMarkerSize(MARKER_SIZE); // Set scene data viewer->setSceneData(arRoot.get()); // And start the main loop while(!viewer->done()){ //Call the rendering function over and over again. renderer(); } } // Time to close the system if(capture){ capture->stop(); delete capture; } if(markerHiderImage) cvReleaseImage(&markerHiderImage); return 0; // bye bye. Happy coding! }
osg::Node* CreateScene() { osg::Group* pGroup = new osg::Group; pGroup->addChild( createDebugText() ); ////////////////////////// // simple terrain part I ////////////////////////// osg::ref_ptr<CTerrain> rTerrain = new CTerrain; rTerrain->Init(); pGroup->addChild( rTerrain->getNodeWithShader() ); ////////////////////////// // RTT terrain ////////////////////////// osg::Group* pRenderMe = new osg::Group; osg::Geode* pGeodeShape = new osg::Geode; pGeodeShape->addDrawable( new osg::ShapeDrawable( new osg::Sphere(osg::Vec3(0.0f,0.0f,4.0f),1.0f) ) ); pGroup->addChild( pGeodeShape ); pRenderMe->addChild( rTerrain->getNode() ); pGroup->addChild( pGeodeShape ); pHeightRTT = new CHeightRTT; pHeightRTT->init( rViewer, rTextCam, rViewer->getCamera(), pGroup, pRenderMe ); ////////////////////////// // simple terrain part II ////////////////////////// rTerrain->setCam( pHeightRTT->getCamera() ); ////////////////////////// // grass billboard stuff ////////////////////////// pGeodeGrass = new osg::Geode(); geomGrass = createGrassGeom(); pGeodeGrass->addDrawable( geomGrass ); osg::ref_ptr< osg::Shader > vertexShader = new osg::Shader(); vertexShader->setType( osg::Shader::VERTEX ); vertexShader->loadShaderSourceFromFile( "grass.vert" ); osg::ref_ptr< osg::Shader > fragShader = new osg::Shader(); fragShader->setType( osg::Shader::FRAGMENT ); fragShader->loadShaderSourceFromFile( "grass.frag" ); osg::ref_ptr< osg::Program > program = new osg::Program(); program->addShader( vertexShader.get() ); program->addShader( fragShader.get() ); ss = pGeodeGrass->getOrCreateStateSet(); ss->setAttribute( program.get(), osg::StateAttribute::ON | osg::StateAttribute::PROTECTED ); uniformCC = new osg::Uniform("cc",(int)cc); ss->addUniform( uniformCC ); // camera lock uniformLock = new osg::Uniform( "bLock", false ); ss->addUniform( uniformLock ); // spacing uniformSpacing = new osg::Uniform( "spacing", (float)sp ); ss->addUniform( uniformSpacing ); // height map ss->setTextureAttributeAndModes( 1, pHeightRTT->getTexture(), osg::StateAttribute::ON ); osg::Uniform* uniformHeightMap = new osg::Uniform( "texHeightMap", 1 ); ss->addUniform( uniformHeightMap ); // height adjust uniformHeightAdjust = new osg::Uniform( "heightAdjust", heightAdjust ); ss->addUniform( uniformHeightAdjust ); // wind factor uniformWindFactor = new osg::Uniform( "windFactor", windFactor ); ss->addUniform( uniformWindFactor ); // grass stretch uniformGrassStretch = new osg::Uniform( "grassStretch", grassStretch ); ss->addUniform( uniformGrassStretch ); // ortho camera inverse view matrix osg::Uniform* uniformOrthoInverseViewMatrix = new osg::Uniform( "orthoInverseViewMatrix", pHeightRTT->getCamera()->getInverseViewMatrix() ); ss->addUniform( uniformOrthoInverseViewMatrix ); pGeodeGrass->setUpdateCallback( new CMyUpdateCallbackGrass( pHeightRTT->getCamera(), uniformOrthoInverseViewMatrix ) ); osg::Texture2D* pTex = new osg::Texture2D; osg::Image* pImage = osgDB::readImageFile( "grass2.tga" ); pTex->setImage( pImage ); ss->setTextureAttributeAndModes( 0, pTex, osg::StateAttribute::ON ); ss->setMode( GL_BLEND, osg::StateAttribute::ON ); ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); ss->setRenderBinDetails( 9, "DepthSortedBin" ); ss->setMode( GL_ALPHA_TEST, osg::StateAttribute::ON ); ss->setAttribute( new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.9f), osg::StateAttribute::ON ); pGroup->addChild( pGeodeGrass ); //////////////// // a grid //////////////// CSulGeomGrid* pGeomGrid = new CSulGeomGrid; pGeomGrid->Create( osg::Vec3(0,0,0), 10, 10, 1, 1, 5, 5 ); pGroup->addChild( pGeomGrid ); // what does this exactly do? (you would think it would disable culling,.. but it doesn't!) // think it culls when an object is a certain pixel size on the screen pGroup->setCullingActive( false ); return pGroup; }
/* Initialize OSG, create a Viewer */ bool InitOSG(int w, int h, bool flip_image, std::string model_file, std::string camera_file) { int offx = 10; int offy = 30; viewer = new osgViewer::Viewer; osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); if (!wsi) { osg::notify(osg::NOTICE) << "Error, no WindowSystemInterface available, cannot create windows." << std::endl; return false; } unsigned int width, height; wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->x = offx; traits->y = offy; traits->width = w; traits->height = h; traits->windowDecoration = true; traits->doubleBuffer = true; traits->sharedContext = 0; osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); if (gc.valid()) { osg::notify(osg::INFO) << " GraphicsWindow has been created successfully." << std::endl; // need to ensure that the window is cleared make sure that the complete window is set the correct colour // rather than just the parts of the window that are under the camera's viewports gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } else { osg::notify(osg::NOTICE) << " GraphicsWindow has not been created successfully." << std::endl; } model_node = osgDB::readNodeFile(model_file.c_str()); model_transform = new osg::MatrixTransform; model_transform->addChild(model_node.get()); model_switch = new osg::Switch; model_switch->addChild(model_transform.get()); model_switch->setAllChildrenOff(); root_group = new osg::Group; root_group->addChild(model_switch.get()); osg::StateSet *state_set = new osg::StateSet; state_set->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE); model_node->setStateSet(state_set); root_group->setStateSet(state_set); viewer->getCamera()->setViewport(new osg::Viewport(0, 0, w, h)); viewer->getCamera()->setGraphicsContext(gc.get()); viewer->addEventHandler(new PickHandler()); viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); viewBG = new ViewWithBackGroundImage(viewer, w, h, flip_image, root_group); cam.SetCalib(camera_file.c_str(), w, h); double p[16]; cam.GetOpenglProjectionMatrix(p, w, h); viewer->getCamera()->setProjectionMatrix(osg::Matrix(p)); return true; }