| 1 | /*! | 
|---|
| 2 |   \file model.h | 
|---|
| 3 |   \brief Contains the Model Class that handles 3D-Models | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | #ifndef _MODEL_H | 
|---|
| 7 | #define _MODEL_H | 
|---|
| 8 |  | 
|---|
| 9 | #include "abstract_model.h" | 
|---|
| 10 | #include "base_object.h" | 
|---|
| 11 | #include "material.h" | 
|---|
| 12 | #include "glincl.h" | 
|---|
| 13 | #include "array.h" | 
|---|
| 14 |  | 
|---|
| 15 |  | 
|---|
| 16 | // FORWARD DEFINITION // | 
|---|
| 17 | class Vector; | 
|---|
| 18 | template<class T> class Array; | 
|---|
| 19 | template<class T> class tList; | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | //! an enumerator fot the different Model Types. | 
|---|
| 23 | /** | 
|---|
| 24 |    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. | 
|---|
| 25 |    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. | 
|---|
| 26 |  * @todo implement this stuff | 
|---|
| 27 | */ | 
|---|
| 28 | typedef enum MODEL_TYPE { | 
|---|
| 29 |   MODEL_DISPLAY_LIST, | 
|---|
| 30 |   MODEL_VERTEX_ARRAY | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | // definition of different modes for setting up Faces | 
|---|
| 35 | #define VERTEX 0       //!< If Faces are created WITH Vertex-Coordinate | 
|---|
| 36 | #define NORMAL 1       //!< If Faces are created WITH Normals (otherwise autocalculate) | 
|---|
| 37 | #define TEXCOORD 2     //!< If Faces are created WITH TextureCoordinate | 
|---|
| 38 |  | 
|---|
| 39 | //! an enumerator for VERTEX_FORMAT | 
|---|
| 40 | typedef enum VERTEX_FORMAT { | 
|---|
| 41 |   VERTEX_ONLY = VERTEX, | 
|---|
| 42 |   VERTEX_NORMAL = NORMAL, | 
|---|
| 43 |   VERTEX_TEXCOORD = TEXCOORD, | 
|---|
| 44 |   VERTEX_TEXCOORD_NORMAL = NORMAL | TEXCOORD | 
|---|
| 45 | }; | 
|---|
| 46 |  | 
|---|
| 47 | //////////////////// | 
|---|
| 48 | /// SUB-ELEMENTS /// | 
|---|
| 49 | //////////////////// | 
|---|
| 50 | //! This is the placeholder of one Vertex beloning to a Face. | 
|---|
| 51 | class ModelFaceElement | 
|---|
| 52 | { | 
|---|
| 53 |  public: | 
|---|
| 54 |   ModelFaceElement(); | 
|---|
| 55 |   ~ModelFaceElement(); | 
|---|
| 56 |  | 
|---|
| 57 |   int                 vertexNumber;         //!< The number of the Vertex out of the Array* vertices, this vertex points to. | 
|---|
| 58 |   int                 normalNumber;         //!< The number of the Normal out of the Array* normals, this vertex points to. | 
|---|
| 59 |   int                 texCoordNumber;       //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to. | 
|---|
| 60 |  | 
|---|
| 61 |   ModelFaceElement*   next;                 //!< Point to the next FaceElement in this List. | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | //! This is the placeholder of a Face belonging to a Group of Faces. | 
|---|
| 65 | class ModelFace | 
|---|
| 66 | { | 
|---|
| 67 |  public: | 
|---|
| 68 |   ModelFace(); | 
|---|
| 69 |   ~ModelFace(); | 
|---|
| 70 |  | 
|---|
| 71 |   unsigned int        vertexCount;     //!< The Count of vertices this Face has. | 
|---|
| 72 |   ModelFaceElement*   firstElem;       //!< Points to the first Vertex (FaceElement) of this Face. | 
|---|
| 73 |   Material*           material;        //!< The Material to use. | 
|---|
| 74 |  | 
|---|
| 75 |   ModelFace*          next;            //!< Pointer to the next Face. | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 78 | //! Group to handle multiple Models per obj-file. | 
|---|
| 79 | class ModelGroup | 
|---|
| 80 | { | 
|---|
| 81 |  public: | 
|---|
| 82 |   ModelGroup(); | 
|---|
| 83 |   ~ModelGroup(); | 
|---|
| 84 |  | 
|---|
| 85 |   void cleanup(); | 
|---|
| 86 |  | 
|---|
| 87 |   char*        name;           //!< the Name of the Group. this is an identifier, that can be accessed via the draw (char* name) function. | 
|---|
| 88 |   GLubyte*     indices;        //!< The indices of the Groups. Needed for vertex-arrays | 
|---|
| 89 |   GLuint       listNumber;     //!< The number of the GL-List this Group gets. | 
|---|
| 90 |   ModelFace*   firstFace;      //!< The first Face in this group. | 
|---|
| 91 |   ModelFace*   currentFace;    //!< The current Face in this Group (the one we are currently working with.) | 
|---|
| 92 |   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... | 
|---|
| 93 |   int          faceCount;      //!< The Number of Faces this Group holds. | 
|---|
| 94 |  | 
|---|
| 95 |   ModelGroup*  next;           //!< Pointer to the next Group. | 
|---|
| 96 | }; | 
|---|
| 97 |  | 
|---|
| 98 | ///////////// | 
|---|
| 99 | /// MODEL /// | 
|---|
| 100 | ///////////// | 
|---|
| 101 |  | 
|---|
| 102 | //! Class that handles 3D-Models. it can also read them in and display them. | 
|---|
| 103 | class Model : public AbstractModel | 
|---|
| 104 | { | 
|---|
| 105 |  public: | 
|---|
| 106 |   Model(const char* modelName = NULL, MODEL_TYPE type = MODEL_DISPLAY_LIST); | 
|---|
| 107 |   virtual ~Model(); | 
|---|
| 108 |  | 
|---|
| 109 |   void draw() const; | 
|---|
| 110 |   void draw(int groupNumber) const; | 
|---|
| 111 |   void draw(char* groupName) const; | 
|---|
| 112 |  | 
|---|
| 113 |   /** @returns Count of the Models (Groups) in this File */ | 
|---|
| 114 |   inline int getGroupCount() const { return this->groupCount; }; | 
|---|
| 115 |  | 
|---|
| 116 |   /** @returns a Pointer to the Vertex-Array, if it was deleted it returns NULL */ | 
|---|
| 117 |   inline const GLfloat* getVertexArray() const { return this->vertices->getArray(); }; | 
|---|
| 118 |   /** @returns the VertexCount of this Model */ | 
|---|
| 119 |   inline unsigned int getVertexCount() const { return this->vertexCount; }; | 
|---|
| 120 |  | 
|---|
| 121 |   /** @returns a Pointer to the Normals-Array, if it was deleted it returns NULL */ | 
|---|
| 122 |   inline const GLfloat* getNormalsArray() const { return this->normals->getArray(); }; | 
|---|
| 123 |   /** @returns the NormalsCount of this Model */ | 
|---|
| 124 |   inline unsigned int getNormalsCount() const { return this->normalCount; }; | 
|---|
| 125 |  | 
|---|
| 126 |   /** @returns a Pointer to the TexCoord-Array, if it was deleted it returns NULL */ | 
|---|
| 127 |   inline const GLfloat* getTexCoordArray() const { return this->vTexture->getArray(); }; | 
|---|
| 128 |   /** @returns the TexCoord-Count of this Model */ | 
|---|
| 129 |   inline unsigned int getTexCoordCount() const { return this->texCoordCount; }; | 
|---|
| 130 |  | 
|---|
| 131 |   /** @returns the Count of Faces of this Model */ | 
|---|
| 132 |   inline unsigned int getFaceCount() const { return this->faceCount; }; | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|
| 135 |   Material* addMaterial(Material* material); | 
|---|
| 136 |   Material* addMaterial(const char* materialName); | 
|---|
| 137 |  | 
|---|
| 138 |   bool addGroup(const char* groupString); | 
|---|
| 139 |   bool addVertex(const char* vertexString); | 
|---|
| 140 |   bool addVertex(float x, float y, float z); | 
|---|
| 141 |   bool addFace(const char* faceString); | 
|---|
| 142 |   bool addFace(int faceElemCount, VERTEX_FORMAT type, ...); | 
|---|
| 143 |   bool addVertexNormal(const char* normalString); | 
|---|
| 144 |   bool addVertexNormal(float x, float y, float z); | 
|---|
| 145 |   bool addVertexTexture(const char* vTextureString); | 
|---|
| 146 |   bool addVertexTexture(float u, float v); | 
|---|
| 147 |   bool setMaterial(const char* mtlString); | 
|---|
| 148 |   bool setMaterial(Material* mtl); | 
|---|
| 149 |   void finalize(); | 
|---|
| 150 |  | 
|---|
| 151 |  | 
|---|
| 152 |  protected: | 
|---|
| 153 |   void cubeModel(); | 
|---|
| 154 |  | 
|---|
| 155 |   Material* findMaterialByName(const char* materialName); | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 |  protected: | 
|---|
| 159 |   float            scaleFactor;     //!< The Factor with which the Model should be scaled. @todo maybe one wants to scale the Model after Initialisation | 
|---|
| 160 |  | 
|---|
| 161 |  private: | 
|---|
| 162 |   bool buildVertexNormals(); | 
|---|
| 163 |  | 
|---|
| 164 |   bool importToDisplayList(); | 
|---|
| 165 |   bool buildTriangleList(); | 
|---|
| 166 |   bool addGLElement(ModelFaceElement* elem); | 
|---|
| 167 |  | 
|---|
| 168 |   bool importToVertexArray(); | 
|---|
| 169 |  | 
|---|
| 170 |   bool deleteArrays(); | 
|---|
| 171 |   bool cleanup(); | 
|---|
| 172 |  | 
|---|
| 173 |  private: | 
|---|
| 174 |   MODEL_TYPE       type;            //!< A type for the Model | 
|---|
| 175 |   bool             finalized;       //!< Sets the Object to be finalized. | 
|---|
| 176 |  | 
|---|
| 177 |   unsigned int     vertexCount;     //!< A modelwide Counter for vertices. | 
|---|
| 178 |   unsigned int     normalCount;     //!< A modelwide Counter for the normals. | 
|---|
| 179 |   unsigned int     texCoordCount;   //!< A modelwide Counter for the texCoord. | 
|---|
| 180 |   unsigned int     faceCount;       //!< A modelwide Counter for the faces | 
|---|
| 181 |   unsigned int     triangleCount;   //!< Number of triangles >= faceCount | 
|---|
| 182 |   Array<GLfloat>*  vertices;        //!< The Array that handles the Vertices. | 
|---|
| 183 |   Array<GLfloat>*  normals;         //!< The Array that handles the Normals. | 
|---|
| 184 |   Array<GLfloat>*  vTexture;        //!< The Array that handles the VertexTextureCoordinates. | 
|---|
| 185 |   sTriangleExt*    triangles;       //!< The Array of triangles in the abstract_model.h style | 
|---|
| 186 |  | 
|---|
| 187 |   ModelGroup*      firstGroup;      //!< The first of all groups. | 
|---|
| 188 |   ModelGroup*      currentGroup;    //!< The currentGroup. this is the one we will work with. | 
|---|
| 189 |   int              groupCount;      //!< The Count of Groups. | 
|---|
| 190 |  | 
|---|
| 191 |   tList<Material>* materialList;    //!< A list for all the Materials in this Model | 
|---|
| 192 |   bool             materialsExtern; //!< If the materials given to this Object are extern. | 
|---|
| 193 | }; | 
|---|
| 194 |  | 
|---|
| 195 | #endif | 
|---|