00001 #include "DeformableOctreeShapeNode.h"
00002
00003 #include <X3DTK/private/X3D_Material.h>
00004 #include <GL/gl.h>
00005
00006 #include "ConstrainedVertex.h"
00007 #include "SFVec3fCellConstrained.h"
00008
00009
00010
00011 namespace X3DTK
00012 {
00013
00015
00016 namespace X3D
00017 {
00018
00019
00020
00021 DeformableOctreeShapeNode::DeformableOctreeShapeNode() :
00022 _coordinate(NULL), _ifs(NULL), _appearance(NULL), _octree(NULL)
00023 , _renderingMethod( DOSN_METHOD_VA )
00024
00025
00026 {
00027
00028 define(Recorder<DeformableOctreeShapeNode>::getTypeName("DeformableOctreeShapeNode"));
00029 }
00030
00031 DeformableOctreeShapeNode::~DeformableOctreeShapeNode()
00032 {
00033
00034
00035
00036
00037
00038
00039 }
00040
00041
00042 void DeformableOctreeShapeNode::postInit()
00043 {
00044 std::cerr << "DOSN::postInit()\n";
00045
00046 MFNode children = getChildList();
00047
00048 for( MFNode::iterator it = children.begin() ; it != children.end() ; ++it )
00049 {
00050
00051
00052 if ( IndexedFaceSet *tmpifs = dynamic_cast<IndexedFaceSet *>(*it) )
00053 {
00054 std::cerr << "Got IFS !\n";
00055 Require( !_ifs );
00056
00057 if( ! tmpifs->getCoord() )
00058 {
00059 std::cerr << "Can't get coord\n";
00060 }
00061
00062 setIFS( tmpifs );
00063 }
00064 else if ( Appearance* tmpapp = dynamic_cast<Appearance *>(*it) )
00065 {
00066 std::cerr << "Got appareance\n";
00067 Require( !_appearance );
00068
00069 _appearance = tmpapp;
00070 }
00071 }
00072 }
00073
00074
00075 void DeformableOctreeShapeNode::setIFS( IndexedFaceSet *ifs )
00076 {
00077 _ifs = ifs;
00078 _coordinate = dynamic_cast<Coordinate*>(_ifs->getCoord());
00079 }
00080
00081
00082
00083
00084
00085 X3D::Coordinate* DeformableOctreeShapeNode::getCoordinate() const
00086 {
00087 return _coordinate;
00088 }
00089
00090
00091
00092
00093 void DeformableOctreeShapeNode::setRenderingMethod( int method )
00094 {
00095 _renderingMethod = method;
00096 }
00097 int DeformableOctreeShapeNode::getRenderingMethod( ) const
00098 {
00099 return _renderingMethod;
00100 }
00101
00102
00103 void DeformableOctreeShapeNode::setOctree( animal::octree::Octree *octree )
00104 {
00105 Require( _ifs );
00106 _octree = octree;
00107
00108 }
00109
00110
00111 MFVec3f DeformableOctreeShapeNode::computeNormals() const
00112 {
00113 Require( _coordinate );
00114 Require( _ifs );
00115
00116
00117 const MFInt32 coordIndex = _ifs->getCoordIndex();
00118
00119
00120 MFVec3f normals(coordIndex.size());
00121
00122 MFInt32 nTriangles(coordIndex.size());
00123
00124 unsigned short nVertices = 0;
00125
00126
00127
00128
00129
00130 for( MFInt32::const_iterator it = coordIndex.begin() ; it!=coordIndex.end() ; ++it )
00131 {
00132 if( nVertices == 0 )
00133 {
00134
00135 const SFVec3f vec0 = _coordinate->getPoint()[*(it+0)];
00136 const SFVec3f vec1 = _coordinate->getPoint()[*(it+1)];
00137 const SFVec3f vec2 = _coordinate->getPoint()[*(it+2)];
00138
00139 const SFVec3f normal = crossprod(vec1-vec0,vec2-vec0);
00140 normals[*(it+0)] += normal;
00141 normals[*(it+1)] += normal;
00142 normals[*(it+2)] += normal;
00143
00144 nTriangles[*(it+0)]++;
00145 nTriangles[*(it+1)]++;
00146 nTriangles[*(it+2)]++;
00147
00148 }
00149 nVertices++;
00150
00151 if( *it == -1 )
00152 {
00153 nVertices = 0;
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 for( MFVec3f::iterator itNorm = normals.begin() ; itNorm != normals.end() ; ++itNorm )
00174 {
00175 (*itNorm).normalize();
00176 }
00177
00178
00179 return normals;
00180 }
00181
00182
00183 void DeformableOctreeShapeNode::draw() const
00184 {
00185
00186
00187 if( _coordinate )
00188 {
00189 glPushAttrib( GL_ALL_ATTRIB_BITS );
00190
00191 glEnable( GL_LIGHTING );
00192 glEnable( GL_NORMALIZE );
00193 glDisable( GL_COLOR_MATERIAL );
00194 glEnable( GL_SMOOTH );
00195
00196 if( _appearance )
00197 {
00198 Material * mat = (Material*)_appearance->getMaterial();
00199
00200 float myVec3[3];
00201 myVec3[0] = 0;
00202 myVec3[1] = 0;
00203 myVec3[2] = 0;
00204
00205 SFColor myColor;
00206 SFFloat myFloat;
00207
00208 if( mat )
00209 {
00210 myColor = mat->getDiffuseColor();
00211 myVec3[0] = myColor.r;
00212 myVec3[1] = myColor.g;
00213 myVec3[2] = myColor.b;
00214 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, myVec3 );
00215
00216 myColor = mat->getSpecularColor();
00217 myVec3[0] = myColor.r;
00218 myVec3[1] = myColor.g;
00219 myVec3[2] = myColor.b;
00220 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, myVec3 );
00221
00222 myFloat = mat->getShininess();
00223 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, myFloat );
00224 }
00225 }
00226
00227 if( _renderingMethod != DOSN_METHOD_CG )
00228 {
00229
00230 MFVec3f normals = computeNormals();
00231
00232
00233 const MFInt32 coordIndex = _ifs->getCoordIndex();
00234
00235 if( _renderingMethod == DOSN_METHOD_VA )
00236 {
00237
00238
00239
00240 GLuint myGlIndex[ coordIndex.size() ];
00241 GLfloat myGlVertex[ 3*_coordinate->getPoint().size() ];
00242 GLfloat myGlNormal[ 3*normals.size() ];
00243
00244 unsigned int i;
00245
00246 i=0;
00247 for( MFInt32::const_iterator it = coordIndex.begin() ; it != coordIndex.end() ; ++it )
00248 {
00249
00250 if( *it != -1 )
00251 {
00252 myGlIndex[ i ] = *it;
00253 ++i;
00254 }
00255 }
00256 unsigned int nTriangleVertices = i;
00257
00258 i=0;
00259 for( MFVec3f::iterator it = normals.begin() ; it != normals.end() ; ++it )
00260 {
00261 myGlNormal[ i++ ] = (*it)[0];
00262 myGlNormal[ i++ ] = (*it)[1];
00263 myGlNormal[ i++ ] = (*it)[2];
00264 }
00265
00266 i=0;
00267 for( MFVec3f::const_iterator it = _coordinate->getPoint().begin() ; it != _coordinate->getPoint().end() ; ++it )
00268 {
00269 myGlVertex[ i++ ] = (*it)[0];
00270 myGlVertex[ i++ ] = (*it)[1];
00271 myGlVertex[ i++ ] = (*it)[2];
00272 }
00273
00274
00275 glEnableClientState(GL_VERTEX_ARRAY);
00276 glVertexPointer( 3, GL_FLOAT, 0, myGlVertex );
00277
00278 glEnableClientState(GL_NORMAL_ARRAY);
00279 glNormalPointer( GL_FLOAT, 0, myGlNormal );
00280
00281 glDrawElements( GL_TRIANGLES, nTriangleVertices, GL_UNSIGNED_INT, myGlIndex );
00282
00283 }
00284 else if( _renderingMethod == DOSN_METHOD_BOURRIN )
00285 {
00286
00287 glBegin( GL_POLYGON );
00288 for( MFInt32::const_iterator it = coordIndex.begin() ; it!=coordIndex.end() ; ++it )
00289 {
00290 if( *it == -1 )
00291 {
00292 glEnd();
00293 glBegin( GL_POLYGON );
00294 }
00295 else
00296 {
00297 const SFVec3f normal = normals[*it];
00298 const SFVec3f vec = _coordinate->getPoint()[*it];
00299 glNormal3f( normal[0], normal[1], normal[2] );
00300 glVertex3f( vec[0], vec[1], vec[2] );
00301 }
00302 }
00303 glEnd();
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 }
00315 else
00316 {
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 }
00370
00371 glPopAttrib();
00372 }
00373 }
00374
00375
00376 }
00377 }