|
楼主 |
发表于 2010-5-22 16:35:03
|
显示全部楼层
- osg::ref_ptr<osg::TextureCubeMap>readCubeMap()
- {
- osg::ref_ptr<osg::TextureCubeMap>cubemap = new osg::TextureCubeMap;
- osg::ref_ptr<osg::Image>imagePosX = osgDB::readImageFile("Cubemap_snow/posx.jpg");
- osg::ref_ptr<osg::Image>imageNegX = osgDB::readImageFile("Cubemap_snow/negx.jpg");
- osg::ref_ptr<osg::Image>imagePosY = osgDB::readImageFile("Cubemap_snow/posy.jpg");
- osg::ref_ptr<osg::Image>imageNegY = osgDB::readImageFile("Cubemap_snow/negy.jpg");
- osg::ref_ptr<osg::Image>imagePosZ = osgDB::readImageFile("Cubemap_snow/posz.jpg");
- osg::ref_ptr<osg::Image>imageNegZ = osgDB::readImageFile("Cubemap_snow/negz.jpg");
- if(imagePosX.get()&&imageNegX.get()&&imagePosY.get()&&imageNegY.get()&&imagePosZ.get()&&imageNegZ.get())
- {
- cubemap->setImage(osg::TextureCubeMap:OSITIVE_X,imagePosX.get());
- cubemap->setImage(osg::TextureCubeMap::NEGATIVE_X,imageNegX.get());
- cubemap->setImage(osg::TextureCubeMap:OSITIVE_Y,imagePosY.get());
- cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y,imageNegY.get());
- cubemap->setImage(osg::TextureCubeMap:OSITIVE_Z,imagePosZ.get());
- cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z,imageNegZ.get());
- cubemap->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
- cubemap->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
- cubemap->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE);
- cubemap->setFilter(osg::Texture::MIN_FILTER,osg::Texture:INEAR_MIPMAP_LINEAR);
- cubemap->setFilter(osg::Texture::MAG_FILTER,osg::Texture:INEAR);
- }
- return cubemap.get();
- }
- struct TexMatCallback:public osg::NodeCallback
- {
- public:
- TexMatCallback(osg::TexMat& tm):
- _texMat(tm)
- {
- }
- virtual void operator()(osg::Node* node,osg::NodeVisitor* nv)
- {
- osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
- if(cv)
- {
- const osg::Matrix& MV = *(cv->getModelViewMatrix());
- const osg::Matrix R = osg::Matrix::rotate(osg:egreesToRadians(112.0f),0.0f,0.0f,1.0f)*
- osg::Matrix::rotate(osg:egreesToRadians(90.0f),1.0f,0.0f,0.0f);
- osg:uat q = MV.getRotate();
- const osg::Matrix C = osg::Matrix::rotate(q.inverse());
- _texMat.setMatrix(C*R);
- }
- traverse(node,nv);
- }
- osg::TexMat& _texMat;
- };
- class MoveEarthSkyWithEyePointTransform:public osg::Transform
- {
- public:
- virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv)const
- {
- osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
- if(cv)
- {
- osg::Vec3 eyePointLocal = cv->getEyeLocal();
- matrix.preMult(osg::Matrix::translate(eyePointLocal));
- }
- return true;
- }
- virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv)const
- {
- osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
- if(cv)
- {
- osg::Vec3 eyePointLocal = cv->getEyeLocal();
- matrix.preMult(osg::Matrix::translate(-eyePointLocal));
- }
- return true;
- }
- };
- osg::ref_ptr<osg::Node>createSkyBox()
- {
- osg::ref_ptr<osg::StateSet>stateset = new osg::StateSet();
- osg::ref_ptr<osg::TexEnv>te = new osg::TexEnv;
- te->setMode(osg::TexEnv::REPLACE);
- stateset->setTextureAttributeAndModes(0,te.get(),osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
- osg::ref_ptr<osg::TexGen>tg = new osg::TexGen;
- tg->setMode(osg::TexGen::NORMAL_MAP);
- stateset->setTextureAttributeAndModes(0,tg.get(),osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
- osg::ref_ptr<osg::TexMat>tm = new osg::TexMat;
- stateset->setTextureAttribute(0,tm.get());
-
- osg::ref_ptr<osg::TextureCubeMap>skymap = readCubeMap();
- stateset->setTextureAttributeAndModes(0,skymap.get(),osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
- stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
- stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);
- osg::ref_ptr<osg:epth>depth = new osg:epth;
-
- depth->setFunction(osg:epth::ALWAYS);
- depth->setRange(1.0,1.0);
- stateset->setAttributeAndModes(depth,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
- stateset->setRenderBinDetails(-1,"RenderBin");
- osg::ref_ptr<osg:rawable>drawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),1));
- osg::ref_ptr<osg::Geode>geode = new osg::Geode;
- geode->setCullingActive(false);
- geode->setStateSet(stateset.get());
- geode->addDrawable(drawable.get());
- osg::ref_ptr<osg::Transform>transform = new MoveEarthSkyWithEyePointTransform();
- transform->setCullingActive(false);
- transform->addChild(transform.get());
- osg::ref_ptr<osg::ClearNode> clearNode = new osg::ClearNode;
- clearNode->setCullCallback(new TexMatCallback(*tm));
- clearNode->addChild(transform.get());
- return clearNode.get();
- }
- int main()
- {
- osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer();
- osg::ref_ptr<osg::Group>rootnode = new osg::Group();
- rootnode->addChild(createSkyBox());
- osgUtil::Optimizer optimizer;
- optimizer.optimize(rootnode.get());
- viewer->setSceneData(rootnode.get());
- viewer->realize();
- viewer->run();
- return 0;
- }
复制代码 |
|