#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class MyAppCallback : public osg::NodeCallback { public: MyAppCallback(osg::Node* subgraph): _subgraph(subgraph) {} virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { // traverse the subgraph to update any nodes. if (_subgraph.valid()) _subgraph->accept(*nv); // must traverse the Node's subgraph traverse(node,nv); } osg::ref_ptr _subgraph; }; class MyCullCallback : public osg::NodeCallback { public: MyCullCallback(osg::Node* subgraph,osg::Texture* texture): _subgraph(subgraph), _texture(texture) {} MyCullCallback(osg::Node* subgraph,osg::Image* image): _subgraph(subgraph), _image(image) {} virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { osgUtil::CullVisitor* cullVisitor = dynamic_cast(nv); if (cullVisitor && (_texture.valid()|| _image.valid()) && _subgraph.valid()) { doPreRender(*node,*cullVisitor); // must traverse the subgraph traverse(node,nv); } else { // must traverse the subgraph traverse(node,nv); } } void doPreRender(osg::Node& node, osgUtil::CullVisitor& cv); osg::ref_ptr _subgraph; osg::ref_ptr _texture; osg::ref_ptr _image; }; void MyCullCallback::doPreRender(osg::Node&, osgUtil::CullVisitor& cv) { const osg::BoundingSphere& bs = _subgraph->getBound(); if (!bs.valid()) { osg::notify(osg::WARN) << "bb invalid"<<_subgraph.get()< rtts = osgNew osgUtil::RenderToTextureStage; // set up lighting. // currently ignore lights in the scene graph itself.. // will do later. osgUtil::RenderStage* previous_stage = cv.getCurrentRenderBin()->_stage; // set up the background color and clear mask. rtts->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,0.0f)); rtts->setClearMask(previous_stage->getClearMask()); // set up to charge the same RenderStageLighting is the parent previous stage. rtts->setRenderStageLighting(previous_stage->getRenderStageLighting()); // record the render bin, to be restored after creation // of the render to text osgUtil::RenderBin* previousRenderBin = cv.getCurrentRenderBin(); // set the current renderbin to be the newly created stage. cv.setCurrentRenderBin(rtts.get()); float znear = 1.0f*bs.radius(); float zfar = 3.0f*bs.radius(); // 2:1 aspect ratio as per flag geomtry below. float top = 0.25f*znear; float right = 0.5f*znear; znear *= 0.9f; zfar *= 1.1f; // set up projection. osg::Matrix* projection = osgNew osg::Matrix; projection->makeFrustum(-right,right,-top,top,znear,zfar); cv.pushProjectionMatrix(projection); osg::Matrix* matrix = new osg::Matrix; matrix->makeLookAt(bs.center()+osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f)); cv.pushModelViewMatrix(matrix); osg::ref_ptr dummyState = osgNew osg::StateSet; cv.pushStateSet(dummyState.get()); { // traverse the subgraph _subgraph->accept(cv); } cv.popStateSet(); // restore the previous model view matrix. cv.popModelViewMatrix(); // restore the previous model view matrix. cv.popProjectionMatrix(); // restore the previous renderbin. cv.setCurrentRenderBin(previousRenderBin); if (rtts->_renderGraphList.size()==0 && rtts->_bins.size()==0) { // getting to this point means that all the subgraph has been // culled by small feature culling or is beyond LOD ranges. return; } int height = 256; int width = 512; const osg::Viewport& viewport = *cv.getViewport(); // offset the impostor viewport from the center of the main window // viewport as often the edges of the viewport might be obscured by // other windows, which can cause image/reading writing problems. int center_x = viewport.x()+viewport.width()/2; int center_y = viewport.y()+viewport.height()/2; osg::Viewport* new_viewport = new osg::Viewport; new_viewport->setViewport(center_x-width/2,center_y-height/2,width,height); rtts->setViewport(new_viewport); dummyState->setAttribute(new_viewport); // and the render to texture stage to the current stages // dependancy list. cv.getCurrentRenderBin()->_stage->addToDependencyList(rtts.get()); // if one exist attach texture to the RenderToTextureStage. if (_texture.valid()) rtts->setTexture(_texture.get()); // if one exist attach image to the RenderToTextureStage. if (_image.valid()) rtts->setImage(_image.get()); } // call back which cretes a deformation field to oscilate the model. class MyGeometryCallback : public osg::Drawable::AppCallback, public osg::Drawable::AttributeFunctor { public: MyGeometryCallback(const osg::Vec3& o, const osg::Vec3& x,const osg::Vec3& y,const osg::Vec3& z, double period,double xphase,double amplitude): osg::Drawable::AttributeFunctor(osg::Drawable::COORDS), _firstCall(true), _startTime(0.0), _time(0.0), _period(period), _xphase(xphase), _amplitude(amplitude), _origin(o), _xAxis(x), _yAxis(y), _zAxis(z) {} virtual void app(osg::NodeVisitor* nv,osg::Drawable* drawable) { const osg::FrameStamp* fs = nv->getFrameStamp(); double referenceTime = fs->getReferenceTime(); if (_firstCall) { _firstCall = false; _startTime = referenceTime; } _time = referenceTime-_startTime; drawable->applyAttributeOperation(*this); drawable->dirtyBound(); osg::Geometry* geometry = dynamic_cast(drawable); if (geometry) { osgUtil::SmoothingVisitor::smooth(*geometry); } } virtual bool apply(osg::Drawable::AttributeBitMask abm,osg::Vec3* begin,osg::Vec3* end) { if (abm == osg::Drawable::COORDS) { const float TwoPI=2.0f*osg::PI; const float phase = -_time/_period; for (osg::Vec3* itr=begin;itrsetSupportsDisplayList(false); osg::Vec3 origin(0.0f,0.0f,0.0f); osg::Vec3 xAxis(1.0f,0.0f,0.0f); osg::Vec3 yAxis(0.0f,0.0f,1.0f); osg::Vec3 zAxis(0.0f,-1.0f,0.0f); float height = 100.0f; float width = 200.0f; int noSteps = 20; osg::Vec3Array* vertices = new osg::Vec3Array; osg::Vec3 bottom = origin; osg::Vec3 top = origin; top.z()+= height; osg::Vec3 dv = xAxis*(width/((float)(noSteps-1))); osg::Vec2Array* texcoords = new osg::Vec2Array; osg::Vec2 bottom_texcoord(0.0f,0.0f); osg::Vec2 top_texcoord(0.0f,1.0f); osg::Vec2 dv_texcoord(1.0f/(float)(noSteps-1),0.0f); for(int i=0;ipush_back(top); vertices->push_back(bottom); top+=dv; bottom+=dv; texcoords->push_back(top_texcoord); texcoords->push_back(bottom_texcoord); top_texcoord+=dv_texcoord; bottom_texcoord+=dv_texcoord; } // pass the created vertex array to the points geometry object. polyGeom->setVertexArray(vertices); polyGeom->setTexCoordArray(0,texcoords); osg::Vec4Array* colors = new osg::Vec4Array; colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); polyGeom->setColorArray(colors); polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); polyGeom->addPrimitive(new osg::DrawArrays(osg::Primitive::QUAD_STRIP,0,vertices->size())); // new we need to add the texture to the Drawable, we do so by creating a // StateSet to contain the Texture StateAttribute. osg::StateSet* stateset = new osg::StateSet; // set up the texture. osg::Image* image = new osg::Image; image->setInternalTextureFormat(GL_RGBA); //osg::Image* image = osgDB::readImageFile("lz.rgb"); osg::Texture* texture = new osg::Texture; texture->setSubloadMode(osg::Texture::IF_DIRTY); texture->setImage(image); stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); // { // // set up the second tex coord array for the an additional texture // // to add texture to the flag. // polyGeom->setTexCoordArray(1,texcoords); // // osg::Image* overlay_image = osgDB::readImageFile("tank.rgb"); // osg::Texture* overlay_texture = new osg::Texture; // overlay_texture->setImage(overlay_image); // stateset->setTextureAttributeAndModes(1, overlay_texture,osg::StateAttribute::ON); // // osg::TexEnv* overlay_texenv = new osg::TexEnv; // overlay_texenv->setMode(osg::TexEnv::BLEND); // overlay_texenv->setColor(osg::Vec4(0.3f,0.3f,0.3f,1.0f)); // stateset->setTextureAttribute(1, overlay_texenv); // } polyGeom->setStateSet(stateset); polyGeom->setAppCallback(new MyGeometryCallback(origin,xAxis,yAxis,zAxis,1.0,1.0/width,0.2f)); osg::Geode* geode = osgNew osg::Geode(); geode->addDrawable(polyGeom); osg::Group* parent = new osg::Group; parent->setAppCallback(new MyAppCallback(subgraph)); // parent->setCullCallback(new MyCullCallback(subgraph,texture)); parent->setCullCallback(new MyCullCallback(subgraph,image)); // parent->addChild(billboard); parent->addChild(geode); return parent; } void write_usage(std::ostream& out,const std::string& name) { out << std::endl; out <<"usage:"<< std::endl; out <<" "< commandLine; for(int i=1;iaddChild(loadedModel); osg::NodeCallback* nc = new osgUtil::TransformCallback(loadedModelTransform->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); loadedModelTransform->setAppCallback(nc); osg::Group* rootNode = new osg::Group(); // rootNode->addChild(loadedModelTransform); rootNode->addChild(createPreRenderSubGraph(loadedModelTransform)); // add model to the viewer. viewer.addViewport( rootNode ); // register trackball, flight and drive. viewer.registerCameraManipulator(new osgGA::TrackballManipulator); viewer.registerCameraManipulator(new osgGA::FlightManipulator); viewer.registerCameraManipulator(new osgGA::DriveManipulator); viewer.open(); viewer.run(); return 0; }