生活随笔
收集整理的這篇文章主要介紹了
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
);cubemap
->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
);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
);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 立方贴图天空盒示例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。