src/fogcube.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 "fogcube.h"
00024 
00025 
00026 
00027 FogCube::FogCube()
00028  : osg::Fog()
00029 {
00030 }
00031 
00032 
00033 FogCube::~FogCube()
00034 {
00035 }
00036 
00037 void FogCube::setCube(float xmin,float xmax,float ymin,float ymax,float zmin,float zmax)
00038 {
00039         this->xmin = xmin;
00040         this->xmax = xmax;
00041         this->ymin = ymin;
00042         this->ymax = ymax;
00043         this->zmin = zmin;
00044         this->zmax = zmax;
00045         
00046         node=NULL;
00047 }
00048 
00049 bool FogCube::isInTheCube( float x,float y,float z)
00050 {
00051         return ( x>=xmin && x<=xmax && y>=ymin && y<=ymax && z>=zmin && z<=zmax)  ;
00052 }
00053 void FogCube::addStaticFogCoords(osg::Node* node)
00054 {
00055         //static 
00056         setFogCoordinateSource(osg::Fog::FOG_COORDINATE);
00057         
00058         this->node=node;
00059         addFog(node);
00060         
00061         osg::Geode* tempGeode = dynamic_cast<osg::Geode*>(node);
00062         if(tempGeode!= NULL)
00063         {
00064                 std::cout<<"drawables : " << tempGeode->getNumDrawables() <<std::endl;
00065                 int i;
00066                 for (i = 0; i < tempGeode->getNumDrawables(); i ++)
00067                 {
00068                         osg::Geometry* tempGeometry =dynamic_cast<osg::Geometry*>(tempGeode->getDrawable(i));
00069                         if (tempGeometry != NULL)
00070                         {
00071                                 osg::Vec3Array* vertices =dynamic_cast<osg::Vec3Array*>(tempGeometry->getVertexArray());
00072                                 int numVertices = vertices->getNumElements();
00073                                 osg::FloatArray* coords = new osg::FloatArray(numVertices);
00074                                 for (int i=0; i < numVertices; i ++)
00075                                 {
00076                                         float offset = (zmax - (*vertices)[i].z());
00077                                         (*coords)[i] = 0.0;
00078                                         if(isInTheCube((*vertices)[i].x(),(*vertices)[i].y(),(*vertices)[i].z())) 
00079                                                 (*coords)[i] = offset;
00080                                 }
00081 
00082                                 tempGeometry->setFogCoordArray(coords);
00083                                 tempGeometry->setFogCoordBinding(osg::Geometry::BIND_PER_VERTEX);
00084                         }
00085                 }
00086                 return;
00087         }
00088 
00089         osg::Group* tempGroup = dynamic_cast<osg::Group*>(node);
00090         if(tempGroup!= NULL)
00091         {
00092                 int i;
00093                 for (i = 0; i < tempGroup->getNumChildren(); i ++)
00094                 {
00095                         addStaticFogCoords(tempGroup->getChild(i));
00096                 }
00097                 return;
00098         }
00099 
00100         int temp = 1;
00101 }
00102 
00104 void FogCube::addDynamicFogCoords(osg::Node* node)
00105 {
00106         this->node=node;
00107         addFog(node);
00108         
00109         osg::Geode* tempGeode = dynamic_cast<osg::Geode*>(node);
00110         if(tempGeode!= NULL)
00111         {
00112                 //std::cout<<"drawables : " << tempGeode->getNumDrawables() <<std::endl;
00113                 int i;
00114                 for (i = 0; i < tempGeode->getNumDrawables(); i ++)
00115                 {
00116                         osg::Geometry* tempGeometry =dynamic_cast<osg::Geometry*>(tempGeode->getDrawable(i));
00117                         if (tempGeometry != NULL)
00118                         {
00119                                 osg::Vec3Array* vertices =dynamic_cast<osg::Vec3Array*>(tempGeometry->getVertexArray());
00120                                 int numVertices = vertices->getNumElements();
00121                                 osg::FloatArray* coords = new osg::FloatArray(numVertices);
00122                                 for (int i=0; i < numVertices; i ++)
00123                                 {
00124                                         float offset = (zmax - (*vertices)[i].z());
00125                                         (*coords)[i] = 0.0;
00126                                         if(isInTheCube((*vertices)[i].x(),(*vertices)[i].y(),(*vertices)[i].z())) 
00127                                                 (*coords)[i] = offset;
00128                                         //std::cout<<"coord : " << offset <<std::endl;
00129                                 }
00130 
00131                                 tempGeometry->setFogCoordArray(coords);
00132                                 tempGeometry->setFogCoordBinding(osg::Geometry::BIND_PER_VERTEX);
00133                         }
00134                 }
00135                 return;
00136         }
00137 
00138         osg::Group* tempGroup = dynamic_cast<osg::Group*>(node);
00139         if(tempGroup!= NULL)
00140         {
00141                 std::cout<<"gr : " << tempGroup->getNumChildren() <<std::endl;
00142                 int i;
00143                 for (i = 0; i < tempGroup->getNumChildren(); i ++)
00144                 {
00145                         addDynamicFogCoords(tempGroup->getChild(i));
00146                 }
00147                 return;
00148         }
00149 
00150         int temp = 1;
00151 }
00152 bool FogCube::addFog(osg::Node* node)
00153 {
00154         this->node=node;
00155 
00156         osg::StateSet* stateset = node->getOrCreateStateSet();
00157         stateset->setAttribute(this, osg::StateAttribute::ON);
00158         stateset->setMode(GL_FOG, osg::StateAttribute::ON);
00159 
00160         return true;
00161 }
00162 
00164 osg::Node*  FogCube::createMultiplesDrawables()
00165 {
00166         if(node==NULL)
00167                 return node;
00168         
00169         osg::Geode* geode = new osg::Geode();
00170         
00171         // ---------------------------------------
00172         // Recover the stateset of the main element
00173         // ---------------------------------------
00174         osg::StateSet* stateset = node->getOrCreateStateSet();
00175         geode->setStateSet( stateset );
00176         
00177         osg::Geode* tempGeode = dynamic_cast<osg::Geode*>(node);
00178         
00179         if(tempGeode!= NULL)
00180         {
00181                 std::cout<< "nb drawables : " << tempGeode->getNumDrawables() << std::endl;
00182                 
00183                 osg::Drawable * drw = tempGeode->getDrawable(0);
00184                 osg::Geometry * geom = drw->asGeometry();
00185                 
00186                 std::cout<< "nb geom :  " << geom->getNumPrimitiveSets() << std::endl;
00187                 std::cout<< "PS 1  :  " << geom->getPrimitiveSet(0)->className()<< geom->getPrimitiveSet(0)->getNumPrimitives() << std::endl;
00188                 
00189                 for(unsigned int i=0;i<geom->getPrimitiveSet(0)->getNumPrimitives();i++)
00190                 {
00191                                 
00192                 }
00193                 
00194                 /*
00195                 osg::Geometry* geometry = new osg::Geometry;
00196 
00197                 osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows));
00198                 osg::Vec2Array& t = *(new osg::Vec2Array(numColumns*numRows));
00199         //osg::UByte4Array& color = *(new osg::UByte4Array(1));   
00200          
00201                 osg::Vec4Array* color = new osg::Vec4Array(1);
00202                 (*color)[0].set(1.0f,1.0f,1.0f,1.0f);
00203         
00204 
00205                 float rowCoordDelta = size.y()/(float)(numRows-1);
00206                 float columnCoordDelta = size.x()/(float)(numColumns-1);
00207 
00208                 float rowTexDelta = 1.0f/(float)(numRows-1);
00209                 float columnTexDelta = 1.0f/(float)(numColumns-1);
00210 
00211                 osg::Vec3 pos = origin;
00212                 osg::Vec2 tex(0.0f,0.0f);
00213                 int vi=0;
00214                 for(r=0;r<numRows;++r)
00215                 {
00216                         pos.x() = origin.x();
00217                         tex.x() = 0.0f;
00218                         for(c=0;c<numColumns;++c)
00219                         {
00220 
00221                                 v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z);
00222                                 t[vi].set(tex.x(),tex.y());
00223                                 pos.x()+=columnCoordDelta;
00224                                 tex.x()+=columnTexDelta;
00225                                 ++vi;
00226                         }
00227                         pos.y() += rowCoordDelta;
00228                         tex.y() += rowTexDelta;
00229                 }
00230 
00231                 geometry->setVertexArray(&v);
00232                 geometry->setColorArray(color);
00233                 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
00234                 geometry->setTexCoordArray(0,&t);
00235 
00236                 for(r=0;r<numRows-1;++r)
00237                 {
00238                         osg::DrawElementsUShort& drawElements = *(new
00239                                         osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns));
00240                         geometry->addPrimitiveSet(&drawElements);
00241                         int ei=0;
00242                         for(c=0;c<numColumns;++c)
00243                         {
00244                                 drawElements[ei++] = (r+1)*numColumns+c;
00245                                 drawElements[ei++] = (r)*numColumns+c;
00246                         }
00247                 }
00248 
00249                 geode->addDrawable(geometry);
00250 
00251                 osgUtil::SmoothingVisitor sv;
00252                 sv.smooth(*geometry);
00253                 
00254                 */
00255                 
00256         }
00257 
00258         //node=geode;
00259         return node;//geode;
00260 }

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