Changeset 4577 in orxonox.OLD for orxonox/trunk/src/lib/graphics/importer
- Timestamp:
- Jun 10, 2005, 2:32:13 AM (19 years ago)
- Location:
- orxonox/trunk/src/lib/graphics/importer
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/graphics/importer/array.cc
r4109 r4577 1 /* 1 /* 2 2 orxonox - the future of 3D-vertical-scrollers 3 3 … … 17 17 18 18 #include "array.h" 19 20 #include "debug.h" 19 21 20 22 /** … … 66 68 void Array::finalizeArray (void) 67 69 { 68 PRINTF(4)("Finalizing array. Length: %i\n", entryCount); 70 PRINTF(4)("Finalizing array. Length: %i\n", entryCount); 69 71 // if ((array = (GLfloat*)malloc( entryCount* sizeof(GLfloat))) == NULL) 70 72 if (!(this->array = new GLfloat [this->entryCount])) … … 90 92 { 91 93 PRINTF(5)("adding new Entry to Array: %f\n", entry); 92 94 93 95 this->currentEntry->value = entry; 94 96 this->currentEntry->next = new Entry; … … 97 99 ++this->entryCount; 98 100 } 99 else 101 else 100 102 PRINTF(2)("adding failed, because list has been finalized\n"); 101 103 } … … 110 112 this->addEntry (entry2); 111 113 } 112 114 113 115 /** 114 116 \brief Simple debug info about the Array -
orxonox/trunk/src/lib/graphics/importer/array.h
r4109 r4577 8 8 #ifndef _ARRAY_H 9 9 #define _ARRAY_H 10 11 #include "stdincl.h" 10 #include "glincl.h" 12 11 13 12 //! Array Class that handles dynamic-float arrays. … … 22 21 void addEntry (GLfloat entry); 23 22 void addEntry(GLfloat entry0, GLfloat entry1, GLfloat entry2); 24 23 25 24 /** \returns The array */ 26 inline const GLfloat* getArray () const { return this->array;}25 inline const GLfloat* getArray () const { return this->array; }; 27 26 /** \returns The Count of entries in the Array*/ 28 inline int getCount(void)const { return this->entryCount;}27 inline int getCount(void)const { return this->entryCount; }; 29 28 void debug(void) const ; 30 29 private: … … 41 40 Entry* firstEntry; //!< Pointer to the first Entry of this Array 42 41 Entry* currentEntry; //!< Pointer to the current Entry of this Array. The one Entry we are working with. 43 44 42 43 45 44 }; 46 45 -
orxonox/trunk/src/lib/graphics/importer/model.cc
r4357 r4577 1 /* 1 /* 2 2 orxonox - the future of 3D-vertical-scrollers 3 3 … … 21 21 #include <stdarg.h> 22 22 23 #include "array.h"24 23 #include "vector.h" 25 24 #include "list.h" … … 38 37 this->vertexNumber = -1; 39 38 this->normalNumber = -1; 40 this->texCoordNumber = -1; 39 this->texCoordNumber = -1; 41 40 42 41 this->next = NULL; … … 60 59 61 60 this->firstElem = NULL; 62 61 63 62 this->material = NULL; 64 63 65 64 this->next = NULL; 66 65 … … 76 75 if (this->firstElem != NULL) 77 76 delete this->firstElem; 78 77 79 78 if (this->next != NULL) 80 79 delete this->next; … … 89 88 this->name = ""; 90 89 this->faceMode = -1; 91 this->faceCount = 0; 90 this->faceCount = 0; 92 91 this->next = NULL; 93 92 94 93 this->firstFace = new ModelFace; 95 94 this->currentFace = this->firstFace; … … 135 134 Model::Model(const char* modelName, MODEL_TYPE type) 136 135 { 137 PRINTF(4)("new 3D-Model is being created\n"); 138 this->name = NULL; 136 PRINTF(4)("new 3D-Model is being created\n"); 139 137 this->setName(modelName); 140 138 this->type = type; … … 147 145 this->normalCount = 0; 148 146 this->texCoordCount = 0; 149 147 150 148 this->scaleFactor = 1; 151 149 … … 168 166 { 169 167 PRINTF(4)("Deleting Model "); 170 if (this->name) 171 { 172 PRINT(4)("%s\n", this->name); 173 delete []this->name; 168 if (this->getName()) 169 { 170 PRINT(4)("%s\n", this->getName()); 174 171 } 175 172 else … … 204 201 // this creates the display List. 205 202 this->importToDisplayList(); 206 203 207 204 208 205 // deletes everything we allocated. … … 223 220 void Model::draw (void) const 224 221 { 225 PRINTF(4)("drawing the 3D-Models\n"); 222 PRINTF(4)("drawing the 3D-Models\n"); 226 223 ModelGroup* tmpGroup = this->firstGroup; 227 224 while (tmpGroup != NULL) … … 239 236 It does this by just calling the List that must have been created earlier. 240 237 */ 241 void Model::draw (int groupNumber) const 238 void Model::draw (int groupNumber) const 242 239 { 243 240 if (groupNumber >= this->groupCount) … … 246 243 return; 247 244 } 248 PRINTF(4)("drawing the requested 3D-Models if found.\n"); 245 PRINTF(4)("drawing the requested 3D-Models if found.\n"); 249 246 ModelGroup* tmpGroup = this->firstGroup; 250 247 int counter = 0; … … 252 249 { 253 250 if (counter == groupNumber) 254 255 256 257 258 251 { 252 PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name); 253 glCallList (tmpGroup->listNumber); 254 return; 255 } 259 256 ++counter; 260 257 tmpGroup = tmpGroup->next; 261 258 } 262 PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this-> name);259 PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->getName()); 263 260 return; 264 261 … … 273 270 void Model::draw (char* groupName) const 274 271 { 275 PRINTF(4)("drawing the requested 3D-Models if found.\n"); 272 PRINTF(4)("drawing the requested 3D-Models if found.\n"); 276 273 ModelGroup* tmpGroup = this->firstGroup; 277 274 while (tmpGroup != NULL) 278 275 { 279 276 if (!strcmp(tmpGroup->name, groupName)) 280 281 282 283 284 277 { 278 PRINTF(4)("Drawing model %s\n", tmpGroup->name); 279 glCallList (tmpGroup->listNumber); 280 return; 281 } 285 282 tmpGroup = tmpGroup->next; 286 283 } 287 PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this-> name);284 PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->getName()); 288 285 return; 289 286 } … … 292 289 // INIT // 293 290 ////////// 294 /**295 \brief sets a name to the Model296 \param name The name to set to this Model297 */298 void Model::setName(const char* name)299 {300 if (this->name)301 delete []this->name;302 if (name)303 {304 this->name = new char[strlen(name)+1];305 strcpy(this->name, name);306 }307 else308 this->name = NULL;309 }310 311 291 /** 312 292 \brief deletes all the arrays … … 334 314 PRINTF(4)("cleaning up the 3D-Model to save Memory.\n"); 335 315 this->firstGroup->cleanup(); 336 return true; 337 } 338 339 316 return true; 317 } 340 318 341 319 ////////// … … 366 344 367 345 // adding material to the List of materials 368 this->materialList->add(newMat); 346 this->materialList->add(newMat); 369 347 return newMat; 370 348 } … … 382 360 { 383 361 if (!strcmp(material->getName(), materialName)) 384 385 386 387 362 { 363 delete tmpIt; 364 return material; 365 } 388 366 material = tmpIt->nextElement(); 389 367 } … … 396 374 \param groupString the new Group to create 397 375 398 This function initializes a new Group. 376 This function initializes a new Group. 399 377 With it you should be able to create Models with more than one SubModel inside 400 378 */ … … 439 417 \param y the Y-coordinate of the Vertex to add. 440 418 \param z the Z-coordinate of the Vertex to add. 441 419 442 420 */ 443 421 bool Model::addVertex(float x, float y, float z) … … 538 516 { 539 517 if (this->currentGroup->currentFace->vertexCount>0) 540 518 tmpElem = tmpElem->next = new ModelFaceElement; 541 519 tmpElem->next = NULL; 542 520 … … 552 530 553 531 if ((texture = strstr (vertex, "/")) != NULL) 554 555 556 557 558 559 560 561 562 } 563 532 { 533 texture[0] = '\0'; 534 texture ++; 535 536 if ((normal = strstr (texture, "/")) !=NULL) 537 { 538 normal[0] = '\0'; 539 normal ++; 540 } 541 } 564 542 if (vertex) 565 543 tmpElem->vertexNumber = atoi(vertex)-1; 566 544 if (texture) 567 545 tmpElem->texCoordNumber = atoi(texture)-1; 568 546 if (normal) 569 547 tmpElem->normalNumber = atoi(normal)-1; 570 548 571 549 faceString += tmpLen; 572 550 if (strcmp (faceString, "\0")) 573 551 faceString++; 574 552 this->currentGroup->currentFace->vertexCount++; 575 553 } … … 587 565 if (this->currentGroup->faceCount > 0) 588 566 this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; 589 567 590 568 ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement; 591 569 592 570 va_list itemlist; 593 571 va_start (itemlist, type); … … 596 574 { 597 575 if (this->currentGroup->currentFace->vertexCount > 0) 598 576 tmpElem = tmpElem->next = new ModelFaceElement; 599 577 600 578 tmpElem->vertexNumber = va_arg (itemlist, int); 601 579 if (type & TEXCOORD) 602 580 tmpElem->texCoordNumber = va_arg (itemlist, int); 603 581 if (type & NORMAL) 604 582 tmpElem->normalNumber = va_arg(itemlist, int); 605 583 this->currentGroup->currentFace->vertexCount++; 606 584 } … … 618 596 if (this->currentGroup->faceCount > 0) 619 597 this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; 620 598 621 599 this->currentGroup->currentFace->material = this->findMaterialByName(matString); 622 600 … … 633 611 if (this->currentGroup->faceCount > 0) 634 612 this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; 635 613 636 614 this->currentGroup->currentFace->material = mtl; 637 615 … … 650 628 */ 651 629 bool Model::buildVertexNormals () 652 { 630 { 653 631 PRINTF(4)("Normals are being calculated.\n"); 654 632 … … 656 634 for (int i=0; i<vertices->getCount()/3;i++) 657 635 normArray[i] = Vector(.0,.0,.0); 658 636 659 637 int firstTouch; 660 638 int secondTouch; … … 668 646 ModelFace* tmpFace = tmpGroup->firstFace; 669 647 while (tmpFace) 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 648 { 649 if (tmpFace->firstElem) 650 { 651 ModelFaceElement* firstElem = tmpFace->firstElem; 652 ModelFaceElement* prevElem; 653 ModelFaceElement* curElem = firstElem; 654 ModelFaceElement* nextElem; 655 ModelFaceElement* lastElem; 656 // 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. 657 while (curElem) 658 { 659 prevElem = curElem; 660 curElem = curElem->next; 661 } 662 lastElem = prevElem; 663 664 curElem = firstElem; 665 for (int j=0; j<tmpFace->vertexCount; j++) 666 { 667 if (!(nextElem = curElem->next)) 668 nextElem = firstElem; 669 curElem->normalNumber = curElem->vertexNumber; 670 671 curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]); 672 prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV; 673 nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV; 674 normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV); 675 676 prevElem = curElem; 677 curElem = curElem->next; 678 } 679 } 680 tmpFace = tmpFace->next; 681 } 704 682 tmpGroup = tmpGroup->next; 705 683 } … … 709 687 normArray[i].normalize(); 710 688 PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z); 711 689 712 690 this->addVertexNormal(normArray[i].x, normArray[i].y, normArray[i].z); 713 691 714 692 } 715 delete []normArray; 693 delete []normArray; 716 694 } 717 695 … … 738 716 // creating a glList for the Group 739 717 if ((this->currentGroup->listNumber = glGenLists(1)) == 0) 740 741 742 743 718 { 719 PRINTF(2)("glList could not be created for this Model\n"); 720 return false; 721 } 744 722 glNewList (this->currentGroup->listNumber, GL_COMPILE); 745 723 … … 747 725 ModelFace* tmpFace = this->currentGroup->firstFace; 748 726 while (tmpFace != NULL) 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 727 { 728 if (tmpFace->vertexCount == 0 && tmpFace->material != NULL) 729 { 730 if (this->currentGroup->faceMode != -1) 731 glEnd(); 732 this->currentGroup->faceMode = 0; 733 Material* tmpMat; 734 if (tmpFace->material != NULL) 735 { 736 tmpFace->material->select(); 737 PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getName()); 738 } 739 } 740 741 else if (tmpFace->vertexCount == 3) 742 { 743 if (this->currentGroup->faceMode != 3) 744 { 745 if (this->currentGroup->faceMode != -1) 746 glEnd(); 747 glBegin(GL_TRIANGLES); 748 } 749 750 this->currentGroup->faceMode = 3; 751 PRINTF(5)("found triag.\n"); 752 } 753 754 else if (tmpFace->vertexCount == 4) 755 { 756 if (this->currentGroup->faceMode != 4) 757 { 758 if (this->currentGroup->faceMode != -1) 759 glEnd(); 760 glBegin(GL_QUADS); 761 } 762 this->currentGroup->faceMode = 4; 763 PRINTF(5)("found quad.\n"); 764 } 765 766 else if (tmpFace->vertexCount > 4) 767 { 768 if (this->currentGroup->faceMode != -1) 769 glEnd(); 770 glBegin(GL_POLYGON); 771 PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount); 772 this->currentGroup->faceMode = tmpFace->vertexCount; 773 } 774 775 ModelFaceElement* tmpElem = tmpFace->firstElem; 776 while (tmpElem != NULL) 777 { 778 // PRINTF(2)("%s\n", tmpElem->value); 779 this->addGLElement(tmpElem); 780 tmpElem = tmpElem->next; 781 } 782 tmpFace = tmpFace->next; 783 } 806 784 glEnd(); 807 785 glEndList(); 808 786 809 787 this->currentGroup = this->currentGroup->next; 810 } 788 } 811 789 } 812 790 … … 832 810 833 811 /** 834 \brief Adds a Face-element (one vertex of a face) with all its information. 812 \brief Adds a Face-element (one vertex of a face) with all its information. 835 813 \param elem The FaceElement to add to the OpenGL-environment. 836 814 … … 848 826 { 849 827 if (likely(elem->texCoordNumber < this->texCoordCount)) 850 828 glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2); 851 829 else 852 853 830 PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n", 831 elem->texCoordNumber, this->texCoordCount); 854 832 } 855 833 if (elem->normalNumber != -1) … … 858 836 glNormal3fv(this->normals->getArray() + elem->normalNumber * 3); 859 837 else 860 861 elem->normalNumber, this->normalCount); 838 PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete", 839 elem->normalNumber, this->normalCount); 862 840 } 863 841 if (elem->vertexNumber != -1) 864 842 { 865 843 if (likely(elem->vertexNumber < this->vertexCount)) 866 844 glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3); 867 845 else 868 869 elem->vertexNumber, this->vertexCount); 870 } 846 PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete", 847 elem->vertexNumber, this->vertexCount); 848 } 871 849 872 850 } -
orxonox/trunk/src/lib/graphics/importer/model.h
r4529 r4577 7 7 #define _MODEL_H 8 8 9 #include "base_object.h" 9 10 #include "material.h" 10 11 #include "glincl.h" 12 #include "array.h" 13 11 14 12 15 // FORWARD DEFINITION // … … 19 22 MODEL_DISPLAY_LIST means, that a DisplayList will be built out of the model. This model will be STATIC, meaning it cannot be changed after initialisation. 20 23 MODEL_VERTEX_ARRAY means, that a VertexArray will be built out of the model. This moel will be DYNAMIX, meaning that one can change the properties from outside of the model. 24 * \todo implement this stuff 21 25 */ 22 26 typedef enum MODEL_TYPE {MODEL_DISPLAY_LIST, 23 27 MODEL_VERTEX_ARRAY}; 24 28 25 29 … … 30 34 31 35 //! an enumerator for VERTEX_FORMAT 32 typedef enum VERTEX_FORMAT { VERTEX_ONLY = VERTEX,33 34 35 VERTEX_TEXCOORD_NORMAL = NORMAL | TEXCOORD};36 typedef enum VERTEX_FORMAT { VERTEX_ONLY = VERTEX, 37 VERTEX_NORMAL = NORMAL, 38 VERTEX_TEXCOORD = TEXCOORD, 39 VERTEX_TEXCOORD_NORMAL = NORMAL | TEXCOORD }; 36 40 37 41 //////////////////// … … 45 49 ~ModelFaceElement(); 46 50 47 int vertexNumber; //!< The number of the Vertex out of the Array* vertices, this vertex points to.48 int normalNumber; //!< The number of the Normal out of the Array* normals, this vertex points to.49 int texCoordNumber; //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to.51 int vertexNumber; //!< The number of the Vertex out of the Array* vertices, this vertex points to. 52 int normalNumber; //!< The number of the Normal out of the Array* normals, this vertex points to. 53 int texCoordNumber; //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to. 50 54 51 ModelFaceElement* next; //!< Point to the next FaceElement in this List.55 ModelFaceElement* next; //!< Point to the next FaceElement in this List. 52 56 }; 53 57 … … 58 62 ModelFace(); 59 63 ~ModelFace(); 60 61 intvertexCount; //!< The Count of vertices this Face has.64 65 unsigned int vertexCount; //!< The Count of vertices this Face has. 62 66 ModelFaceElement* firstElem; //!< Points to the first Vertex (FaceElement) of this Face. 63 67 Material* material; //!< The Material to use. 64 68 65 69 ModelFace* next; //!< Pointer to the next Face. 66 }; 70 }; 67 71 68 72 //! Group to handle multiple Models per obj-file. … … 82 86 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 87 int faceCount; //!< The Number of Faces this Group holds. 84 88 85 89 ModelGroup* next; //!< Pointer to the next Group. 86 90 }; … … 91 95 92 96 //! Class that handles 3D-Models. it can also read them in and display them. 93 class Model 97 class Model : public BaseObject 94 98 { 95 99 public: … … 97 101 virtual ~Model(void); 98 102 99 void setName(const char* name);100 /** \returns the Name of the Model */101 inline const char* getName() {return this->name;}102 103 103 void draw(void) const; 104 104 void draw(int groupNumber) const; … … 106 106 107 107 /** \returns Count of the Models (Groups) in this File */ 108 inline int getGroupCount(void) const {return this->groupCount;} 108 inline int getGroupCount(void) const { return this->groupCount; }; 109 110 /** \returns a Pointer to the Vertex-Array, if it was deleted it returns NULL */ 111 inline const GLfloat* getVertexArray(void) const { return this->vertices->getArray(); }; 112 /** \returns the VertexCount of this Model */ 113 inline unsigned int getVertexArrayCount(void) const { return this->vertexCount; }; 114 115 /** \returns a Pointer to the Normals-Array, if it was deleted it returns NULL */ 116 inline const GLfloat* getNormalsArray(void) const { return this->normals->getArray(); }; 117 /** \returns the NormalsCount of this Model */ 118 inline unsigned int getNormalsArrayCount(void) const { return this->normalCount; }; 119 120 /** \returns a Pointer to the TexCoord-Array, if it was deleted it returns NULL */ 121 inline const GLfloat* getTexCoordArray(void) const { return this->vTexture->getArray(); }; 122 /** \returns the TexCoord-Count of this Model */ 123 inline unsigned int getTexCoordArrayCount(void) const { return this->texCoordCount; }; 124 109 125 110 126 Material* addMaterial(Material* material); … … 152 168 153 169 private: 154 char* name; //!< This is the name of the Model.155 170 MODEL_TYPE type; //!< A type for the Model 156 171 bool finalized; //!< Sets the Object to be finalized. 157 172 158 intvertexCount; //!< A modelwide Counter for vertices.159 intnormalCount; //!< A modelwide Counter for the normals.160 inttexCoordCount; //!< A modelwide Counter for the texCoord.173 unsigned int vertexCount; //!< A modelwide Counter for vertices. 174 unsigned int normalCount; //!< A modelwide Counter for the normals. 175 unsigned int texCoordCount; //!< A modelwide Counter for the texCoord. 161 176 Array* vertices; //!< The Array that handles the Vertices. 162 177 Array* normals; //!< The Array that handles the Normals.
Note: See TracChangeset
for help on using the changeset viewer.