Changeset 1001 for GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
- Timestamp:
- 06/05/06 23:14:04 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
r971 r1001 29 29 #include "Triangle3.h" 30 30 #include "ViewCellsManager.h" 31 #include "ResourceManager.h" 31 32 32 33 namespace GtpVisibilityPreprocessor { … … 60 61 #define ROTATE_SCENE 0 61 62 62 63 static void RotateMesh(Mesh *mesh) 63 static int sUniqueMeshIdx = 0; 64 65 // hack: rotate mesh by n degrees 66 static void RotateMesh(Mesh *mesh, const float angle) 64 67 { 65 68 VertexContainer::iterator it, it_end = mesh->mVertices.end(); 66 69 67 const float angle = 30.0f* PI / 180.0f;68 const Matrix4x4 rot = RotationYMatrix(angle );70 const float angleRad = angle * PI / 180.0f; 71 const Matrix4x4 rot = RotationYMatrix(angleRad); 69 72 70 73 for (it = mesh->mVertices.begin(); it != it_end; ++ it) … … 83 86 , mSpaceCount(0) 84 87 , mLoadPolygonsAsMeshes(loadPolygonsAsMeshes) 85 { 86 mCurrentNode = root; 87 // this matrix should never be removed from stack 88 //mTransformations.push(IdentityMatrix()); 88 , mCurrentMesh(NULL) 89 { 90 mCurrentNode = root; 91 92 // this matrix should never be removed from stack 93 //mTransformations.push(IdentityMatrix()); 89 94 } 90 95 … … 99 104 // StdInParseHandlers: Implementation of the SAX DocumentHandler interface 100 105 // --------------------------------------------------------------------------- 106 101 107 void X3dParseHandlers::endElement(const XMLCh* const name) 102 108 { 103 StrX lname(name); 104 string element(lname.LocalForm()); 105 106 // only create new mesh instance if define mechanism was not used 107 if (element == "Shape") 108 EndShape(); 109 110 if (element == "Transform") 111 EndTransform(); 112 } 113 114 115 116 117 void X3dParseHandlers::ApplyTransformation(Mesh *mesh, 118 const Matrix4x4 &m) const 119 { 120 VertexContainer::iterator it, it_end = mesh->mVertices.end(); 121 122 for (it = mesh->mVertices.begin(); it != it_end; ++ it) 123 { 124 (*it) = m * (*it); 125 } 109 StrX lname(name); 110 string element(lname.LocalForm()); 111 112 // only create new mesh instance if define mechanism was not used 113 if (element == "Shape") 114 EndShape(); 115 116 if (element == "Transform") 117 EndTransform(); 126 118 } 127 119 … … 134 126 trafos.pop(); 135 127 136 ApplyTransformation(mesh, m); 137 } 138 } 128 mesh->ApplyTransformation(m); 129 } 130 } 131 132 133 void X3dParseHandlers::ApplyTransformations(TrafoStack trafos, 134 TransformedMeshInstance *mi) const 135 { 136 while (!trafos.empty()) 137 { 138 const Matrix4x4 m = trafos.top(); 139 trafos.pop(); 140 141 mi->ApplyWorldTransform(m); 142 } 143 } 144 139 145 140 146 void X3dParseHandlers::StartTransform(AttributeList& attributes) … … 143 149 144 150 const int len = attributes.getLength(); 151 145 152 Matrix4x4 *rotm = NULL; 146 153 Matrix4x4 *scalem = NULL; … … 208 215 void X3dParseHandlers::EndShape() 209 216 { 210 // this shape is a definition => don't create mesh instance 211 if (mIsMeshDefinition) 212 { 213 mMeshDefinitions[mCurrentMeshName.c_str()] = mCurrentMesh; 214 //cout << "new definition: " << mCurrentMeshName << endl; 217 //-- if shape is merely a definition => don't create particular mesh instance 218 if (!mCurrentMesh || mIsMeshDefinition) 219 { 220 return; 221 } 222 223 //-- each polygon is one single mesh 224 if (mLoadPolygonsAsMeshes) 225 { 226 FaceContainer::const_iterator fit, fit_end = mCurrentMesh->mFaces.end(); 227 228 cout << "m"; 215 229 216 return; 217 } 218 219 // each polygon is one single mesh 220 if (mLoadPolygonsAsMeshes) 221 { 222 //if (mCurrentMesh->mFaces.empty()) cout << "error!" << endl; 223 224 FaceContainer::const_iterator fit, fit_end = mCurrentMesh->mFaces.end(); 225 226 cout << "m"; 227 //cout << "m: " << mCurrentMesh->mFaces.size() << endl; 228 for (fit = mCurrentMesh->mFaces.begin(); fit != fit_end; ++ fit) 229 { 230 cout << "f"; 231 232 Face *face = *fit; 233 // only one face per mesh 234 Mesh *mesh = new Mesh(); 235 VertexIndexContainer vc; 236 237 VertexIndexContainer::const_iterator vit, vit_end = face->mVertexIndices.end(); 230 for (fit = mCurrentMesh->mFaces.begin(); fit != fit_end; ++ fit) 231 { 232 cout << "f"; 233 234 Face *face = *fit; 235 236 // we create a new mesh for each face from the current mesh 237 Mesh *mesh = MeshManager::GetSingleton()->CreateResource(); 238 238 239 int i = 0; 240 for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit, ++ i) 239 VertexIndexContainer::const_iterator vit, vit_end = face->mVertexIndices.end(); 240 241 int i = 0; 242 VertexIndexContainer vcIndices; 243 244 for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit, ++ i) 245 { 246 cout << "i"; 247 const int index = (*vit); 248 249 // add vertices 250 mesh->mVertices.push_back(mCurrentMesh->mVertices[index]); 251 252 // indices don't make much sense if mesh = face, but 253 // we need them anyway ... 254 vcIndices.push_back(i); 255 } 256 257 mesh->mFaces.push_back(new Face(vcIndices)); 258 259 // NOTE: should rather be written into trafo of mesh instance 260 ApplyTransformations(mTransformations, mesh); 261 262 mesh->Preprocess(); 263 264 // make an instance of this mesh 265 MeshInstance *mi = new MeshInstance(mesh); 266 mCurrentNode->mGeometry.push_back(mi); 267 } 268 269 // this mesh is not needed, unless it is used as a definition 270 if (!mUsingMeshDefinition) 271 MeshManager::GetSingleton()->DestroyEntry(mCurrentMesh->GetId()); 272 } 273 else // default usage: create a mesh instance from the current mesh 274 { 275 MeshInstance *mi; 276 277 if (0 && !mUsingMeshDefinition) 278 { 279 // make an instance of this mesh 280 mi = new MeshInstance(mCurrentMesh); 281 282 // this mesh is used only once => write transformations directly into it 283 ApplyTransformations(mTransformations, mCurrentMesh); 284 } 285 else 286 { 287 // make an instance of this mesh 288 TransformedMeshInstance *tmi = new TransformedMeshInstance(mCurrentMesh); 289 290 // apply transformation on the instance of the mesh 291 ApplyTransformations(mTransformations, tmi); 292 293 mi = tmi; 294 } 295 296 if (mCurrentMaterial) 297 { 298 // HACK: add the material to the mesh directly if no material yet 299 if (mCurrentMesh->mMaterial) 241 300 { 242 cout << "i"; 243 int index = (*vit); 244 // add vertices 245 mesh->mVertices.push_back(mCurrentMesh->mVertices[index]); 246 vc.push_back(i); 301 mCurrentMesh->mMaterial = mCurrentMaterial; 247 302 } 248 249 mesh->mFaces.push_back(new Face(vc)); 250 251 // NOTE: should rather be written into trafo of mesh instance 252 ApplyTransformations(mTransformations, mesh); 253 254 mesh->Preprocess(); 255 256 // make an instance of this mesh 257 MeshInstance *mi = new MeshInstance(mesh); 258 mCurrentNode->mGeometry.push_back(mi); 259 } 260 261 // LEAK!! TODO: delete if not defd 262 if (!mUsingMeshDefinition) 263 delete mCurrentMesh; 264 265 //mCurrentVertices.clear(); 266 //mCurrentVertexIndices.clear(); 267 } 268 else 269 { 270 if (!mCurrentMesh->mFaces.empty()) 271 { 272 // should rather be written into the transformation 273 // of a mesh instance 274 ApplyTransformations(mTransformations, mCurrentMesh); 275 276 mCurrentMesh->Preprocess(); 277 // make an instance of this mesh 278 MeshInstance *mi = new MeshInstance(mCurrentMesh); 279 280 mCurrentNode->mGeometry.push_back(mi); 281 // set the object id to a unique value 282 //mi->SetId(mCurrentObjectId ++); 283 } 284 else 285 { 286 // empty mesh => discard 287 cout<<"X"; 288 289 delete mCurrentMesh; 290 } 291 303 else // add material to the instance 304 { 305 mi->SetMaterial(mCurrentMaterial); 306 } 307 } 308 309 // create local mesh kd tree 310 mCurrentMesh->Preprocess(); 311 // add to scene graph 312 mCurrentNode->mGeometry.push_back(mi); 313 314 // reset current mesh 292 315 mCurrentMesh = NULL; 293 316 } … … 297 320 void X3dParseHandlers::StartIndexedFaceSet(AttributeList& attributes) 298 321 { 299 int len = attributes.getLength(); 300 301 int i; 302 322 //-- indexedfaceset corresponds to Mesh in our implementation 323 const int len = attributes.getLength(); 324 325 VertexIndexContainer vertices; 303 326 304 327 mIsMeshDefinition = false; 305 328 mUsingMeshDefinition = false; 306 for (i = 0; i < len; ++ i) 329 330 for (int i = 0; i < len; ++ i) 307 331 { 308 string attrName(StrX(attributes.getName(i)).LocalForm()); 309 310 // this is a definition of a mesh 311 if (attrName == "DEF") 312 { 332 const string attrName(StrX(attributes.getName(i)).LocalForm()); 333 334 //-- we use an already defined mesh 335 if (attrName == "USE") 336 { 337 StrX attrValue(attributes.getValue(i)); 338 const char *meshName = attrValue.LocalForm(); 339 340 mUsingMeshDefinition = true; 341 342 // retrieve mesh from mesh container 343 const int meshIdx = mMeshDefinitions[meshName]; 344 345 mCurrentMesh = 346 MeshManager::GetSingleton()->FindEntry(meshIdx); 347 348 //Debug << "retrieving mesh definition: " << mCurrentMeshName << endl; 349 cout << "u"; 350 } 351 else if (attrName == "DEF") //-- a definition of a mesh 352 { 353 const StrX attrValue(attributes.getValue(i)); 354 const char *meshName = attrValue.LocalForm(); 355 356 // this is only a definition, don't create actual instance 313 357 mIsMeshDefinition = true; 358 359 //-- create new mesh definition 360 mCurrentMesh = MeshManager::GetSingleton()->CreateResource(); 361 362 // store the mesh defination in a lookup table 363 mMeshDefinitions[meshName] = mCurrentMesh->GetId(); 364 cout << "d"; 365 } 366 367 //-- read coordinate indices for current mesh 368 else if (attrName == "coordIndex") 369 { 314 370 StrX attrValue(attributes.getValue(i)); 315 371 const char *ptr = attrValue.LocalForm(); 316 mCurrentMeshName = ptr; 317 318 cout << "d"; 319 } 320 321 // we use an already defined mesh 322 if (attrName == "USE") 323 { 324 StrX attrValue(attributes.getValue(i)); 325 326 // discard new mesh and assign defined mesh 327 DEL_PTR(mCurrentMesh); 328 const char *ptr = attrValue.LocalForm(); 329 330 mCurrentMesh = mMeshDefinitions[ptr]; 331 mUsingMeshDefinition = true; 332 cout << "u"; 333 } 334 335 if (attrName == "coordIndex") 336 { 337 StrX attrValue(attributes.getValue(i)); 372 373 //-- immediate use: create a new mesh using a generic name 374 if (!mCurrentMesh) 375 { 376 mCurrentMesh = MeshManager::GetSingleton()->CreateResource(); 377 } 378 338 379 // handle coordIndex 339 380 vertices.clear(); 340 const char *ptr = attrValue.LocalForm();381 341 382 342 383 char *endptr; … … 372 413 373 414 void 374 X3dParseHandlers::StartMaterial( 375 AttributeList& attributes) 376 { 377 int len = attributes.getLength(); 378 int i; 379 if (!mCurrentMesh->mMaterial) 380 mCurrentMesh->mMaterial = new Material; 381 for (i=0; i < len; i++) { 382 string attrName(StrX(attributes.getName(i)).LocalForm()); 383 StrX attrValue(attributes.getValue(i)); 384 const char *ptr = attrValue.LocalForm(); 385 if (attrName == "diffuseColor") { 386 float r, g, b; 387 if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3) 388 mCurrentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b); 389 } 390 } 391 } 392 393 void 394 X3dParseHandlers::StartCoordinate( 395 AttributeList& attributes) 396 { 397 int len = attributes.getLength(); 415 X3dParseHandlers::StartMaterial(AttributeList& attributes) 416 { 417 const int len = attributes.getLength(); 418 419 mCurrentMaterial = MaterialManager::GetSingleton()->CreateResource(); 420 421 for (int i = 0; i < len; ++ i) 422 { 423 const string attrName(StrX(attributes.getName(i)).LocalForm()); 424 425 const StrX attrValue(attributes.getValue(i)); 426 const char *ptr = attrValue.LocalForm(); 427 428 429 //-- we use an already defined material 430 if (attrName == "USE") 431 { 432 //mUsingMaterialDefinition = true; 433 string matName(ptr); 434 435 // retrieve mesh from mesh container 436 const int matIdx = mMaterialDefinitions[matName]; 437 438 mCurrentMaterial = 439 MaterialManager::GetSingleton()->FindEntry(matIdx); 440 441 //Debug << "retrieving mesh definition: " << mCurrentMeshName << endl; 442 cout << "u"; 443 } 444 else if (attrName == "DEF") //-- a definition of a material 445 { 446 //mIsMaterialDefinition = true; 447 string matName(ptr); 448 449 //-- create new material definition 450 mCurrentMaterial = MaterialManager::GetSingleton()->CreateResource(); 451 // store the mesh defination in a lookup table 452 mMaterialDefinitions[matName] = mCurrentMaterial->GetId(); 453 cout << "d"; 454 } 455 // TODO: support not only diffuse material 456 else if (attrName == "diffuseColor") 457 { 458 float r, g, b; 459 460 if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3) 461 mCurrentMaterial->mDiffuseColor = RgbColor(r, g, b); 462 } 463 } 464 } 465 466 467 void 468 X3dParseHandlers::StartCoordinate(AttributeList& attributes) 469 { 470 const int len = attributes.getLength(); 398 471 399 472 int i; … … 402 475 for (i=0; i < len; i++) 403 476 { 404 string attrName(StrX(attributes.getName(i)).LocalForm());477 const string attrName(StrX(attributes.getName(i)).LocalForm()); 405 478 406 479 if (attrName == "point") … … 441 514 vertices.push_back(v); 442 515 } 443 516 517 // substitute vertices into current mesh 444 518 mCurrentMesh->mVertices = vertices; 445 519 } … … 456 530 457 531 if (element == "IndexedFaceSet") { 458 459 532 // create a new mesh node in the scene graph 533 StartIndexedFaceSet(attributes); 460 534 } 461 535 462 536 if (element == "Shape") { 463 cout << "+"; 464 mCurrentMesh = new Mesh; 537 cout << "+"; 538 539 // reset current shape values 540 mCurrentMesh = NULL; 541 mCurrentMaterial = NULL; 542 mCurrentVertexIndices.clear(); 543 //mCurrentVertices.clear(); 465 544 } 466 545 467 if (element == "Coordinate") 468 { 469 if (mCurrentMesh) 470 StartCoordinate(attributes); 546 if (element == "Coordinate") { 547 StartCoordinate(attributes); 471 548 } 472 473 if (element == "Material") { 474 StartMaterial(attributes); 549 550 // todo 551 if (0 && element == "Material") { 552 StartMaterial(attributes); 475 553 } 476 554 … … 479 557 } 480 558 481 mElementCount++;559 ++ mElementCount; 482 560 mAttrCount += attributes.getLength(); 483 561 } … … 485 563 void 486 564 X3dParseHandlers::characters(const XMLCh* const chars, 487 565 const unsigned int length) 488 566 { 489 567 mCharacterCount += length; … … 492 570 void 493 571 X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars, 494 572 const unsigned int length) 495 573 { 496 574 mSpaceCount += length; … … 507 585 508 586 509 510 587 // --------------------------------------------------------------------------- 511 588 // StdInParseHandlers: Overrides of the SAX ErrorHandler interface 512 589 // --------------------------------------------------------------------------- 590 591 513 592 void 514 593 X3dParseHandlers::error(const SAXParseException& e) … … 600 679 const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis(); 601 680 parser->parse(filename.c_str()); 681 602 682 const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis(); 603 683 duration = endMillis - startMillis; … … 674 754 void X3dViewCellsParseHandlers::endElement(const XMLCh* const name) 675 755 { 676 StrX lname(name); 677 string element(lname.LocalForm()); 678 679 if (element == "Shape") 680 EndShape(); 756 StrX lname(name); 757 758 string element(lname.LocalForm()); 759 760 if (element == "Shape") 761 EndShape(); 681 762 } 682 763 … … 694 775 int i; 695 776 696 // clear previous vertex indices697 mCurrentVertexIndices.clear();698 699 700 777 for (i=0; i < len; i++) 701 778 {
Note: See TracChangeset
for help on using the changeset viewer.