00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
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
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
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
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 }
00257
00258
00259 return node;
00260 }