src/skyBox.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by                                                 *
00003  *         Pierre-yves JEZEQUEL, Julien MICHOT, Loic MOISAN,               *
00004  *         Julien PAPILLON, Sebastien PINEAUD, Barthelemy SERRES           *
00005  *                                                                         *
00006  *   https://sourceforge.net/projects/anidam                               *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  ***************************************************************************/
00023 #include <string>
00024 #include <stdio.h>
00025 #include "skyBox.h"
00026 
00027 SkyBox::SkyBox()
00028     {
00029         //SkyBox::readCubeMap();
00030     }
00031 SkyBox::~SkyBox()
00032     {
00033     
00034     }
00035 
00036 
00037 osg::TextureCubeMap* SkyBox::readCubeMap()
00038 {
00039     osg::TextureCubeMap* cubemap = new osg::TextureCubeMap;
00040     //#define CUBEMAP_FILENAME(face) "nvlobby_" #face ".png"
00041     //#define CUBEMAP_FILENAME(face) "Cubemap_axis/" #face ".png"
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     //std::cout << "CubeMap ok." << std::endl;
00068         printf("Cube map ok\n");
00069         }
00070 catch(std::exception & e)
00071         {
00072             printf("%s",e.what());//std::cout << "Erreur cubeMap.(" << e.what() << ")" << std::endl; 
00073         }
00074     return cubemap;
00075 }
00076 
00077 
00078 // Update texture matrix for cubemaps
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     // clear the depth to the far plane.
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 //  clearNode->setRequiresClear(false);
00183     clearNode->setCullCallback(new TexMatCallback(*tm));
00184     clearNode->addChild(transform);
00185 
00186     return clearNode;
00187 }
00188 

Generated on Tue Jun 5 16:56:48 2007 for Anidam by  doxygen 1.5.1