Changeset 10141 in orxonox.OLD for trunk/src/lib/graphics
- Timestamp:
- Dec 23, 2006, 12:05:13 PM (18 years ago)
- Location:
- trunk/src/lib/graphics/importer
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/importer/static_model.h
r9869 r10141 37 37 void rebuild() { data->rebuild(); }; 38 38 39 Material* addMaterial( Material*material) { return data->addMaterial(material); };39 Material* addMaterial(const Material& material) { return data->addMaterial(material); }; 40 40 Material* addMaterial(const std::string& materialName) { return data->addMaterial(materialName); }; 41 41 -
trunk/src/lib/graphics/importer/static_model_data.cc
r9869 r10141 22 22 #include "debug.h" 23 23 #include <stdarg.h> 24 24 #include <algorithm> 25 25 26 26 … … 32 32 * @brief creates a new ModelFaceElement 33 33 */ 34 ModelFaceElement::ModelFaceElement()34 StaticModelData::FaceElement::FaceElement() 35 35 { 36 36 this->vertexNumber = -1; 37 37 this->normalNumber = -1; 38 38 this->texCoordNumber = -1; 39 40 this->next = NULL; 41 } 42 43 /** 44 * @brief destroys a ModelFaceElement 45 */ 46 ModelFaceElement::~ModelFaceElement() 47 { 48 if (this->next) 49 delete this->next; 50 } 39 } 40 51 41 52 42 /** 53 43 * @brief creates a new ModelFace 54 44 */ 55 ModelFace::ModelFace() 56 { 57 this->vertexCount = 0; 58 59 this->firstElem = NULL; 60 61 this->material = NULL; 62 63 this->next = NULL; 64 } 65 66 /** 67 * deletes a ModelFace 68 */ 69 ModelFace::~ModelFace() 70 { 71 PRINTF(5)("Cleaning up Face\n"); 72 73 if (this->firstElem != NULL) 74 delete this->firstElem; 75 76 if (this->next != NULL) 77 delete this->next; 45 StaticModelData::Face::Face() 46 { 47 this->_material = NULL; 78 48 } 79 49 … … 81 51 * @brief Creates a new ModelGroup 82 52 */ 83 ModelGroup::ModelGroup()53 StaticModelData::Group::Group() 84 54 { 85 55 PRINTF(4)("Adding new Group\n"); 86 56 this->name = ""; 87 57 this->faceMode = -1; 88 this->faceCount = 0;89 this->next = NULL;90 58 this->listNumber = 0; 91 59 this->indices = NULL; 92 93 this->firstFace = new ModelFace;94 this->currentFace = this->firstFace;95 60 } 96 61 … … 98 63 * @brief deletes a ModelGroup 99 64 */ 100 ModelGroup::~ModelGroup()65 StaticModelData::Group::~Group() 101 66 { 102 67 PRINTF(5)("Cleaning up group\n"); 103 if (this->firstFace != NULL)104 delete this->firstFace;105 106 68 // deleting the glList 107 69 if (this->listNumber != 0) 108 70 glDeleteLists(this->listNumber, 1); 109 110 if (this->next !=NULL)111 delete this->next;112 113 71 } 114 72 … … 118 76 * actually does the same as the delete Operator, but does not delete the predecessing group 119 77 */ 120 void ModelGroup::cleanup()78 void StaticModelData::Group::cleanup() 121 79 { 122 80 PRINTF(5)("Cleaning up group\n"); 123 if (this->firstFace) 124 delete this->firstFace; 125 this->firstFace = NULL; 126 if (this->next) 127 this->next->cleanup(); 81 this->_faces.clear(); 128 82 } 129 83 … … 150 104 151 105 // setting the start group; 152 this->currentGroup = this->firstGroup = new ModelGroup; 153 this->groupCount = 0; 106 this->_modelGroups.push_back(Group()); 154 107 this->faceCount = 0; 155 108 … … 164 117 StaticModelData::~StaticModelData() 165 118 { 166 PRINTF(4)("Deleting Model "); 167 if (!this->getName().empty()) 168 { 169 PRINT(4)("%s\n", this->getCName()); 170 } 171 else 172 { 173 PRINT(4)("\n"); 174 } 119 PRINTF(4)("Deleting Model %s\n", this->getCName()); 175 120 this->cleanup(); 176 177 PRINTF(5)("Deleting display Lists.\n");178 delete this->firstGroup;179 180 // deleting the MaterialList181 PRINTF(5)("Deleting Materials.\n");182 183 //! @todo do we really have to delete this material??184 std::list<ModelMaterial*>::iterator modMat;185 for(modMat = this->materialList.begin(); modMat != this->materialList.end(); modMat++)186 {187 if (!(*modMat)->external)188 delete (*modMat)->material;189 delete (*modMat);190 }191 121 } 192 122 … … 223 153 { 224 154 PRINTF(4)("drawing the 3D-Models\n"); 225 ModelGroup* tmpGroup = this->firstGroup; 226 while (tmpGroup != NULL) 227 { 228 PRINTF(5)("Drawing model %s\n", tmpGroup->name.c_str()); 229 glCallList (tmpGroup->listNumber); 230 tmpGroup = tmpGroup->next; 155 156 for(unsigned int i = 0; i < _modelGroups.size(); ++i ) 157 { 158 PRINTF(5)("Drawing model %s\n", _modelGroups[i].name.c_str()); 159 glCallList (_modelGroups[i].listNumber); 231 160 } 232 161 } … … 239 168 * It does this by just calling the List that must have been created earlier. 240 169 */ 241 void StaticModelData::draw ( int groupNumber) const242 { 243 if (unlikely(groupNumber >= this->groupCount))244 { 245 PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, this->groupCount);170 void StaticModelData::draw (unsigned int groupNumber) const 171 { 172 if (unlikely(groupNumber >= _modelGroups.size())) 173 { 174 PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, _modelGroups.size()); 246 175 return; 247 176 } 248 PRINTF(4)("drawing the requested 3D-Models if found.\n"); 249 ModelGroup* tmpGroup = this->firstGroup; 250 int counter = 0; 251 while (tmpGroup != NULL) 252 { 253 if (counter == groupNumber) 254 { 255 PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name.c_str()); 256 glCallList (tmpGroup->listNumber); 257 return; 258 } 259 ++counter; 260 tmpGroup = tmpGroup->next; 261 } 262 PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->getCName()); 263 return; 177 else 178 { 179 PRINTF(4)("Drawing model number %i named %s\n", groupNumber, _modelGroups[groupNumber].name.c_str()); 180 glCallList (_modelGroups[groupNumber].listNumber); 181 } 264 182 } 265 183 … … 274 192 { 275 193 PRINTF(4)("drawing the requested 3D-Models if found.\n"); 276 ModelGroup* tmpGroup = this->firstGroup; 277 while (tmpGroup != NULL) 278 { 279 if (tmpGroup->name == groupName) 280 { 281 PRINTF(4)("Drawing model %s\n", tmpGroup->name.c_str()); 282 glCallList (tmpGroup->listNumber); 283 return; 284 } 285 tmpGroup = tmpGroup->next; 286 } 287 PRINTF(2)("Model Named %s in %s not Found.\n", groupName.c_str(), this->getCName()); 288 return; 194 std::vector<Group>::const_iterator it = std::find(_modelGroups.begin(), _modelGroups.end(), groupName); 195 196 if (it != _modelGroups.end()) 197 { 198 PRINTF(4)("Drawing model %s\n", (*it).name.c_str()); 199 glCallList ((*it).listNumber); 200 } 201 else 202 { 203 PRINTF(2)("Model Named %s in %s not Found.\n", groupName.c_str(), this->getCName()); 204 } 289 205 } 290 206 … … 303 219 { 304 220 PRINTF(4)("cleaning up the 3D-Model to save Memory.\n"); 305 this->firstGroup->cleanup(); 221 for (unsigned int i = 0; i < _modelGroups.size(); ++i) 222 _modelGroups[i].cleanup(); 306 223 return true; 307 224 } … … 318 235 * with this option set the Materials will not be deleted with the Model. 319 236 */ 320 Material* StaticModelData::addMaterial(Material* material) 321 { 322 if (material == NULL) 323 return NULL; 324 ModelMaterial* modMat = new ModelMaterial; 325 modMat->external = true; 326 modMat->material = material; 327 this->materialList.push_back(modMat); 328 return modMat->material; 237 Material* StaticModelData::addMaterial(const Material& material) 238 { 239 this->materialList.push_back(material); 240 return &materialList.back(); 329 241 } 330 242 … … 336 248 Material* StaticModelData::addMaterial(const std::string& materialName) 337 249 { 338 ModelMaterial* modMat = new ModelMaterial;339 modMat->external = false;340 modMat->material = new Material(materialName);341 342 250 // adding material to the List of materials 343 this->materialList.push_back( modMat);344 return modMat->material;251 this->materialList.push_back(Material(materialName)); 252 return &materialList.back(); 345 253 } 346 254 … … 352 260 Material* StaticModelData::findMaterialByName(const std::string& materialName) 353 261 { 354 std::list<M odelMaterial*>::iterator modMat;355 for (modMat = this->materialList.begin(); modMat != this->materialList.end(); modMat++)356 if (materialName == (*modMat)->material->getName())357 return (*modMat)->material;358 return NULL;262 std::list<Material>::iterator it = std::find(materialList.begin(), materialList.end(), materialName); 263 if (it != materialList.end()) 264 return &(*it); 265 else 266 return NULL; 359 267 } 360 268 … … 369 277 { 370 278 PRINTF(5)("Read Group: %s.\n", groupString.c_str()); 371 if ( this->groupCount != 0 && this->currentGroup->faceCount > 0)372 {373 // finalizeGroup(currentGroup); 374 this->currentGroup = this->currentGroup->next = new ModelGroup;375 }279 if (!_modelGroups.empty() && !_modelGroups.back()._faces.empty()) 280 _modelGroups.push_back(Group()); 281 282 if (groupString == "default") 283 _modelGroups.back().name = groupString; 376 284 // setting the group name if not default. 377 if (groupString == "default")378 {379 this->currentGroup->name = groupString;380 }381 ++this->groupCount;382 285 return true; 383 286 } … … 500 403 { 501 404 const char* faceString = faceStringInput.c_str(); 502 if (this->currentGroup->faceCount >0) 503 this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; 504 505 ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement; 506 tmpElem->next = NULL; 405 Face newFace; 406 507 407 while(strcmp (faceString, "\0")) 508 408 { 509 if (this->currentGroup->currentFace->vertexCount>0) 510 tmpElem = tmpElem->next = new ModelFaceElement; 511 tmpElem->next = NULL; 409 FaceElement newElem; 512 410 513 411 char tmpValue [50]; … … 533 431 } 534 432 if (vertex) 535 tmpElem->vertexNumber = atoi(vertex)-1;433 newElem.vertexNumber = atoi(vertex)-1; 536 434 if (texture) 537 tmpElem->texCoordNumber = atoi(texture)-1;435 newElem.texCoordNumber = atoi(texture)-1; 538 436 if (normal) 539 tmpElem->normalNumber = atoi(normal)-1;437 newElem.normalNumber = atoi(normal)-1; 540 438 541 439 faceString += tmpLen; 542 440 if (strcmp (faceString, "\0")) 543 441 faceString++; 544 this->currentGroup->currentFace->vertexCount++; 545 } 546 547 this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2; 548 this->faceCount += this->currentGroup->currentFace->vertexCount -2; 442 443 newFace._elements.push_back(newElem); 444 } 445 446 //this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2; 447 _modelGroups.back()._faces.push_back(newFace); 448 this->faceCount += newFace._elements.size() - 2; 549 449 return true; 550 450 } … … 557 457 bool StaticModelData::addFace(int faceElemCount, VERTEX_FORMAT type, va_list args) 558 458 { 559 if (this->currentGroup->faceCount > 0) 560 this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; 561 562 ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement; 459 Face newFace; 563 460 564 461 for (int i = 0; i < faceElemCount; i++) 565 462 { 566 if (this->currentGroup->currentFace->vertexCount > 0) 567 tmpElem = tmpElem->next = new ModelFaceElement; 568 569 tmpElem->vertexNumber = va_arg (args, int); 463 FaceElement newElem; 464 465 newElem.vertexNumber = va_arg (args, int); 570 466 if (type & TEXCOORD) 571 tmpElem->texCoordNumber = va_arg (args, int);467 newElem.texCoordNumber = va_arg (args, int); 572 468 if (type & NORMAL) 573 tmpElem->normalNumber = va_arg(args, int); 574 this->currentGroup->currentFace->vertexCount++; 575 } 576 577 this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount - 2; 578 this->faceCount += this->currentGroup->currentFace->vertexCount -2; 469 newElem.normalNumber = va_arg(args, int); 470 471 newFace._elements.push_back(newElem); 472 } 473 474 //this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2; 475 _modelGroups.back()._faces.push_back(newFace); 476 this->faceCount += newFace._elements.size() - 2; 579 477 return true; 580 478 } … … 586 484 bool StaticModelData::setMaterial(const std::string& matString) 587 485 { 588 if (this->currentGroup->faceCount > 0) 589 this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; 590 591 this->currentGroup->currentFace->material = this->findMaterialByName(matString); 592 593 if (this->currentGroup->faceCount == 0) 594 this->currentGroup->faceCount++; 486 Face matFace; 487 matFace._material = this->findMaterialByName(matString); 488 _modelGroups.back()._faces.push_back(matFace); 489 595 490 return true; 596 491 } … … 602 497 bool StaticModelData::setMaterial(Material* mtl) 603 498 { 604 if (this->currentGroup->faceCount > 0) 605 this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; 606 607 this->currentGroup->currentFace->material = mtl; 608 609 if (this->currentGroup->faceCount == 0) 610 this->currentGroup->faceCount++; 499 Face matFace; 500 matFace._material = mtl; 501 _modelGroups.back()._faces.push_back(matFace); 502 611 503 return true; 612 504 } … … 633 525 Vector curV; 634 526 635 ModelGroup* tmpGroup = firstGroup; 636 while (tmpGroup != NULL) 637 { 638 ModelFace* tmpFace = tmpGroup->firstFace; 639 while (tmpFace != NULL) 527 for (std::vector<Group>::iterator group = _modelGroups.begin(); 528 group != _modelGroups.end(); 529 ++group) 530 { 531 for (std::vector<Face>::iterator face = (*group)._faces.begin(); 532 face != (*group)._faces.end(); 533 ++face) 640 534 { 641 if (tmpFace->firstElem != NULL) 642 { 643 ModelFaceElement* firstElem = tmpFace->firstElem; 644 ModelFaceElement* prevElem; 645 ModelFaceElement* curElem = firstElem; 646 ModelFaceElement* nextElem; 647 ModelFaceElement* lastElem; 648 // find last Element of the Chain. !! IMPORTANT:the last Element of the Chain must point to NULL, or it will resolv into an infinity-loop. 649 while (curElem != NULL) 535 if (!(*face)._elements.empty()) 536 { 537 std::vector<FaceElement>::iterator firstElem = (*face)._elements.begin(); 538 std::vector<FaceElement>::iterator prevElem; 539 std::vector<FaceElement>::iterator curElem = firstElem; 540 std::vector<FaceElement>::iterator nextElem; 541 std::vector<FaceElement>::iterator lastElem; 542 543 // find last Element of the Chain. 544 while (curElem != (*face)._elements.end()) 650 545 { 651 546 prevElem = curElem; 652 curElem = curElem->next;547 ++curElem; 653 548 } 654 549 lastElem = prevElem; 655 550 656 551 curElem = firstElem; 657 for (unsigned int j = 0; j < tmpFace->vertexCount; j++)552 for (unsigned int j = 0; j < (*face)._elements.size(); j++) 658 553 { 659 if (!(nextElem = curElem->next)) 554 nextElem = curElem; 555 nextElem++; 556 if (nextElem == (*face)._elements.end()) 660 557 nextElem = firstElem; 661 curElem->normalNumber = curElem->vertexNumber;662 663 curV = Vector (this->vertices[ curElem->vertexNumber*3],664 this->vertices[ curElem->vertexNumber*3+1],665 this->vertices[ curElem->vertexNumber*3+2]);666 667 prevV = Vector (this->vertices[ prevElem->vertexNumber*3],668 this->vertices[ prevElem->vertexNumber*3+1],669 this->vertices[ prevElem->vertexNumber*3+2]) - curV;670 671 nextV = Vector (this->vertices[ nextElem->vertexNumber*3],672 this->vertices[ nextElem->vertexNumber*3+1],673 this->vertices[ nextElem->vertexNumber*3+2]) - curV;674 normArray[ curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);558 (*curElem).normalNumber = (*curElem).vertexNumber; 559 560 curV = Vector (this->vertices[(*curElem).vertexNumber*3], 561 this->vertices[(*curElem).vertexNumber*3+1], 562 this->vertices[(*curElem).vertexNumber*3+2]); 563 564 prevV = Vector (this->vertices[(*prevElem).vertexNumber*3], 565 this->vertices[(*prevElem).vertexNumber*3+1], 566 this->vertices[(*prevElem).vertexNumber*3+2]) - curV; 567 568 nextV = Vector (this->vertices[(*nextElem).vertexNumber*3], 569 this->vertices[(*nextElem).vertexNumber*3+1], 570 this->vertices[(*nextElem).vertexNumber*3+2]) - curV; 571 normArray[(*curElem).vertexNumber] = normArray[(*curElem).vertexNumber] + nextV.cross(prevV); 675 572 676 573 prevElem = curElem; 677 curElem = curElem->next;574 ++curElem; 678 575 } 679 576 } 680 tmpFace = tmpFace->next;681 577 } 682 tmpGroup = tmpGroup->next;683 578 } 684 579 … … 707 602 this->buildVertexNormals(); 708 603 709 this->currentGroup = this->firstGroup;710 711 while (this->currentGroup != NULL)604 for (std::vector<Group>::iterator group = _modelGroups.begin(); 605 group != _modelGroups.end(); 606 ++group) 712 607 { 713 608 714 609 // creating a glList for the Group 715 if (( this->currentGroup->listNumber = glGenLists(1)) == 0)610 if (((*group).listNumber = glGenLists(1)) == 0) 716 611 { 717 612 PRINTF(2)("glList could not be created for this Model\n"); 718 613 return false; 719 614 } 720 glNewList ( this->currentGroup->listNumber, GL_COMPILE);615 glNewList ((*group).listNumber, GL_COMPILE); 721 616 722 617 // Putting Faces to GL 723 ModelFace* tmpFace = this->currentGroup->firstFace; 724 while (tmpFace != NULL) 618 for (std::vector<Face>::const_iterator face = (*group)._faces.begin(); 619 face != (*group)._faces.end(); 620 ++face) 725 621 { 726 if ( tmpFace->vertexCount == 0 && tmpFace->material != NULL)727 { 728 if ( this->currentGroup->faceMode != -1)622 if ((*face)._elements.empty() && (*face)._material != NULL) 623 { 624 if ((*group).faceMode != -1) 729 625 glEnd(); 730 this->currentGroup->faceMode = 0;731 if ( tmpFace->material != NULL)626 (*group).faceMode = 0; 627 if ((*face)._material != NULL) 732 628 { 733 tmpFace->material->select();734 PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getCName());629 (*face)._material->select(); 630 PRINTF(5)("using material %s for coming Faces.\n", (*face)._material->getCName()); 735 631 } 736 632 } 737 633 738 else if ( tmpFace->vertexCount== 3)739 { 740 if ( this->currentGroup->faceMode != 3)634 else if ((*face)._elements.size() == 3) 635 { 636 if ((*group).faceMode != 3) 741 637 { 742 if ( this->currentGroup->faceMode != -1)638 if ((*group).faceMode != -1) 743 639 glEnd(); 744 640 glBegin(GL_TRIANGLES); 745 641 } 746 642 747 this->currentGroup->faceMode = 3;643 (*group).faceMode = 3; 748 644 PRINTF(5)("found triag.\n"); 749 645 } 750 646 751 else if ( tmpFace->vertexCount== 4)752 { 753 if ( this->currentGroup->faceMode != 4)647 else if ((*face)._elements.size() == 4) 648 { 649 if ((*group).faceMode != 4) 754 650 { 755 if ( this->currentGroup->faceMode != -1)651 if ((*group).faceMode != -1) 756 652 glEnd(); 757 653 glBegin(GL_QUADS); 758 654 } 759 this->currentGroup->faceMode = 4;655 (*group).faceMode = 4; 760 656 PRINTF(5)("found quad.\n"); 761 657 } 762 658 763 else if ( tmpFace->vertexCount> 4)764 { 765 if ( this->currentGroup->faceMode != -1)659 else if ((*face)._elements.size() > 4) 660 { 661 if ((*group).faceMode != -1) 766 662 glEnd(); 767 663 glBegin(GL_POLYGON); 768 PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount); 769 this->currentGroup->faceMode = tmpFace->vertexCount; 770 } 771 772 ModelFaceElement* tmpElem = tmpFace->firstElem; 773 while (tmpElem != NULL) 774 { 775 // PRINTF(2)("%s\n", tmpElem->value); 776 this->addGLElement(tmpElem); 777 tmpElem = tmpElem->next; 778 } 779 tmpFace = tmpFace->next; 664 PRINTF(5)("Polygon with %i faces found.", (*face)._elements.size()); 665 (*group).faceMode = (*face)._elements.size(); 666 } 667 668 for (std::vector<FaceElement>::const_iterator elem = (*face)._elements.begin(); 669 elem != (*face)._elements.end(); 670 ++elem) 671 { 672 // PRINTF(2)("%s\n", (*elem).value); 673 this->addGLElement(*elem); 674 } 780 675 } 781 676 glEnd(); 782 677 glEndList(); 783 678 784 this->currentGroup = this->currentGroup->next;785 679 } 786 680 return true; … … 800 694 801 695 int index = 0; //!< the counter for the triangle array 802 ModelFaceElement* tmpElem; //!< the temporary faceelement reference803 ModelFace* tmpFace; //!< the temporary face referece804 805 696 unsigned int numTriangles = 0; 806 697 bool warned = false; … … 808 699 /* count the number of triangles */ 809 700 /* now iterate through all groups and build up the triangle list */ 810 this->currentGroup = this->firstGroup; 811 while( this->currentGroup != NULL) 812 { 813 tmpFace = this->currentGroup->firstFace; 814 while( tmpFace != NULL) 701 for (std::vector<Group>::iterator group = _modelGroups.begin(); 702 group != _modelGroups.end(); 703 ++group) 704 { 705 for (std::vector<Face>::const_iterator face = (*group)._faces.begin(); 706 face != (*group)._faces.end(); 707 ++face) 815 708 { 816 709 /* if its a triangle just add it to the list */ 817 if( tmpFace->vertexCount== 3)710 if( (*face)._elements.size() == 3) 818 711 { 819 712 ++numTriangles; 820 713 } /* if the polygon is a quad */ 821 else if( tmpFace->vertexCount== 4)714 else if((*face)._elements.size() == 4) 822 715 { 823 716 numTriangles += 2; 824 717 } 825 else if( tmpFace->vertexCount> 4)718 else if( (*face)._elements.size() > 4) 826 719 { 827 720 if (!warned) … … 831 724 } 832 725 } 833 tmpFace = tmpFace->next;834 726 } 835 this->currentGroup = this->currentGroup->next;836 727 } 837 728 … … 845 736 846 737 /* now iterate through all groups and build up the triangle list */ 847 this->currentGroup = this->firstGroup; 848 while( this->currentGroup != NULL) 849 { 850 tmpFace = this->currentGroup->firstFace; 851 while( tmpFace != NULL) 738 for (std::vector<Group>::iterator group = _modelGroups.begin(); 739 group != _modelGroups.end(); 740 ++group) 741 { 742 for (std::vector<Face>::const_iterator face = (*group)._faces.begin(); 743 face != (*group)._faces.end(); 744 ++face) 852 745 { 853 tmpElem = tmpFace->firstElem;854 855 746 /* if its a triangle just add it to the list */ 856 if( tmpFace->vertexCount == 3) 857 { 858 for( int j = 0; j < 3; ++j) 747 if( (*face)._elements.size() == 3) 748 { 749 unsigned int j = 0; 750 for (std::vector<FaceElement>::const_iterator elem = (*face)._elements.begin(); 751 elem != (*face)._elements.end(); 752 ++elem) 859 753 { 860 this->triangles[index].indexToVertices[j] = (unsigned int)tmpElem->vertexNumber * 3 ; 861 this->triangles[index].indexToNormals[j] = (unsigned int)tmpElem->normalNumber * 3 ; 862 this->triangles[index].indexToTexCoor[j] = (unsigned int)tmpElem->texCoordNumber * 3 ; 863 tmpElem = tmpElem->next; 864 754 this->triangles[index].indexToVertices[j] = (unsigned int)(*elem).vertexNumber * 3; 755 this->triangles[index].indexToNormals[j] = (unsigned int)(*elem).normalNumber * 3; 756 this->triangles[index].indexToTexCoor[j] = (unsigned int)(*elem).texCoordNumber * 3; 757 ++j; 865 758 } 866 759 ++index; 867 760 } /* if the polygon is a quad */ 868 else if( tmpFace->vertexCount == 4) 869 { 870 871 this->triangles[index].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber * 3; 872 this->triangles[index].indexToNormals[0] = (unsigned int)tmpElem->normalNumber * 3; 873 this->triangles[index].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber * 3; 874 875 this->triangles[index + 1].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber * 3; 876 this->triangles[index + 1].indexToNormals[0] = (unsigned int)tmpElem->normalNumber * 3; 877 this->triangles[index + 1].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber * 3; 878 tmpElem = tmpElem->next; 879 880 this->triangles[index].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber * 3; 881 this->triangles[index].indexToNormals[1] = (unsigned int)tmpElem->normalNumber * 3; 882 this->triangles[index].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber * 3; 883 tmpElem = tmpElem->next; 884 885 this->triangles[index].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber * 3; 886 this->triangles[index].indexToNormals[2] = (unsigned int)tmpElem->normalNumber * 3; 887 this->triangles[index].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber * 3; 888 889 this->triangles[index + 1].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber * 3; 890 this->triangles[index + 1].indexToNormals[2] = (unsigned int)tmpElem->normalNumber * 3; 891 this->triangles[index + 1].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber * 3; 892 tmpElem = tmpElem->next; 893 894 this->triangles[index + 1].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber * 3; 895 this->triangles[index + 1].indexToNormals[1] = (unsigned int)tmpElem->normalNumber * 3; 896 this->triangles[index + 1].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber * 3; 761 762 else if( (*face)._elements.size() == 4) 763 { 764 std::vector<FaceElement>::const_iterator elem = (*face)._elements.begin(); 765 766 this->triangles[index].indexToVertices[0] = (unsigned int)(*elem).vertexNumber * 3; 767 this->triangles[index].indexToNormals[0] = (unsigned int)(*elem).normalNumber * 3; 768 this->triangles[index].indexToTexCoor[0] = (unsigned int)(*elem).texCoordNumber * 3; 769 770 this->triangles[index + 1].indexToVertices[0] = (unsigned int)(*elem).vertexNumber * 3; 771 this->triangles[index + 1].indexToNormals[0] = (unsigned int)(*elem).normalNumber * 3; 772 this->triangles[index + 1].indexToTexCoor[0] = (unsigned int)(*elem).texCoordNumber * 3; 773 ++elem; 774 775 this->triangles[index].indexToVertices[1] = (unsigned int)(*elem).vertexNumber * 3; 776 this->triangles[index].indexToNormals[1] = (unsigned int)(*elem).normalNumber * 3; 777 this->triangles[index].indexToTexCoor[1] = (unsigned int)(*elem).texCoordNumber * 3; 778 ++elem; 779 780 this->triangles[index].indexToVertices[2] = (unsigned int)(*elem).vertexNumber * 3; 781 this->triangles[index].indexToNormals[2] = (unsigned int)(*elem).normalNumber * 3; 782 this->triangles[index].indexToTexCoor[2] = (unsigned int)(*elem).texCoordNumber * 3; 783 784 this->triangles[index + 1].indexToVertices[2] = (unsigned int)(*elem).vertexNumber * 3; 785 this->triangles[index + 1].indexToNormals[2] = (unsigned int)(*elem).normalNumber * 3; 786 this->triangles[index + 1].indexToTexCoor[2] = (unsigned int)(*elem).texCoordNumber * 3; 787 ++elem; 788 789 this->triangles[index + 1].indexToVertices[1] = (unsigned int)(*elem).vertexNumber * 3; 790 this->triangles[index + 1].indexToNormals[1] = (unsigned int)(*elem).normalNumber * 3; 791 this->triangles[index + 1].indexToTexCoor[1] = (unsigned int)(*elem).texCoordNumber * 3; 897 792 898 793 index += 2; 899 794 } 900 tmpFace = tmpFace->next;901 795 } 902 this->currentGroup = this->currentGroup->next;903 796 } 904 797 return true; … … 916 809 merging this information, the face will be drawn. 917 810 */ 918 bool StaticModelData::addGLElement ( ModelFaceElement*elem)811 bool StaticModelData::addGLElement (const FaceElement& elem) 919 812 { 920 813 PRINTF(5)("importing grafical Element to openGL.\n"); 921 814 922 if (elem ->texCoordNumber > -1)923 { 924 if (likely((unsigned int)elem ->texCoordNumber < this->vTexture.size()))925 glTexCoord2fv(&this->vTexture[0] + elem ->texCoordNumber * 2);815 if (elem.texCoordNumber > -1) 816 { 817 if (likely((unsigned int)elem.texCoordNumber < this->vTexture.size())) 818 glTexCoord2fv(&this->vTexture[0] + elem.texCoordNumber * 2); 926 819 else 927 820 PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n", 928 elem ->texCoordNumber, this->vTexture.size());929 } 930 if (elem ->normalNumber > -1)931 { 932 if (likely((unsigned int)elem ->normalNumber < this->normals.size()))933 glNormal3fv(&this->normals[0] + elem ->normalNumber * 3);821 elem.texCoordNumber, this->vTexture.size()); 822 } 823 if (elem.normalNumber > -1) 824 { 825 if (likely((unsigned int)elem.normalNumber < this->normals.size())) 826 glNormal3fv(&this->normals[0] + elem.normalNumber * 3); 934 827 else 935 828 PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete", 936 elem ->normalNumber, this->normals.size());937 } 938 if (elem ->vertexNumber > -1)939 { 940 if (likely((unsigned int)elem ->vertexNumber < this->vertices.size()))941 glVertex3fv(&this->vertices[0]+ elem ->vertexNumber * 3);829 elem.normalNumber, this->normals.size()); 830 } 831 if (elem.vertexNumber > -1) 832 { 833 if (likely((unsigned int)elem.vertexNumber < this->vertices.size())) 834 glVertex3fv(&this->vertices[0]+ elem.vertexNumber * 3); 942 835 else 943 836 PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete", 944 elem ->vertexNumber, this->vertices.size());945 } 946 947 return true; 948 } 837 elem.vertexNumber, this->vertices.size()); 838 } 839 840 return true; 841 } -
trunk/src/lib/graphics/importer/static_model_data.h
r9869 r10141 27 27 }; 28 28 29 ////////////////////30 /// SUB-ELEMENTS ///31 ////////////////////32 //! This is the placeholder of one Vertex beloning to a Face.33 class ModelFaceElement34 {35 public:36 ModelFaceElement();37 ~ModelFaceElement();38 29 39 int vertexNumber; //!< The number of the Vertex out of the Array* vertices, this vertex points to.40 int normalNumber; //!< The number of the Normal out of the Array* normals, this vertex points to.41 int texCoordNumber; //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to.42 43 ModelFaceElement* next; //!< Point to the next FaceElement in this List.44 };45 46 //! This is the placeholder of a Face belonging to a Group of Faces.47 class ModelFace48 {49 public:50 ModelFace();51 ~ModelFace();52 53 unsigned int vertexCount; //!< The Count of vertices this Face has.54 ModelFaceElement* firstElem; //!< Points to the first Vertex (FaceElement) of this Face.55 Material* material; //!< The Material to use.56 57 ModelFace* next; //!< Pointer to the next Face.58 };59 60 //! Group to handle multiple Models per obj-file.61 class ModelGroup62 {63 public:64 ModelGroup();65 ~ModelGroup();66 67 void cleanup();68 69 std::string name; //!< the Name of the Group. this is an identifier, that can be accessed via the draw (std::string name) function.70 GLubyte* indices; //!< The indices of the Groups. Needed for vertex-arrays71 GLuint listNumber; //!< The number of the GL-List this Group gets.72 ModelFace* firstFace; //!< The first Face in this group.73 ModelFace* currentFace; //!< The current Face in this Group (the one we are currently working with.)74 int faceMode; //!< The Mode the Face is in: initially -1, 0 for FaceList opened, 1 for Material, 3 for triangle, 4 for Quad, 5+ for Poly @todo ENUM...75 int faceCount; //!< The Number of Faces this Group holds.76 77 ModelGroup* next; //!< Pointer to the next Group.78 };79 80 struct ModelMaterial81 {82 Material* material;83 bool external;84 };85 30 86 31 ///////////// … … 95 40 { 96 41 ObjectListDeclaration(StaticModelData); 42 43 private: 44 //////////////////// 45 /// SUB-ELEMENTS /// 46 //////////////////// 47 //! This is the placeholder of one Vertex beloning to a Face. 48 class FaceElement 49 { 97 50 public: 98 typedef CountPointer<StaticModelData> Pointer; 51 FaceElement(); 52 53 int vertexNumber; //!< The number of the Vertex out of the Array* vertices, this vertex points to. 54 int normalNumber; //!< The number of the Normal out of the Array* normals, this vertex points to. 55 int texCoordNumber; //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to. 56 }; 57 58 //! This is the placeholder of a Face belonging to a Group of Faces. 59 class Face 60 { 61 public: 62 Face(); 63 64 std::vector<FaceElement> _elements; //!< Elements of the Face. 65 Material* _material; //!< The Material to use. 66 }; 67 68 //! Group to handle multiple Models per obj-file. 69 class Group 70 { 71 public: 72 Group(); 73 ~Group(); 74 75 //! Compares the name with the groups name. 76 bool operator==(const std::string& name) const { return this->name == name; }; 77 void cleanup(); 78 79 std::string name; //!< the Name of the Group. this is an identifier, that can be accessed via the draw (std::string name) function. 80 GLubyte* indices; //!< The indices of the Groups. Needed for vertex-arrays 81 GLuint listNumber; //!< The number of the GL-List this Group gets. 82 int faceMode; //!< The Mode the Face is in: initially -1, 0 for FaceList opened, 1 for Material, 3 for triangle, 4 for Quad, 5+ for Poly @todo ENUM... 83 84 std::vector<Face> _faces; //!< Faces. 85 }; 99 86 100 87 public: 88 typedef CountPointer<StaticModelData> Pointer; 89 90 public: 101 91 StaticModelData(const std::string& modelName = ""); 102 92 virtual ~StaticModelData(); 103 93 104 94 void draw() const; 105 void draw( int groupNumber) const;95 void draw(unsigned int groupNumber) const; 106 96 void draw(const std::string& groupName) const; 107 97 108 98 void rebuild(); 109 99 110 Material* addMaterial( Material*material);100 Material* addMaterial(const Material& material); 111 101 Material* addMaterial(const std::string& materialName); 112 102 … … 151 141 bool buildTriangleList(); 152 142 153 bool addGLElement( ModelFaceElement*elem);143 bool addGLElement(const StaticModelData::FaceElement& elem); 154 144 155 145 bool cleanup(); … … 168 158 std::vector<sTriangleExt> triangles; //!< The Triangles if built. 169 159 170 ModelGroup* firstGroup; //!< The first of all groups.171 ModelGroup* currentGroup; //!< The currentGroup. this is the one we will work with.172 int groupCount; //!< The Count of Groups.173 160 174 std::list<ModelMaterial*> materialList; //!< A list for all the Materials in this Model 161 std::vector<Group> _modelGroups; 162 163 std::list<Material> materialList; //!< A list for all the Materials in this Model 175 164 }; 176 165
Note: See TracChangeset
for help on using the changeset viewer.