00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <string>
00024 #include <stdio.h>
00025 #include "skyBox.h"
00026
00027 SkyBox::SkyBox()
00028 {
00029
00030 }
00031 SkyBox::~SkyBox()
00032 {
00033
00034 }
00035
00036
00037 osg::TextureCubeMap* SkyBox::readCubeMap()
00038 {
00039 osg::TextureCubeMap* cubemap = new osg::TextureCubeMap;
00040
00041
00042 #define CUBEMAP_FILENAME(face) "Cubemap/" #face ".jpg"
00043 try{
00044 osg::Image* imagePosX = osgDB::readImageFile(CUBEMAP_FILENAME(posx));
00045 osg::Image* imageNegX = osgDB::readImageFile(CUBEMAP_FILENAME(negx));
00046 osg::Image* imagePosY = osgDB::readImageFile(CUBEMAP_FILENAME(posy));
00047 osg::Image* imageNegY = osgDB::readImageFile(CUBEMAP_FILENAME(negy));
00048 osg::Image* imagePosZ = osgDB::readImageFile(CUBEMAP_FILENAME(posz));
00049 osg::Image* imageNegZ = osgDB::readImageFile(CUBEMAP_FILENAME(negz));
00050
00051 if (imagePosX && imageNegX && imagePosY && imageNegY && imagePosZ && imageNegZ)
00052 {
00053 cubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
00054 cubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);
00055 cubemap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
00056 cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);
00057 cubemap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
00058 cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
00059
00060 cubemap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
00061 cubemap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
00062 cubemap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
00063
00064 cubemap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
00065 cubemap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
00066 }
00067
00068 printf("Cube map ok\n");
00069 }
00070 catch(std::exception & e)
00071 {
00072 printf("%s",e.what());
00073 }
00074 return cubemap;
00075 }
00076
00077
00078
00079 struct TexMatCallback : public osg::NodeCallback
00080 {
00081 public:
00082
00083 TexMatCallback(osg::TexMat& tm) :
00084 _texMat(tm)
00085 {
00086 }
00087
00088 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
00089 {
00090 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
00091 if (cv)
00092 {
00093 const osg::Matrix& MV = cv->getModelViewMatrix();
00094 const osg::Matrix R = osg::Matrix::rotate( osg::DegreesToRadians(112.0f), 0.0f,0.0f,1.0f)*
00095 osg::Matrix::rotate( osg::DegreesToRadians(90.0f), 1.0f,0.0f,0.0f);
00096
00097 osg::Quat q = MV.getRotate();
00098 const osg::Matrix C = osg::Matrix::rotate( q.inverse() );
00099
00100 _texMat.setMatrix( C*R );
00101 }
00102
00103 traverse(node,nv);
00104 }
00105
00106 osg::TexMat& _texMat;
00107 };
00108
00109
00110 class MoveEarthySkyWithEyePointTransform : public osg::Transform
00111 {
00112 public:
00114 virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const
00115 {
00116 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
00117 if (cv)
00118 {
00119 osg::Vec3 eyePointLocal = cv->getEyeLocal();
00120 matrix.preMult(osg::Matrix::translate(eyePointLocal));
00121 }
00122 return true;
00123 }
00124
00126 virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const
00127 {
00128 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
00129 if (cv)
00130 {
00131 osg::Vec3 eyePointLocal = cv->getEyeLocal();
00132 matrix.postMult(osg::Matrix::translate(-eyePointLocal));
00133 }
00134 return true;
00135 }
00136 };
00137
00138
00139 osg::Node* SkyBox::createSkyBox()
00140 {
00141
00142 osg::StateSet* stateset = new osg::StateSet();
00143
00144 osg::TexEnv* te = new osg::TexEnv;
00145 te->setMode(osg::TexEnv::REPLACE);
00146 stateset->setTextureAttributeAndModes(0, te, osg::StateAttribute::ON);
00147
00148 osg::TexGen *tg = new osg::TexGen;
00149 tg->setMode(osg::TexGen::NORMAL_MAP);
00150 stateset->setTextureAttributeAndModes(0, tg, osg::StateAttribute::ON);
00151
00152 osg::TexMat *tm = new osg::TexMat;
00153 stateset->setTextureAttribute(0, tm);
00154
00155 osg::TextureCubeMap* skymap = readCubeMap();
00156 stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON);
00157
00158 stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
00159 stateset->setMode( GL_CULL_FACE, osg::StateAttribute::OFF );
00160
00161
00162 osg::Depth* depth = new osg::Depth;
00163 depth->setFunction(osg::Depth::ALWAYS);
00164 depth->setRange(1.0,1.0);
00165 stateset->setAttributeAndModes(depth, osg::StateAttribute::ON );
00166
00167 stateset->setRenderBinDetails(6,"RenderBin");
00168
00169 osg::Drawable* drawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),100));
00170
00171 osg::Geode* geode = new osg::Geode;
00172 geode->setCullingActive(false);
00173 geode->setStateSet( stateset );
00174 geode->addDrawable(drawable);
00175
00176
00177 osg::Transform* transform = new MoveEarthySkyWithEyePointTransform;
00178 transform->setCullingActive(false);
00179 transform->addChild(geode);
00180
00181 osg::ClearNode* clearNode = new osg::ClearNode;
00182
00183 clearNode->setCullCallback(new TexMatCallback(*tm));
00184 clearNode->addChild(transform);
00185
00186 return clearNode;
00187 }
00188