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