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