日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OSG/TextureCubeMap 立方贴图天空盒示例

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OSG/TextureCubeMap 立方贴图天空盒示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原來實例帶的圖片不清楚,換做了opengl入門里面的圖片:

#include <osgViewer/Viewer>#include <osg/Vec3> #include <osg/Vec4> #include <osg/Quat> #include <osg/Matrix> #include <osg/ShapeDrawable> #include <osg/Geometry> #include <osg/Geode> #include <osg/Transform> #include <osg/Material> #include <osg/NodeCallback> #include <osg/Depth> #include <osg/CullFace> #include <osg/TexMat> #include <osg/TexGen> #include <osg/TexEnv> #include <osg/TextureCubeMap>#include <osgDB/WriteFile> #include <osgDB/ReadFile>#include <osgUtil/Optimizer>#include <iostream>#pragma comment(lib, "OpenThreadsd.lib") #pragma comment(lib, "osgd.lib") #pragma comment(lib, "osgDBd.lib") #pragma comment(lib, "osgUtild.lib") #pragma comment(lib, "osgGAd.lib") #pragma comment(lib, "osgViewerd.lib") #pragma comment(lib, "osgTextd.lib")//讀取立方圖 osg::ref_ptr<osg::TextureCubeMap> readCubeMap() {osg::ref_ptr<osg::TextureCubeMap> cubemap = new osg::TextureCubeMap;osg::ref_ptr<osg::Image> imagePosX = osgDB::readImageFile("right.jpg");osg::ref_ptr<osg::Image> imageNegX = osgDB::readImageFile("left.jpg");osg::ref_ptr<osg::Image> imagePosY = osgDB::readImageFile("down.jpg");osg::ref_ptr<osg::Image> imageNegY = osgDB::readImageFile("up.jpg");osg::ref_ptr<osg::Image> imagePosZ = osgDB::readImageFile("front.jpg");osg::ref_ptr<osg::Image> imageNegZ = osgDB::readImageFile("back.jpg");if (imagePosX.get() && imageNegX.get() && imagePosY.get() && imageNegY.get() && imagePosZ.get() && imageNegZ.get()){//設置立方圖的六個面的貼圖cubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX.get());cubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX.get());cubemap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY.get());cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY.get());cubemap->setImage(osg::TextureCubeMap::POSITIVE_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);//設置濾波:線形和mipmapcubemap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);cubemap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);}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::DegreesToRadians(112.0f), 0.0f,0.0f,1.0f)*osg::Matrix::rotate( osg::DegreesToRadians(90.0f), 1.0f,0.0f,0.0f);osg::Quat q = MV.getRotate();const osg::Matrix C = osg::Matrix::rotate( q.inverse() );//設置紋理矩陣_texMat.setMatrix( C*R );}traverse(node,nv);}//紋理矩陣osg::TexMat& _texMat; };//一個變換類,使天空盒繞視點旋轉 class MoveEarthySkyWithEyePointTransform : 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.postMult(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);//自動生成紋理坐標,反射方式(REFLECTION_MAP)/*NORMAL_MAP 標準模式-立方圖紋理REFLECTION_MAP 反射模式-球體紋理SPHERE_MAP 球體模型-球體紋理*/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::Depth> depth = new osg::Depth;depth->setFunction(osg::Depth::ALWAYS);depth->setRange(1.0,1.0);//遠平面 stateset->setAttributeAndModes(depth, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);//設置渲染順序為-1,先渲染stateset->setRenderBinDetails(-1,"RenderBin");osg::ref_ptr<osg::Drawable> 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 MoveEarthySkyWithEyePointTransform();transform->setCullingActive(false);transform->addChild(geode.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());// 加入模型rootnode->addChild(osgDB::readNodeFile("cow.osg"));//優化場景數據osgUtil::Optimizer optimzer;optimzer.optimize(rootnode.get());viewer->setSceneData(rootnode.get());viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));viewer->addEventHandler(new osgViewer::StatsHandler());//實現狀態信息統計viewer->addEventHandler(new osgViewer::WindowSizeHandler());viewer->realize();viewer->run();return 0 ; }

效果如下圖所示:

總結

以上是生活随笔為你收集整理的OSG/TextureCubeMap 立方贴图天空盒示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。