[4577] | 1 | /* |
---|
[2823] | 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 | |
---|
[3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 17 | |
---|
[6010] | 18 | #include "vertex_array_model.h" |
---|
[3427] | 19 | |
---|
[3418] | 20 | #include <stdarg.h> |
---|
[8362] | 21 | #include "debug.h" |
---|
[3398] | 22 | |
---|
[6308] | 23 | #include "tc.h" |
---|
| 24 | |
---|
[2776] | 25 | |
---|
[9869] | 26 | ObjectListDefinition(VertexArrayModel); |
---|
[9406] | 27 | |
---|
[4022] | 28 | ///////////// |
---|
| 29 | /// MODEL /// |
---|
| 30 | ///////////// |
---|
[2842] | 31 | /** |
---|
[6010] | 32 | * @brief Creates a 3D-VertexArrayModel. |
---|
| 33 | * |
---|
| 34 | * assigns it a Name and a Type |
---|
| 35 | */ |
---|
| 36 | VertexArrayModel::VertexArrayModel() |
---|
[3398] | 37 | { |
---|
[9869] | 38 | this->registerObject(this, VertexArrayModel::_objectList); |
---|
[3909] | 39 | |
---|
[6037] | 40 | this->newStripe(); |
---|
[6010] | 41 | } |
---|
[4577] | 42 | |
---|
[6308] | 43 | /** |
---|
| 44 | * @brief special copy constructor for converting Models to VertexArray-Stripes |
---|
| 45 | * @param model the Model to produce a VertexArray model from. |
---|
| 46 | * |
---|
| 47 | * Code that uses Brad Granthams |
---|
| 48 | * excelent TC-code for generating stripes out of a mix of ModelCoordinates. |
---|
| 49 | */ |
---|
| 50 | VertexArrayModel::VertexArrayModel(const Model& model) |
---|
| 51 | { |
---|
[9869] | 52 | this->registerObject(this, VertexArrayModel::_objectList); |
---|
[3909] | 53 | |
---|
[6452] | 54 | // importing the data to the new Model. |
---|
[6313] | 55 | this->newStripe(); |
---|
| 56 | |
---|
| 57 | for (unsigned int i = 0; i < model.getVertexCount()*3; i+=3) |
---|
| 58 | this->addVertex(model.getVertexArray()[i], model.getVertexArray()[i+1], model.getVertexArray()[i+2]); |
---|
| 59 | for (unsigned int i = 0; i < model.getVertexCount()*3; i+=3) |
---|
[6314] | 60 | this->addColor((float)i / (float)model.getVertexCount(), 0, 0); |
---|
| 61 | |
---|
[6313] | 62 | for (unsigned int i = 0; i < model.getNormalsCount()*3; i+=3) |
---|
[6314] | 63 | this->addNormal(model.getNormalsArray()[i], model.getNormalsArray()[i+1], model.getNormalsArray()[i+2]); |
---|
| 64 | for (unsigned int i = 0; i < model.getTexCoordCount(); i+=2) |
---|
| 65 | this->addTexCoor(model.getTexCoordArray()[i], model.getTexCoordArray()[i+1]); |
---|
[6313] | 66 | |
---|
| 67 | |
---|
[6308] | 68 | // The acTC object generating this Model. // |
---|
| 69 | ACTCData *tc; |
---|
| 70 | tc = actcNew(); |
---|
| 71 | if(tc == NULL) { |
---|
| 72 | /* memory allocation failed */ |
---|
| 73 | /* print error here and exit or whatever */ |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | // inputing the data of model to the tc |
---|
| 77 | actcBeginInput(tc); |
---|
| 78 | for(unsigned int i = 0; i < model.getTriangleCount(); i++) |
---|
[6313] | 79 | { |
---|
[6310] | 80 | actcAddTriangle(tc, |
---|
| 81 | model.getTriangles()[i].indexToVertices[0], |
---|
| 82 | model.getTriangles()[i].indexToVertices[1], |
---|
| 83 | model.getTriangles()[i].indexToVertices[2]); |
---|
[6313] | 84 | } |
---|
[6308] | 85 | actcEndInput(tc); |
---|
| 86 | |
---|
[6309] | 87 | |
---|
| 88 | |
---|
[6308] | 89 | int prim; |
---|
[6477] | 90 | unsigned int v1, v2, v3; |
---|
[6308] | 91 | |
---|
| 92 | actcBeginOutput(tc); |
---|
| 93 | while((prim = actcStartNextPrim(tc, &v1, &v2) != ACTC_DATABASE_EMPTY)) |
---|
| 94 | { |
---|
[6310] | 95 | this->newStripe(); |
---|
| 96 | |
---|
[6308] | 97 | this->addIndice(v1); |
---|
| 98 | this->addIndice(v2); |
---|
| 99 | /* start a primitive of type "prim" with v1 and v2 */ |
---|
| 100 | while(actcGetNextVert(tc, &v3) != ACTC_PRIM_COMPLETE) |
---|
| 101 | { |
---|
| 102 | /* continue primitive using v3 */ |
---|
| 103 | this->addIndice(v3); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | actcEndOutput(tc); |
---|
| 107 | |
---|
| 108 | this->finalize(); |
---|
| 109 | } |
---|
| 110 | |
---|
[6309] | 111 | |
---|
[3398] | 112 | /** |
---|
[6308] | 113 | * @brief deletes a VertexArrayModel. |
---|
[6010] | 114 | * |
---|
| 115 | * Looks if any from model allocated space is still in use, and if so deleted it. |
---|
| 116 | */ |
---|
| 117 | VertexArrayModel::~VertexArrayModel() |
---|
[2847] | 118 | { |
---|
[9406] | 119 | PRINTF(4)("Deleting VertexArrayModel %s\n", this->getCName()); |
---|
[6010] | 120 | } |
---|
[3396] | 121 | |
---|
[3140] | 122 | |
---|
[2842] | 123 | /** |
---|
[6037] | 124 | * @brief Draws the VertexArrayModels of all Groups. |
---|
| 125 | * |
---|
| 126 | * It does this by just calling the Lists that must have been created earlier. |
---|
[6010] | 127 | */ |
---|
[6037] | 128 | void VertexArrayModel::draw() const |
---|
[3398] | 129 | { |
---|
[9406] | 130 | PRINTF(4)("drawing 3D-VertexArrayModel %s\n", this->getCName()); |
---|
[6310] | 131 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
[6519] | 132 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[6310] | 133 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
| 134 | glEnableClientState(GL_COLOR_ARRAY ); |
---|
[4803] | 135 | |
---|
[4799] | 136 | |
---|
[6310] | 137 | glVertexPointer(3, GL_FLOAT, 0, &this->vertices[0]); |
---|
| 138 | glNormalPointer(GL_FLOAT, 0, &this->normals[0]); |
---|
[6519] | 139 | glTexCoordPointer(2, GL_FLOAT, 0, &this->texCoords[0]); |
---|
[6310] | 140 | glColorPointer(3, GL_FLOAT, 0, &this->colors[0]); |
---|
[4803] | 141 | |
---|
[6038] | 142 | for (GLuint i = 1; i < this->stripes.size(); ++i) |
---|
[6037] | 143 | { |
---|
[6310] | 144 | glDrawElements( GL_TRIANGLE_STRIP, |
---|
| 145 | this->stripes[i] - this->stripes[i-1], |
---|
| 146 | GL_UNSIGNED_INT, |
---|
| 147 | &this->indices[this->stripes[i-1]] ); |
---|
[6037] | 148 | } |
---|
[2748] | 149 | } |
---|
[2754] | 150 | |
---|
[3186] | 151 | |
---|
[3912] | 152 | ////////// |
---|
| 153 | // MESH // |
---|
| 154 | ////////// |
---|
[3068] | 155 | /** |
---|
[6037] | 156 | * @brief generates a new Stripe in this Model |
---|
| 157 | */ |
---|
| 158 | void VertexArrayModel::newStripe() |
---|
| 159 | { |
---|
[6310] | 160 | // no stripes of size 0 |
---|
| 161 | if (this->stripes.empty() || this->indices.size() != this->stripes.back()) |
---|
| 162 | this->stripes.push_back(this->indices.size()); |
---|
[6037] | 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | /** |
---|
[6010] | 167 | * @brief parses a vertex-String |
---|
[4836] | 168 | * @param x the X-coordinate of the Vertex to add. |
---|
| 169 | * @param y the Y-coordinate of the Vertex to add. |
---|
| 170 | * @param z the Z-coordinate of the Vertex to add. |
---|
[6010] | 171 | */ |
---|
| 172 | void VertexArrayModel::addVertex(float x, float y, float z) |
---|
[3400] | 173 | { |
---|
[6453] | 174 | this->vertices.push_back(Vector(x,y,z)); |
---|
[6010] | 175 | this->pModelInfo.numVertices++; |
---|
[3400] | 176 | } |
---|
| 177 | |
---|
[3912] | 178 | |
---|
| 179 | /** |
---|
[6010] | 180 | * @brief adds a VertexNormal. |
---|
[4836] | 181 | * @param x The x coordinate of the Normal. |
---|
| 182 | * @param y The y coordinate of the Normal. |
---|
| 183 | * @param z The z coordinate of the Normal. |
---|
[6010] | 184 | * |
---|
| 185 | * If a vertexNormal line is found this function will inject it into the vertexNormal-Array |
---|
| 186 | */ |
---|
| 187 | void VertexArrayModel::addNormal(float x, float y, float z) |
---|
[3912] | 188 | { |
---|
[6453] | 189 | this->normals.push_back(Vector(x,y,z)); |
---|
[6010] | 190 | this->pModelInfo.numNormals++; |
---|
[3912] | 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | /** |
---|
[6310] | 195 | * @brief adds a Texture Coordinate |
---|
[4836] | 196 | * @param u The u coordinate of the TextureCoordinate. |
---|
| 197 | * @param v The y coordinate of the TextureCoordinate. |
---|
[6010] | 198 | * |
---|
| 199 | * If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array |
---|
| 200 | */ |
---|
| 201 | void VertexArrayModel::addTexCoor(float u, float v) |
---|
[3912] | 202 | { |
---|
[6769] | 203 | this->texCoords.push_back(Vector2D(u,v)); |
---|
[6010] | 204 | this->pModelInfo.numTexCoor++; |
---|
[3912] | 205 | } |
---|
| 206 | |
---|
[6310] | 207 | /** |
---|
| 208 | * @brief adds a new Color |
---|
| 209 | * @param r the Red Component of the VertexColor to add. |
---|
| 210 | * @param g the Green Component of the VertexColor to add. |
---|
| 211 | * @param b the Blue of the VertexColor to add. |
---|
| 212 | */ |
---|
| 213 | void VertexArrayModel::addColor(float r, float g, float b) |
---|
| 214 | { |
---|
[6453] | 215 | this->colors.push_back(Vector(r,g,b)); |
---|
[6310] | 216 | // FIXME |
---|
| 217 | } |
---|
[3186] | 218 | |
---|
[6310] | 219 | |
---|
[2842] | 220 | /** |
---|
[4836] | 221 | * adds a new Face |
---|
| 222 | * @param faceElemCount the number of Vertices to add to the Face. |
---|
| 223 | * @param type The information Passed with each Vertex |
---|
[3400] | 224 | */ |
---|
[6310] | 225 | void VertexArrayModel::addIndice(GLuint indice) |
---|
[3400] | 226 | { |
---|
[6310] | 227 | this->indices.push_back(indice); |
---|
[3400] | 228 | } |
---|
| 229 | |
---|
[4577] | 230 | |
---|
[3912] | 231 | /** |
---|
[6037] | 232 | * @brief Finalizes an Object. This can be done outside of the Class. |
---|
| 233 | */ |
---|
| 234 | void VertexArrayModel::finalize() |
---|
[3063] | 235 | { |
---|
| 236 | // finalize the Arrays |
---|
[6037] | 237 | this->newStripe(); |
---|
[3063] | 238 | } |
---|
| 239 | |
---|
[3916] | 240 | |
---|
| 241 | |
---|
[6310] | 242 | |
---|
[6010] | 243 | ///////////// |
---|
| 244 | // TESTING // |
---|
| 245 | ///////////// |
---|
[3916] | 246 | /** |
---|
[6310] | 247 | * @brief Includes a default model |
---|
| 248 | * |
---|
| 249 | * This will inject a Cube, because this is the most basic model. |
---|
| 250 | */ |
---|
| 251 | void VertexArrayModel::planeModel(float sizeX, float sizeY, unsigned int resolutionX, unsigned int resolutionY) |
---|
| 252 | { |
---|
| 253 | GLuint i, j; |
---|
| 254 | for (i = 0; i < resolutionY; i++) |
---|
| 255 | { |
---|
| 256 | for (j = 0; j < resolutionX; j++) |
---|
| 257 | { |
---|
[6766] | 258 | this->addVertex( ((float)i - (float)resolutionX/2.0)/(float)resolutionX * sizeX, |
---|
| 259 | 0.0, |
---|
| 260 | ((float)j - (float)resolutionY/2.0)/(float)resolutionY * sizeY); |
---|
[6310] | 261 | this->addNormal(0.0, 1, 0.0); |
---|
[10368] | 262 | this->addTexCoor((float)i/(float)(resolutionY-1), (float)j/(float)(resolutionX-1)); |
---|
[6771] | 263 | this->addColor(1.0, 1.0, 1.0); |
---|
[6310] | 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | for (i = 0; i < resolutionY-1; i++) |
---|
| 268 | { |
---|
| 269 | for (j = 0; j < resolutionX; j++) |
---|
| 270 | { |
---|
| 271 | this->addIndice( resolutionY*i + j ); |
---|
| 272 | this->addIndice( resolutionY*(i+1) + j ); |
---|
| 273 | } |
---|
| 274 | this->newStripe(); |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | #include <cmath> |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * @brief builds a Triangle Stripped sphere |
---|
| 282 | * @param radius: radius |
---|
| 283 | * @param loops: the count of loops |
---|
| 284 | * @param segmentsPerLoop how many Segments per loop |
---|
[4791] | 285 | */ |
---|
[6310] | 286 | void VertexArrayModel::spiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop) |
---|
[4791] | 287 | { |
---|
[6310] | 288 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
| 289 | { |
---|
| 290 | float theta = 0; |
---|
| 291 | float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop; |
---|
| 292 | float sinTheta = std::sin(theta); |
---|
| 293 | float sinPhi = std::sin(phi); |
---|
| 294 | float cosTheta = std::cos(theta); |
---|
| 295 | float cosPhi = std::cos(phi); |
---|
| 296 | this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); |
---|
| 297 | this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); |
---|
| 298 | this->addTexCoor(0,0); /// FIXME |
---|
| 299 | this->addColor(.125,.436,.246); ///FIXME |
---|
| 300 | } |
---|
[6695] | 301 | |
---|
[6310] | 302 | for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber) |
---|
| 303 | { |
---|
| 304 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
[4796] | 305 | { |
---|
[6310] | 306 | float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops)); |
---|
| 307 | if (loopNumber == loops) |
---|
| 308 | { |
---|
| 309 | theta = PI; |
---|
| 310 | } |
---|
| 311 | float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop; |
---|
| 312 | float sinTheta = std::sin(theta); |
---|
| 313 | float sinPhi = std::sin(phi); |
---|
| 314 | float cosTheta = std::cos(theta); |
---|
| 315 | float cosPhi = std::cos(phi); |
---|
| 316 | this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); |
---|
| 317 | this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); |
---|
| 318 | this->addTexCoor(0,0); //FIXME |
---|
| 319 | this->addColor(.125,.436,.246); |
---|
[4796] | 320 | |
---|
| 321 | } |
---|
[6310] | 322 | } |
---|
[6695] | 323 | |
---|
[6310] | 324 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
| 325 | { |
---|
| 326 | this->addIndice(loopSegmentNumber); |
---|
| 327 | this->addIndice(segmentsPerLoop + loopSegmentNumber); |
---|
| 328 | } |
---|
[6695] | 329 | |
---|
[6310] | 330 | for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber) |
---|
| 331 | { |
---|
| 332 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
[4793] | 333 | { |
---|
[6310] | 334 | this->addIndice( ((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber); |
---|
| 335 | this->addIndice( ((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber); |
---|
[4793] | 336 | } |
---|
[6310] | 337 | } |
---|
[2821] | 338 | } |
---|
[6310] | 339 | |
---|
| 340 | |
---|
| 341 | /** |
---|
| 342 | * @brief print out some nice debug information about this VertexArrayModel. |
---|
| 343 | */ |
---|
| 344 | void VertexArrayModel::debug() const |
---|
| 345 | { |
---|
[9406] | 346 | PRINT(0)("VertexArrayModel (%s): debug\n", this->getCName()); |
---|
[6310] | 347 | PRINT(0)("Stripes: %d; Indices: %d; Vertices: %d; Normals %d; TextCoords %d; Colors %d\n", |
---|
| 348 | this->stripes.size(), |
---|
| 349 | this->indices.size(), |
---|
[6453] | 350 | this->vertices.size(), |
---|
| 351 | this->normals.size(), |
---|
[6769] | 352 | this->texCoords.size(), |
---|
[6453] | 353 | this->colors.size() ); |
---|
[6310] | 354 | for (GLuint i = 1; i < this->stripes.size(); ++i) |
---|
| 355 | { |
---|
| 356 | PRINT(0)("Stripe-%d (s:%d:e:%d):: ", i, this->stripes[i-1], this->stripes[i]); |
---|
| 357 | for (GLuint j = this->stripes[i-1] ; j < this->stripes[i]; j++) |
---|
| 358 | { |
---|
[6314] | 359 | PRINT(0)("->%d", this->indices[j]); |
---|
[6310] | 360 | } |
---|
| 361 | PRINT(0)("\n"); |
---|
| 362 | } |
---|
| 363 | } |
---|