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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/31 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OSG/TextureCubeMap 立方贴图天空盒示例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原來(lái)實(shí)例帶的圖片不清楚,換做了opengl入門(mén)里面的圖片:

#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()){//設(shè)置立方圖的六個(gè)面的貼圖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());//設(shè)置紋理環(huán)繞模式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);//設(shè)置濾波:線形和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){//得到模型視圖矩陣并設(shè)置旋轉(zhuǎn)角度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() );//設(shè)置紋理矩陣_texMat.setMatrix( C*R );}traverse(node,nv);}//紋理矩陣osg::TexMat& _texMat; };//一個(gè)變換類(lèi),使天空盒繞視點(diǎn)旋轉(zhuǎn) class MoveEarthySkyWithEyePointTransform : public osg::Transform { public://局部矩陣計(jì)算成世界矩陣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;}//世界矩陣計(jì)算為局部矩陣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;} };//創(chuàng)建天空盒 osg::ref_ptr<osg::Node> createSkyBox() {osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();//設(shè)置紋理映射方式,指定為替代方式,即紋理中的顏色代替原來(lái)的顏色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);//自動(dòng)生成紋理坐標(biāo),反射方式(REFLECTION_MAP)/*NORMAL_MAP 標(biāo)準(zhǔn)模式-立方圖紋理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);//設(shè)置紋理矩陣osg::ref_ptr<osg::TexMat> tm = new osg::TexMat;stateset->setTextureAttribute(0, tm.get());//設(shè)置立方圖紋理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 );//將深度設(shè)置為遠(yuǎn)平面osg::ref_ptr<osg::Depth> depth = new osg::Depth;depth->setFunction(osg::Depth::ALWAYS);depth->setRange(1.0,1.0);//遠(yuǎn)平面 stateset->setAttributeAndModes(depth, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);//設(shè)置渲染順序?yàn)?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));//把球體加入到葉節(jié)點(diǎn)osg::ref_ptr<osg::Geode> geode = new osg::Geode;geode->setCullingActive(false);geode->setStateSet( stateset.get());geode->addDrawable(drawable.get());//設(shè)置變換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"));//優(yōu)化場(chǎng)景數(shù)據(jù)osgUtil::Optimizer optimzer;optimzer.optimize(rootnode.get());viewer->setSceneData(rootnode.get());viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));viewer->addEventHandler(new osgViewer::StatsHandler());//實(shí)現(xiàn)狀態(tài)信息統(tǒng)計(jì)viewer->addEventHandler(new osgViewer::WindowSizeHandler());viewer->realize();viewer->run();return 0 ; }

效果如下圖所示:

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。