Changeset 13041 for OpenSceneGraph/trunk/src/osgPlugins/ply/vertexData.cpp
- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/ply/vertexData.cpp
r12292 r13041 1 /* 1 /* 2 2 vertexData.cpp 3 3 Copyright (c) 2007, Tobias Wolf <twolf@access.unizh.ch> 4 All rights reserved. 5 4 All rights reserved. 5 6 6 Implementation of the VertexData class. 7 7 */ … … 53 53 54 54 /* Read the vertex and (if available/wanted) color data from the open file. */ 55 void VertexData::readVertices( PlyFile* file, const int nVertices, 55 void VertexData::readVertices( PlyFile* file, const int nVertices, 56 56 const int fields ) 57 57 { … … 81 81 } vertex; 82 82 83 PlyProperty vertexProps[] = 83 PlyProperty vertexProps[] = 84 84 { 85 85 { "x", PLY_FLOAT, PLY_FLOAT, offsetof( _Vertex, x ), 0, 0, 0, 0 }, … … 104 104 { "specular_power", PLY_FLOAT, PLY_FLOAT, offsetof( _Vertex, specular_power ), 0, 0, 0, 0 }, 105 105 }; 106 106 107 107 // use all 6 properties when reading colors, only the first 3 otherwise 108 for( int i = 0; i < 3; ++i ) 108 for( int i = 0; i < 3; ++i ) 109 109 ply_get_property( file, "vertex", &vertexProps[i] ); 110 110 111 111 if (fields & NORMALS) 112 for( int i = 3; i < 6; ++i ) 112 for( int i = 3; i < 6; ++i ) 113 113 ply_get_property( file, "vertex", &vertexProps[i] ); 114 114 115 115 if (fields & RGB) 116 for( int i = 6; i < 9; ++i ) 116 for( int i = 6; i < 9; ++i ) 117 117 ply_get_property( file, "vertex", &vertexProps[i] ); 118 118 119 119 if (fields & AMBIENT) 120 for( int i = 9; i < 12; ++i ) 120 for( int i = 9; i < 12; ++i ) 121 121 ply_get_property( file, "vertex", &vertexProps[i] ); 122 122 123 123 if (fields & DIFFUSE) 124 for( int i = 12; i < 15; ++i ) 124 for( int i = 12; i < 15; ++i ) 125 125 ply_get_property( file, "vertex", &vertexProps[i] ); 126 126 127 127 if (fields & SPECULAR) 128 for( int i = 15; i < 20; ++i ) 128 for( int i = 15; i < 20; ++i ) 129 129 ply_get_property( file, "vertex", &vertexProps[i] ); 130 130 131 131 // check whether array is valid otherwise allocate the space 132 132 if(!_vertices.valid()) 133 _vertices = new osg::Vec3Array; 133 _vertices = new osg::Vec3Array; 134 134 135 135 if( fields & NORMALS ) … … 138 138 _normals = new osg::Vec3Array; 139 139 } 140 140 141 141 // If read colors allocate space for color array 142 142 if( fields & RGB ) … … 157 157 _diffuse = new osg::Vec4Array; 158 158 } 159 159 160 160 if( fields & SPECULAR ) 161 161 { … … 203 203 } face; 204 204 205 PlyProperty faceProps[] = 206 { 207 { "vertex_indices", PLY_INT, PLY_INT, offsetof( _Face, vertices ), 205 PlyProperty faceProps[] = 206 { 207 { "vertex_indices", PLY_INT, PLY_INT, offsetof( _Face, vertices ), 208 208 1, PLY_UCHAR, PLY_UCHAR, offsetof( _Face, nVertices ) } 209 209 }; 210 210 211 211 ply_get_property( file, "face", &faceProps[0] ); 212 212 213 213 //triangles.clear(); 214 214 //triangles.reserve( nFaces ); … … 216 216 _triangles = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); 217 217 218 218 219 219 // read in the faces, asserting that they are only triangles 220 220 int ind1 = _invertFaces ? 2 : 0; … … 234 234 _triangles->push_back( face.vertices[1]); 235 235 _triangles->push_back( face.vertices[ind3] ); 236 236 237 237 // free the memory that was allocated by ply_get_element 238 238 free( face.vertices ); … … 251 251 int nComments; 252 252 char** comments; 253 253 254 254 PlyFile* file = NULL; 255 255 256 256 // Try to open ply file as for reading 257 257 try{ 258 file = ply_open_for_reading( const_cast< char* >( filename ), 259 &nPlyElems, &elemNames, 258 file = ply_open_for_reading( const_cast< char* >( filename ), 259 &nPlyElems, &elemNames, 260 260 &fileType, &version ); 261 261 } … … 263 263 catch( exception& e ) 264 264 { 265 MESHERROR << "Unable to read PLY file, an exception occured: " 265 MESHERROR << "Unable to read PLY file, an exception occured: " 266 266 << e.what() << endl; 267 267 } … … 269 269 if( !file ) 270 270 { 271 MESHERROR << "Unable to open PLY file " << filename 271 MESHERROR << "Unable to open PLY file " << filename 272 272 << " for reading." << endl; 273 273 return NULL; … … 275 275 276 276 MESHASSERT( elemNames != 0 ); 277 277 278 278 279 279 nComments = file->num_comments; 280 280 comments = file->comments; 281 282 281 282 283 283 #ifndef NDEBUG 284 MESHINFO << filename << ": " << nPlyElems << " elements, file type = " 284 MESHINFO << filename << ": " << nPlyElems << " elements, file type = " 285 285 << fileType << ", version = " << version << endl; 286 286 #endif … … 298 298 int nElems; 299 299 int nProps; 300 300 301 301 PlyProperty** props = NULL; 302 302 try{ 303 props = ply_get_element_description( file, elemNames[i], 303 props = ply_get_element_description( file, elemNames[i], 304 304 &nElems, &nProps ); 305 305 } 306 306 catch( exception& e ) 307 307 { 308 MESHERROR << "Unable to get PLY file description, an exception occured: " 308 MESHERROR << "Unable to get PLY file description, an exception occured: " 309 309 << e.what() << endl; 310 310 } 311 311 MESHASSERT( props != 0 ); 312 312 313 313 #ifndef NDEBUG 314 314 MESHINFO << "element " << i << ": name = " << elemNames[i] << ", " … … 320 320 } 321 321 #endif 322 322 323 323 // if the string is vertex means vertex data is started 324 324 if( equal_strings( elemNames[i], "vertex" ) ) … … 349 349 } 350 350 351 try { 351 try { 352 352 // Read vertices and store in a std::vector array 353 353 readVertices( file, nElems, fields ); … … 381 381 catch( exception& e ) 382 382 { 383 MESHERROR << "Unable to read vertex in PLY file, an exception occured: " 383 MESHERROR << "Unable to read vertex in PLY file, an exception occured: " 384 384 << e.what() << endl; 385 385 // stop for loop by setting the loop variable to break condition 386 386 // this way resources still get released even on error cases 387 387 i = nPlyElems; 388 388 389 389 } 390 390 } … … 401 401 catch( exception& e ) 402 402 { 403 MESHERROR << "Unable to read PLY file, an exception occured: " 403 MESHERROR << "Unable to read PLY file, an exception occured: " 404 404 << e.what() << endl; 405 405 // stop for loop by setting the loop variable to break condition … … 407 407 i = nPlyElems; 408 408 } 409 409 410 410 // free the memory that was allocated by ply_get_element_description 411 411 for( int j = 0; j < nProps; ++j ) … … 413 413 free( props ); 414 414 } 415 415 416 416 ply_close( file ); 417 417 418 418 // free the memory that was allocated by ply_open_for_reading 419 419 for( int i = 0; i < nPlyElems; ++i ) … … 443 443 geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); 444 444 } 445 445 446 446 // Add the primitive set 447 447 if (_triangles.valid() && _triangles->size() > 0 ) … … 478 478 // set flage true to activate the vertex buffer object of drawable 479 479 geom->setUseVertexBufferObjects(true); 480 480 481 481 482 482 osg::Geode* geode = new osg::Geode; … … 484 484 return geode; 485 485 } 486 486 487 487 return NULL; 488 488 } … … 502 502 if(!_normals.valid()) 503 503 { 504 _normals = new osg::Vec3Array; 505 } 506 504 _normals = new osg::Vec3Array; 505 } 506 507 507 //normals.clear(); 508 508 if( vertexNormals ) … … 515 515 } 516 516 517 517 518 518 for( size_t i = 0; i < ((_triangles->size())); i += 3 ) 519 519 { … … 527 527 (*_vertices)[i1], 528 528 (*_vertices)[i2] ); 529 529 530 530 // count emtpy normals in debug mode 531 531 #ifndef NDEBUG … … 533 533 ++wrongNormals; 534 534 #endif 535 535 536 536 if( vertexNormals ) 537 537 { 538 (*_normals)[i0] += triangleNormal.triNormal; 539 (*_normals)[i1] += triangleNormal.triNormal; 538 (*_normals)[i0] += triangleNormal.triNormal; 539 (*_normals)[i1] += triangleNormal.triNormal; 540 540 (*_normals)[i2] += triangleNormal.triNormal; 541 541 } 542 542 else 543 _normals->push_back( triangleNormal.triNormal ); 544 } 545 543 _normals->push_back( triangleNormal.triNormal ); 544 } 545 546 546 // normalize all the normals 547 547 if( vertexNormals ) 548 548 for( size_t i = 0; i < _normals->size(); ++i ) 549 549 (*_normals)[i].normalize(); 550 550 551 551 #ifndef NDEBUG 552 552 if( wrongNormals > 0 ) 553 553 MESHINFO << wrongNormals << " faces had no valid normal." << endl; 554 #endif 554 #endif 555 555 } 556 556
