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