| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
|---|
| 17 | |
|---|
| 18 | #include "vertex_array_model.h" |
|---|
| 19 | |
|---|
| 20 | #include "stdlibincl.h" |
|---|
| 21 | #include <stdarg.h> |
|---|
| 22 | |
|---|
| 23 | using namespace std; |
|---|
| 24 | |
|---|
| 25 | ///////////// |
|---|
| 26 | /// MODEL /// |
|---|
| 27 | ///////////// |
|---|
| 28 | /** |
|---|
| 29 | * @brief Creates a 3D-VertexArrayModel. |
|---|
| 30 | * |
|---|
| 31 | * assigns it a Name and a Type |
|---|
| 32 | */ |
|---|
| 33 | VertexArrayModel::VertexArrayModel() |
|---|
| 34 | { |
|---|
| 35 | this->setClassID(CL_MODEL, "VertexArrayModel"); |
|---|
| 36 | |
|---|
| 37 | this->finalized = false; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * @brief deletes an VertexArrayModel. |
|---|
| 43 | * |
|---|
| 44 | * Looks if any from model allocated space is still in use, and if so deleted it. |
|---|
| 45 | */ |
|---|
| 46 | VertexArrayModel::~VertexArrayModel() |
|---|
| 47 | { |
|---|
| 48 | PRINTF(4)("Deleting VertexArrayModel "); |
|---|
| 49 | if (this->getName()) |
|---|
| 50 | { |
|---|
| 51 | PRINT(4)("%s\n", this->getName()); |
|---|
| 52 | } |
|---|
| 53 | else |
|---|
| 54 | { |
|---|
| 55 | PRINT(4)("\n"); |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * @brief Finalizes an Object. This can be done outside of the Class. |
|---|
| 62 | */ |
|---|
| 63 | void VertexArrayModel::finalize() |
|---|
| 64 | { |
|---|
| 65 | // this creates the display List. |
|---|
| 66 | this->importToVertexArray(); |
|---|
| 67 | |
|---|
| 68 | this->finalized = true; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | ////////// |
|---|
| 73 | // DRAW // |
|---|
| 74 | ////////// |
|---|
| 75 | /** |
|---|
| 76 | * Draws the VertexArrayModels of all Groups. |
|---|
| 77 | It does this by just calling the Lists that must have been created earlier. |
|---|
| 78 | */ |
|---|
| 79 | void VertexArrayModel::draw () const |
|---|
| 80 | { |
|---|
| 81 | PRINTF(4)("drawing the 3D-VertexArrayModels\n"); |
|---|
| 82 | |
|---|
| 83 | glEnableClientState(GL_VERTEX_ARRAY); |
|---|
| 84 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|---|
| 85 | glEnableClientState(GL_NORMAL_ARRAY); |
|---|
| 86 | // glEnableClientState(GL_INDEX_ARRAY); |
|---|
| 87 | |
|---|
| 88 | glVertexPointer(3, GL_FLOAT, 0, this->vertices.getArray()); |
|---|
| 89 | glNormalPointer(GL_FLOAT, 0, this->normals.getArray()); |
|---|
| 90 | glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords.getArray()); |
|---|
| 91 | |
|---|
| 92 | printf("%d\n", this->indices.getCount()); |
|---|
| 93 | glDrawElements(GL_TRIANGLE_STRIP, |
|---|
| 94 | this->indices.getCount(), |
|---|
| 95 | GL_UNSIGNED_BYTE, |
|---|
| 96 | this->indices.getArray()); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | /* const GLfloat* pVertices = NULL; |
|---|
| 101 | const GLfloat* pNorm = NULL; |
|---|
| 102 | |
|---|
| 103 | glBegin(GL_TRIANGLES); |
|---|
| 104 | for( int i = 0; i < this->triangleCount; ++i) |
|---|
| 105 | { |
|---|
| 106 | //printf("int i = %i\n", i); |
|---|
| 107 | pNorm = &this->normals->getArray()[this->triangles[i].indexToNormals[0]]; |
|---|
| 108 | pVertices = &this->vertices->getArray()[this->triangles[i].indexToVertices[0]]; |
|---|
| 109 | glNormal3f(pNorm[0], pNorm[1], pNorm[2]); |
|---|
| 110 | glVertex3f(pVertices[0], pVertices[1], pVertices[2]); |
|---|
| 111 | |
|---|
| 112 | pNorm = &this->normals->getArray()[this->triangles[i].indexToNormals[1]]; |
|---|
| 113 | pVertices = &this->vertices->getArray()[this->triangles[i].indexToVertices[1]]; |
|---|
| 114 | glNormal3f(pNorm[0], pNorm[1], pNorm[2]); |
|---|
| 115 | glVertex3f(pVertices[0], pVertices[1], pVertices[2]); |
|---|
| 116 | |
|---|
| 117 | pNorm = &this->normals->getArray()[this->triangles[i].indexToNormals[2]]; |
|---|
| 118 | pVertices = &this->vertices->getArray()[this->triangles[i].indexToVertices[2]]; |
|---|
| 119 | glNormal3f(pNorm[0], pNorm[1], pNorm[2]); |
|---|
| 120 | glVertex3f(pVertices[0], pVertices[1], pVertices[2]); |
|---|
| 121 | |
|---|
| 122 | } |
|---|
| 123 | glEnd(); |
|---|
| 124 | */ |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | ////////// |
|---|
| 129 | // MESH // |
|---|
| 130 | ////////// |
|---|
| 131 | /** |
|---|
| 132 | * @brief parses a vertex-String |
|---|
| 133 | * @param x the X-coordinate of the Vertex to add. |
|---|
| 134 | * @param y the Y-coordinate of the Vertex to add. |
|---|
| 135 | * @param z the Z-coordinate of the Vertex to add. |
|---|
| 136 | */ |
|---|
| 137 | void VertexArrayModel::addVertex(float x, float y, float z) |
|---|
| 138 | { |
|---|
| 139 | this->vertices.addEntry(x, y, z); |
|---|
| 140 | this->pModelInfo.numVertices++; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | * @brief adds a VertexNormal. |
|---|
| 146 | * @param x The x coordinate of the Normal. |
|---|
| 147 | * @param y The y coordinate of the Normal. |
|---|
| 148 | * @param z The z coordinate of the Normal. |
|---|
| 149 | * |
|---|
| 150 | * If a vertexNormal line is found this function will inject it into the vertexNormal-Array |
|---|
| 151 | */ |
|---|
| 152 | void VertexArrayModel::addNormal(float x, float y, float z) |
|---|
| 153 | { |
|---|
| 154 | this->normals.addEntry(x, y, z); |
|---|
| 155 | this->pModelInfo.numNormals++; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * adds a Texture Coordinate |
|---|
| 161 | * @param u The u coordinate of the TextureCoordinate. |
|---|
| 162 | * @param v The y coordinate of the TextureCoordinate. |
|---|
| 163 | * |
|---|
| 164 | * If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array |
|---|
| 165 | */ |
|---|
| 166 | void VertexArrayModel::addTexCoor(float u, float v) |
|---|
| 167 | { |
|---|
| 168 | this->texCoords.addEntry(u); |
|---|
| 169 | this->texCoords.addEntry(v); |
|---|
| 170 | this->pModelInfo.numTexCoor++; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * adds a new Face |
|---|
| 176 | * @param faceElemCount the number of Vertices to add to the Face. |
|---|
| 177 | * @param type The information Passed with each Vertex |
|---|
| 178 | */ |
|---|
| 179 | void VertexArrayModel::addIndice(GLubyte indice) |
|---|
| 180 | { |
|---|
| 181 | this->indices.addEntry(indice); |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | //////////// |
|---|
| 187 | // openGL // |
|---|
| 188 | //////////// |
|---|
| 189 | /** |
|---|
| 190 | * reads and includes the Faces/Materials into the openGL state Machine |
|---|
| 191 | */ |
|---|
| 192 | void VertexArrayModel::importToVertexArray() |
|---|
| 193 | { |
|---|
| 194 | // finalize the Arrays |
|---|
| 195 | this->vertices.finalizeArray(); |
|---|
| 196 | this->texCoords.finalizeArray(); |
|---|
| 197 | this->normals.finalizeArray(); |
|---|
| 198 | this->indices.finalizeArray(); |
|---|
| 199 | |
|---|
| 200 | // glEnableClientState(GL_VERTEX_ARRAY); |
|---|
| 201 | // glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|---|
| 202 | // glEnableClientState(GL_NORMAL_ARRAY); |
|---|
| 203 | // glEnableClientState(GL_INDEX_ARRAY); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | ///////////// |
|---|
| 209 | // TESTING // |
|---|
| 210 | ///////////// |
|---|
| 211 | /** |
|---|
| 212 | * Includes a default model |
|---|
| 213 | * |
|---|
| 214 | * This will inject a Cube, because this is the most basic model. |
|---|
| 215 | */ |
|---|
| 216 | void VertexArrayModel::cubeModel() |
|---|
| 217 | { |
|---|
| 218 | unsigned int i, j; |
|---|
| 219 | for (i = 0; i < 20; i++) |
|---|
| 220 | { |
|---|
| 221 | for (j = 0; j < 20; j++) |
|---|
| 222 | { |
|---|
| 223 | this->addVertex(i* 50, .5, (j)*50); |
|---|
| 224 | this->addNormal(0, 1, 0); |
|---|
| 225 | this->addTexCoor((float)i/20.0, (float)j/20.0); |
|---|
| 226 | |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | for (i = 0; i < 20; i++) |
|---|
| 230 | { |
|---|
| 231 | this->addIndice(i); |
|---|
| 232 | this->addIndice(i+20); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | /* |
|---|
| 236 | this->addVertex (-0.5, -0.5, 0.5); |
|---|
| 237 | this->addVertex (0.5, -0.5, 0.5); |
|---|
| 238 | this->addVertex (-0.5, 0.5, 0.5); |
|---|
| 239 | this->addVertex (0.5, 0.5, 0.5); |
|---|
| 240 | this->addVertex (-0.5, 0.5, -0.5); |
|---|
| 241 | this->addVertex (0.5, 0.5, -0.5); |
|---|
| 242 | this->addVertex (-0.5, -0.5, -0.5); |
|---|
| 243 | this->addVertex (0.5, -0.5, -0.5); |
|---|
| 244 | |
|---|
| 245 | this->addTexCoor (0.0, 0.0); |
|---|
| 246 | this->addTexCoor (1.0, 0.0); |
|---|
| 247 | this->addTexCoor (0.0, 1.0); |
|---|
| 248 | this->addTexCoor (1.0, 1.0); |
|---|
| 249 | this->addTexCoor (0.0, 2.0); |
|---|
| 250 | this->addTexCoor (1.0, 2.0); |
|---|
| 251 | this->addTexCoor (0.0, 3.0); |
|---|
| 252 | this->addTexCoor (1.0, 3.0); |
|---|
| 253 | this->addTexCoor (0.0, 4.0); |
|---|
| 254 | this->addTexCoor (1.0, 4.0); |
|---|
| 255 | this->addTexCoor (2.0, 0.0); |
|---|
| 256 | this->addTexCoor (2.0, 1.0); |
|---|
| 257 | this->addTexCoor (-1.0, 0.0); |
|---|
| 258 | this->addTexCoor (-1.0, 1.0); |
|---|
| 259 | |
|---|
| 260 | this->addNormal (0.0, 0.0, 1.0); |
|---|
| 261 | this->addNormal (0.0, 0.0, 1.0); |
|---|
| 262 | this->addNormal (0.0, 0.0, 1.0); |
|---|
| 263 | this->addNormal (0.0, 0.0, 1.0); |
|---|
| 264 | this->addNormal (0.0, 1.0, 0.0); |
|---|
| 265 | this->addNormal (0.0, 1.0, 0.0); |
|---|
| 266 | this->addNormal (0.0, 1.0, 0.0); |
|---|
| 267 | this->addNormal (0.0, 1.0, 0.0); |
|---|
| 268 | this->addNormal (0.0, 0.0, -1.0); |
|---|
| 269 | this->addNormal (0.0, 0.0, -1.0); |
|---|
| 270 | this->addNormal (0.0, 0.0, -1.0); |
|---|
| 271 | this->addNormal (0.0, 0.0, -1.0); |
|---|
| 272 | this->addNormal (0.0, -1.0, 0.0); |
|---|
| 273 | this->addNormal (0.0, -1.0, 0.0); |
|---|
| 274 | this->addNormal (0.0, -1.0, 0.0); |
|---|
| 275 | this->addNormal (0.0, -1.0, 0.0); |
|---|
| 276 | this->addNormal (1.0, 0.0, 0.0); |
|---|
| 277 | this->addNormal (1.0, 0.0, 0.0); |
|---|
| 278 | this->addNormal (1.0, 0.0, 0.0); |
|---|
| 279 | this->addNormal (1.0, 0.0, 0.0); |
|---|
| 280 | this->addNormal (-1.0, 0.0, 0.0); |
|---|
| 281 | this->addNormal (-1.0, 0.0, 0.0); |
|---|
| 282 | this->addNormal (-1.0, 0.0, 0.0); |
|---|
| 283 | this->addNormal (-1.0, 0.0, 0.0); |
|---|
| 284 | */ |
|---|
| 285 | } |
|---|