[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: ... |
---|
[4793] | 14 | |
---|
| 15 | 2005-07-06: (Patrick) added new function buildTriangleList() |
---|
[2823] | 16 | */ |
---|
| 17 | |
---|
[3590] | 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 19 | |
---|
[3360] | 20 | #include "model.h" |
---|
[3427] | 21 | |
---|
[5427] | 22 | #include "stdlibincl.h" |
---|
[3418] | 23 | #include <stdarg.h> |
---|
[3398] | 24 | |
---|
[3473] | 25 | #include "vector.h" |
---|
[3427] | 26 | |
---|
[3140] | 27 | using namespace std; |
---|
[2776] | 28 | |
---|
[4022] | 29 | |
---|
| 30 | //////////////////// |
---|
| 31 | /// SUB-Elements /// |
---|
| 32 | //////////////////// |
---|
[4023] | 33 | /** |
---|
[4836] | 34 | * creates a new ModelFaceElement |
---|
[4023] | 35 | */ |
---|
[4022] | 36 | ModelFaceElement::ModelFaceElement() |
---|
[4038] | 37 | { |
---|
[4109] | 38 | this->vertexNumber = -1; |
---|
| 39 | this->normalNumber = -1; |
---|
[4577] | 40 | this->texCoordNumber = -1; |
---|
[4109] | 41 | |
---|
[4038] | 42 | this->next = NULL; |
---|
| 43 | } |
---|
[4022] | 44 | |
---|
[4023] | 45 | /** |
---|
[4836] | 46 | * destroys a ModelFaceElement |
---|
[4023] | 47 | */ |
---|
[4022] | 48 | ModelFaceElement::~ModelFaceElement() |
---|
| 49 | { |
---|
[4038] | 50 | if (this->next) |
---|
[4022] | 51 | delete this->next; |
---|
| 52 | } |
---|
| 53 | |
---|
[4023] | 54 | /** |
---|
[4836] | 55 | * creates a new ModelFace |
---|
[4023] | 56 | */ |
---|
[4022] | 57 | ModelFace::ModelFace() |
---|
| 58 | { |
---|
| 59 | this->vertexCount = 0; |
---|
| 60 | |
---|
| 61 | this->firstElem = NULL; |
---|
[4577] | 62 | |
---|
[4022] | 63 | this->material = NULL; |
---|
[4577] | 64 | |
---|
[4022] | 65 | this->next = NULL; |
---|
| 66 | } |
---|
| 67 | |
---|
[4023] | 68 | /** |
---|
[4836] | 69 | * deletes a ModelFace |
---|
[4023] | 70 | */ |
---|
[4022] | 71 | ModelFace::~ModelFace() |
---|
| 72 | { |
---|
| 73 | PRINTF(5)("Cleaning up Face\n"); |
---|
| 74 | |
---|
| 75 | if (this->firstElem != NULL) |
---|
[4038] | 76 | delete this->firstElem; |
---|
[4577] | 77 | |
---|
[4022] | 78 | if (this->next != NULL) |
---|
[4038] | 79 | delete this->next; |
---|
[4022] | 80 | } |
---|
| 81 | |
---|
[4023] | 82 | /** |
---|
[4836] | 83 | * Creates a new ModelGroup |
---|
[4023] | 84 | */ |
---|
[4022] | 85 | ModelGroup::ModelGroup() |
---|
| 86 | { |
---|
| 87 | PRINTF(4)("Adding new Group\n"); |
---|
| 88 | this->name = ""; |
---|
| 89 | this->faceMode = -1; |
---|
[4577] | 90 | this->faceCount = 0; |
---|
[4022] | 91 | this->next = NULL; |
---|
[5216] | 92 | this->listNumber = 0; |
---|
[4577] | 93 | |
---|
[4022] | 94 | this->firstFace = new ModelFace; |
---|
| 95 | this->currentFace = this->firstFace; |
---|
| 96 | } |
---|
| 97 | |
---|
[4023] | 98 | /** |
---|
[4836] | 99 | * deletes a ModelGroup |
---|
[4023] | 100 | */ |
---|
[4022] | 101 | ModelGroup::~ModelGroup() |
---|
| 102 | { |
---|
| 103 | PRINTF(5)("Cleaning up group\n"); |
---|
| 104 | if (this->firstFace != NULL) |
---|
[4038] | 105 | delete this->firstFace; |
---|
[4022] | 106 | |
---|
[5217] | 107 | // deleting the glList |
---|
| 108 | if (this->listNumber != 0) |
---|
| 109 | glDeleteLists(this->listNumber, 1); |
---|
| 110 | |
---|
[5218] | 111 | if (this->name[0] != '\0') |
---|
| 112 | delete[] this->name; |
---|
| 113 | |
---|
[4022] | 114 | if (this->next !=NULL) |
---|
| 115 | delete this->next; |
---|
[5216] | 116 | |
---|
[4022] | 117 | } |
---|
| 118 | |
---|
[4023] | 119 | /** |
---|
[4836] | 120 | * cleans up a ModelGroup |
---|
[4023] | 121 | |
---|
| 122 | actually does the same as the delete Operator, but does not delete the predecessing group |
---|
| 123 | */ |
---|
[4746] | 124 | void ModelGroup::cleanup() |
---|
[4022] | 125 | { |
---|
[4023] | 126 | PRINTF(5)("Cleaning up group\n"); |
---|
[4022] | 127 | if (this->firstFace) |
---|
| 128 | delete this->firstFace; |
---|
| 129 | this->firstFace = NULL; |
---|
| 130 | if (this->next) |
---|
| 131 | this->next->cleanup(); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | ///////////// |
---|
| 136 | /// MODEL /// |
---|
| 137 | ///////////// |
---|
[2842] | 138 | /** |
---|
[4836] | 139 | * Creates a 3D-Model. |
---|
[4022] | 140 | |
---|
| 141 | assigns it a Name and a Type |
---|
[3398] | 142 | */ |
---|
[3916] | 143 | Model::Model(const char* modelName, MODEL_TYPE type) |
---|
[3398] | 144 | { |
---|
[5790] | 145 | this->setClassID(CL_MODEL, "Model"); |
---|
[4577] | 146 | PRINTF(4)("new 3D-Model is being created\n"); |
---|
[3398] | 147 | this->setName(modelName); |
---|
[3916] | 148 | this->type = type; |
---|
[3909] | 149 | |
---|
| 150 | this->finalized = false; |
---|
| 151 | // setting the start group; |
---|
[4022] | 152 | this->currentGroup = this->firstGroup = new ModelGroup; |
---|
[3909] | 153 | this->groupCount = 0; |
---|
[4106] | 154 | this->vertexCount = 0; |
---|
| 155 | this->normalCount = 0; |
---|
| 156 | this->texCoordCount = 0; |
---|
[4677] | 157 | this->faceCount = 0; |
---|
[4798] | 158 | this->triangleCount = 0; |
---|
| 159 | this->triangles = NULL; |
---|
[4577] | 160 | |
---|
[3909] | 161 | this->scaleFactor = 1; |
---|
| 162 | |
---|
[5390] | 163 | this->vertices = new tArray<GLfloat>(); |
---|
| 164 | this->vTexture = new tArray<GLfloat>(); |
---|
| 165 | this->normals = new tArray<GLfloat>(); |
---|
[3909] | 166 | |
---|
[3917] | 167 | if (this->type == MODEL_VERTEX_ARRAY) |
---|
| 168 | glEnableClientState(GL_VERTEX_ARRAY | GL_NORMAL_ARRAY | GL_TEXTURE_COORD_ARRAY); |
---|
[3398] | 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
[4836] | 172 | * deletes an Model. |
---|
[3186] | 173 | |
---|
[3360] | 174 | Looks if any from model allocated space is still in use, and if so deleted it. |
---|
[2847] | 175 | */ |
---|
[4746] | 176 | Model::~Model() |
---|
[2847] | 177 | { |
---|
[3548] | 178 | PRINTF(4)("Deleting Model "); |
---|
[4577] | 179 | if (this->getName()) |
---|
[5790] | 180 | { |
---|
| 181 | PRINT(4)("%s\n", this->getName()); |
---|
| 182 | } |
---|
[3396] | 183 | else |
---|
[5790] | 184 | { |
---|
| 185 | PRINT(4)("\n"); |
---|
| 186 | } |
---|
| 187 | this->cleanup(); |
---|
[3396] | 188 | |
---|
[3911] | 189 | PRINTF(5)("Deleting display Lists.\n"); |
---|
[4038] | 190 | delete this->firstGroup; |
---|
[3140] | 191 | |
---|
[4580] | 192 | // deleting Arrays (if not allready done by finalize) |
---|
[3916] | 193 | this->deleteArrays(); |
---|
| 194 | |
---|
[3915] | 195 | // deleting the MaterialList |
---|
[5304] | 196 | PRINTF(5)("Deleting Materials.\n"); |
---|
[4038] | 197 | |
---|
[4834] | 198 | //! @todo do we really have to delete this material?? |
---|
[5774] | 199 | list<ModelMaterial*>::iterator modMat; |
---|
| 200 | for(modMat = this->materialList.begin(); modMat != this->materialList.end(); modMat++) |
---|
[5304] | 201 | { |
---|
[5774] | 202 | if (!(*modMat)->external) |
---|
| 203 | delete (*modMat)->material; |
---|
| 204 | delete (*modMat); |
---|
[4834] | 205 | } |
---|
[2847] | 206 | } |
---|
| 207 | |
---|
[2842] | 208 | /** |
---|
[4836] | 209 | * Finalizes an Object. This can be done outside of the Class. |
---|
[3398] | 210 | */ |
---|
[4746] | 211 | void Model::finalize() |
---|
[3398] | 212 | { |
---|
[3916] | 213 | // this creates the display List. |
---|
| 214 | this->importToDisplayList(); |
---|
[4791] | 215 | this->buildTriangleList(); |
---|
[3916] | 216 | |
---|
| 217 | // deletes everything we allocated. |
---|
[4580] | 218 | //if (this->type == MODEL_DISPLAY_LIST) |
---|
| 219 | //this->deleteArrays(); |
---|
[5790] | 220 | // this->cleanup(); |
---|
[3398] | 221 | |
---|
[4804] | 222 | /* load the ModelInfo */ |
---|
[6008] | 223 | this->pModelInfo.numVertices = this->vertexCount; |
---|
| 224 | this->pModelInfo.pVertices = this->vertices->getArray(); |
---|
| 225 | this->pModelInfo.numTriangles = this->triangleCount; |
---|
| 226 | this->pModelInfo.pTriangles = this->triangles; |
---|
| 227 | this->pModelInfo.numNormals = this->normalCount; |
---|
| 228 | this->pModelInfo.pNormals = this->normals->getArray(); |
---|
| 229 | this->pModelInfo.numTexCoor = this->vTexture->getCount(); |
---|
| 230 | this->pModelInfo.pTexCoor = this->vTexture->getArray(); |
---|
[4804] | 231 | |
---|
[3398] | 232 | this->finalized = true; |
---|
| 233 | } |
---|
| 234 | |
---|
[5790] | 235 | /** |
---|
| 236 | * rebuild the Model from the Information we got. |
---|
| 237 | */ |
---|
| 238 | void Model::rebuild() |
---|
| 239 | { |
---|
| 240 | PRINTF(3)("Rebuilding Model '%s'\n", this->getName()); |
---|
| 241 | this->finalize(); |
---|
| 242 | |
---|
| 243 | } |
---|
| 244 | |
---|
[3912] | 245 | ////////// |
---|
| 246 | // DRAW // |
---|
| 247 | ////////// |
---|
[3398] | 248 | /** |
---|
[4836] | 249 | * Draws the Models of all Groups. |
---|
[2851] | 250 | It does this by just calling the Lists that must have been created earlier. |
---|
[2842] | 251 | */ |
---|
[4746] | 252 | void Model::draw () const |
---|
[2748] | 253 | { |
---|
[4803] | 254 | |
---|
[4577] | 255 | PRINTF(4)("drawing the 3D-Models\n"); |
---|
[4038] | 256 | ModelGroup* tmpGroup = this->firstGroup; |
---|
| 257 | while (tmpGroup != NULL) |
---|
[2850] | 258 | { |
---|
[4038] | 259 | PRINTF(5)("Drawing model %s\n", tmpGroup->name); |
---|
| 260 | glCallList (tmpGroup->listNumber); |
---|
| 261 | tmpGroup = tmpGroup->next; |
---|
[2850] | 262 | } |
---|
[4799] | 263 | |
---|
[4803] | 264 | |
---|
| 265 | /* const GLfloat* pVertices = NULL; |
---|
[4802] | 266 | const GLfloat* pNorm = NULL; |
---|
[4799] | 267 | |
---|
| 268 | glBegin(GL_TRIANGLES); |
---|
| 269 | for( int i = 0; i < this->triangleCount; ++i) |
---|
| 270 | { |
---|
[4802] | 271 | //printf("int i = %i\n", i); |
---|
| 272 | pNorm = &this->normals->getArray()[this->triangles[i].indexToNormals[0]]; |
---|
[4799] | 273 | pVertices = &this->vertices->getArray()[this->triangles[i].indexToVertices[0]]; |
---|
[4802] | 274 | glNormal3f(pNorm[0], pNorm[1], pNorm[2]); |
---|
[4799] | 275 | glVertex3f(pVertices[0], pVertices[1], pVertices[2]); |
---|
| 276 | |
---|
[4802] | 277 | pNorm = &this->normals->getArray()[this->triangles[i].indexToNormals[1]]; |
---|
[4799] | 278 | pVertices = &this->vertices->getArray()[this->triangles[i].indexToVertices[1]]; |
---|
[4802] | 279 | glNormal3f(pNorm[0], pNorm[1], pNorm[2]); |
---|
[4799] | 280 | glVertex3f(pVertices[0], pVertices[1], pVertices[2]); |
---|
| 281 | |
---|
[4802] | 282 | pNorm = &this->normals->getArray()[this->triangles[i].indexToNormals[2]]; |
---|
[4799] | 283 | pVertices = &this->vertices->getArray()[this->triangles[i].indexToVertices[2]]; |
---|
[4802] | 284 | glNormal3f(pNorm[0], pNorm[1], pNorm[2]); |
---|
[4799] | 285 | glVertex3f(pVertices[0], pVertices[1], pVertices[2]); |
---|
| 286 | |
---|
| 287 | } |
---|
[4803] | 288 | glEnd();*/ |
---|
[2748] | 289 | } |
---|
[2754] | 290 | |
---|
[2842] | 291 | /** |
---|
[4836] | 292 | * Draws the Model number groupNumber |
---|
| 293 | * @param groupNumber The number of the group that will be displayed. |
---|
[3186] | 294 | |
---|
[2851] | 295 | It does this by just calling the List that must have been created earlier. |
---|
| 296 | */ |
---|
[4577] | 297 | void Model::draw (int groupNumber) const |
---|
[2851] | 298 | { |
---|
[3195] | 299 | if (groupNumber >= this->groupCount) |
---|
[2852] | 300 | { |
---|
[3548] | 301 | PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, this->groupCount); |
---|
[2852] | 302 | return; |
---|
| 303 | } |
---|
[4577] | 304 | PRINTF(4)("drawing the requested 3D-Models if found.\n"); |
---|
[4038] | 305 | ModelGroup* tmpGroup = this->firstGroup; |
---|
[2851] | 306 | int counter = 0; |
---|
[4038] | 307 | while (tmpGroup != NULL) |
---|
[2851] | 308 | { |
---|
| 309 | if (counter == groupNumber) |
---|
[4577] | 310 | { |
---|
| 311 | PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name); |
---|
| 312 | glCallList (tmpGroup->listNumber); |
---|
| 313 | return; |
---|
| 314 | } |
---|
[2851] | 315 | ++counter; |
---|
[4038] | 316 | tmpGroup = tmpGroup->next; |
---|
[2851] | 317 | } |
---|
[4577] | 318 | PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->getName()); |
---|
[2851] | 319 | return; |
---|
| 320 | |
---|
| 321 | } |
---|
[2852] | 322 | |
---|
| 323 | /** |
---|
[4836] | 324 | * Draws the Model with a specific groupName |
---|
| 325 | * @param groupName The name of the group that will be displayed. |
---|
[3186] | 326 | |
---|
[2852] | 327 | It does this by just calling the List that must have been created earlier. |
---|
| 328 | */ |
---|
[3360] | 329 | void Model::draw (char* groupName) const |
---|
[2851] | 330 | { |
---|
[4577] | 331 | PRINTF(4)("drawing the requested 3D-Models if found.\n"); |
---|
[4038] | 332 | ModelGroup* tmpGroup = this->firstGroup; |
---|
| 333 | while (tmpGroup != NULL) |
---|
[2851] | 334 | { |
---|
[4038] | 335 | if (!strcmp(tmpGroup->name, groupName)) |
---|
[4577] | 336 | { |
---|
| 337 | PRINTF(4)("Drawing model %s\n", tmpGroup->name); |
---|
| 338 | glCallList (tmpGroup->listNumber); |
---|
| 339 | return; |
---|
| 340 | } |
---|
[4038] | 341 | tmpGroup = tmpGroup->next; |
---|
[2851] | 342 | } |
---|
[4577] | 343 | PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->getName()); |
---|
[2851] | 344 | return; |
---|
| 345 | } |
---|
| 346 | |
---|
[3912] | 347 | ////////// |
---|
| 348 | // INIT // |
---|
| 349 | ////////// |
---|
[2851] | 350 | /** |
---|
[4836] | 351 | * deletes all the arrays |
---|
[3066] | 352 | */ |
---|
[4746] | 353 | bool Model::deleteArrays() |
---|
[3066] | 354 | { |
---|
[3195] | 355 | if (this->vertices) |
---|
| 356 | delete this->vertices; |
---|
| 357 | if (this->vTexture) |
---|
| 358 | delete this->vTexture; |
---|
| 359 | if (this->normals) |
---|
| 360 | delete this->normals; |
---|
[4798] | 361 | if (this->triangles) |
---|
[5211] | 362 | delete[] this->triangles; |
---|
[4038] | 363 | |
---|
[3916] | 364 | this->vertices = NULL; |
---|
| 365 | this->vTexture = NULL; |
---|
| 366 | this->normals = NULL; |
---|
[5211] | 367 | this->triangles = NULL; |
---|
| 368 | this->triangleCount = 0; |
---|
[3916] | 369 | } |
---|
[3066] | 370 | |
---|
[3916] | 371 | /** |
---|
[4836] | 372 | * finalizes an Model. |
---|
[4834] | 373 | * This funcion is needed, to delete all the Lists, and arrays that are no more |
---|
| 374 | * needed because they are already imported into openGL. |
---|
| 375 | * This will be applied at the end of the importing Process. |
---|
[3916] | 376 | */ |
---|
[4746] | 377 | bool Model::cleanup() |
---|
[3916] | 378 | { |
---|
| 379 | PRINTF(4)("cleaning up the 3D-Model to save Memory.\n"); |
---|
[4022] | 380 | this->firstGroup->cleanup(); |
---|
[4577] | 381 | return true; |
---|
[3066] | 382 | } |
---|
| 383 | |
---|
[3912] | 384 | ////////// |
---|
| 385 | // MESH // |
---|
| 386 | ////////// |
---|
[3068] | 387 | /** |
---|
[5308] | 388 | * adds a new Material to the Material List |
---|
[4836] | 389 | * @param material the Material to add |
---|
| 390 | * @returns the added material |
---|
[4834] | 391 | * |
---|
| 392 | * this also tells this Model, that all the Materials are handled externally |
---|
| 393 | * with this option set the Materials will not be deleted with the Model. |
---|
[5304] | 394 | */ |
---|
[3913] | 395 | Material* Model::addMaterial(Material* material) |
---|
| 396 | { |
---|
[5308] | 397 | if (material == NULL) |
---|
| 398 | return NULL; |
---|
[5304] | 399 | ModelMaterial* modMat = new ModelMaterial; |
---|
| 400 | modMat->external = true; |
---|
| 401 | modMat->material = material; |
---|
[5774] | 402 | this->materialList.push_back(modMat); |
---|
[5304] | 403 | return modMat->material; |
---|
[3913] | 404 | } |
---|
| 405 | |
---|
| 406 | /** |
---|
[4836] | 407 | * adds a new Material to the Material List |
---|
| 408 | * @param materialName the name of the Material to add |
---|
| 409 | * @returns the added material |
---|
[3913] | 410 | */ |
---|
| 411 | Material* Model::addMaterial(const char* materialName) |
---|
| 412 | { |
---|
[5304] | 413 | ModelMaterial* modMat = new ModelMaterial; |
---|
| 414 | modMat->external = false; |
---|
[5308] | 415 | modMat->material = new Material(materialName); |
---|
[3913] | 416 | |
---|
| 417 | // adding material to the List of materials |
---|
[5774] | 418 | this->materialList.push_back(modMat); |
---|
[5304] | 419 | return modMat->material; |
---|
[3913] | 420 | } |
---|
| 421 | |
---|
[3914] | 422 | /** |
---|
[4836] | 423 | * finds a Material by its name and returns it |
---|
| 424 | * @param materialName the Name of the material to search for. |
---|
| 425 | * @returns the Material if found, NULL otherwise |
---|
[3914] | 426 | */ |
---|
[3913] | 427 | Material* Model::findMaterialByName(const char* materialName) |
---|
| 428 | { |
---|
[5774] | 429 | list<ModelMaterial*>::iterator modMat; |
---|
| 430 | for (modMat = this->materialList.begin(); modMat != this->materialList.end(); modMat++) |
---|
| 431 | if (!strcmp((*modMat)->material->getName(), materialName)) |
---|
| 432 | return (*modMat)->material; |
---|
[3913] | 433 | return NULL; |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | /** |
---|
[4836] | 437 | * parses a group String |
---|
| 438 | * @param groupString the new Group to create |
---|
[3186] | 439 | |
---|
[4577] | 440 | This function initializes a new Group. |
---|
[4022] | 441 | With it you should be able to create Models with more than one SubModel inside |
---|
[3066] | 442 | */ |
---|
[4038] | 443 | bool Model::addGroup(const char* groupString) |
---|
[3066] | 444 | { |
---|
[3548] | 445 | PRINTF(5)("Read Group: %s.\n", groupString); |
---|
[4022] | 446 | if (this->groupCount != 0 && this->currentGroup->faceCount > 0) |
---|
[3066] | 447 | { |
---|
[5218] | 448 | // finalizeGroup(currentGroup); |
---|
[4022] | 449 | this->currentGroup = this->currentGroup->next = new ModelGroup; |
---|
[3066] | 450 | } |
---|
[3140] | 451 | // setting the group name if not default. |
---|
| 452 | if (strcmp(groupString, "default")) |
---|
| 453 | { |
---|
[3195] | 454 | this->currentGroup->name = new char [strlen(groupString)+1]; |
---|
| 455 | strcpy(this->currentGroup->name, groupString); |
---|
[3140] | 456 | } |
---|
[3195] | 457 | ++this->groupCount; |
---|
[3066] | 458 | } |
---|
| 459 | |
---|
| 460 | /** |
---|
[4836] | 461 | * parses a vertex-String |
---|
| 462 | * @param vertexString The String that will be parsed. |
---|
[3186] | 463 | |
---|
[2842] | 464 | If a vertex line is found this function will inject it into the vertex-Array |
---|
| 465 | */ |
---|
[3909] | 466 | bool Model::addVertex (const char* vertexString) |
---|
[2767] | 467 | { |
---|
[3071] | 468 | float subbuffer1; |
---|
| 469 | float subbuffer2; |
---|
| 470 | float subbuffer3; |
---|
| 471 | sscanf (vertexString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3); |
---|
[3195] | 472 | this->vertices->addEntry(subbuffer1*scaleFactor, subbuffer2*scaleFactor, subbuffer3*scaleFactor); |
---|
[4106] | 473 | this->vertexCount++; |
---|
[2767] | 474 | return true; |
---|
| 475 | } |
---|
| 476 | |
---|
[2842] | 477 | /** |
---|
[4836] | 478 | * parses a vertex-String |
---|
| 479 | * @param x the X-coordinate of the Vertex to add. |
---|
| 480 | * @param y the Y-coordinate of the Vertex to add. |
---|
| 481 | * @param z the Z-coordinate of the Vertex to add. |
---|
[4577] | 482 | |
---|
[3400] | 483 | */ |
---|
[3894] | 484 | bool Model::addVertex(float x, float y, float z) |
---|
[3400] | 485 | { |
---|
[3548] | 486 | PRINTF(5)("reading in a vertex: %f %f %f\n", x, y, z); |
---|
[3400] | 487 | this->vertices->addEntry(x*scaleFactor, y*scaleFactor, z*scaleFactor); |
---|
[4106] | 488 | this->vertexCount++; |
---|
[3400] | 489 | return true; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | /** |
---|
[4836] | 493 | * parses a vertexNormal-String |
---|
| 494 | * @param normalString The String that will be parsed. |
---|
[3912] | 495 | |
---|
| 496 | If a vertexNormal line is found this function will inject it into the vertexNormal-Array |
---|
| 497 | */ |
---|
| 498 | bool Model::addVertexNormal (const char* normalString) |
---|
| 499 | { |
---|
| 500 | float subbuffer1; |
---|
| 501 | float subbuffer2; |
---|
| 502 | float subbuffer3; |
---|
| 503 | sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3); |
---|
| 504 | this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3); |
---|
[4106] | 505 | this->normalCount++; |
---|
[3912] | 506 | return true; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | /** |
---|
[4836] | 510 | * adds a VertexNormal. |
---|
| 511 | * @param x The x coordinate of the Normal. |
---|
| 512 | * @param y The y coordinate of the Normal. |
---|
| 513 | * @param z The z coordinate of the Normal. |
---|
[3912] | 514 | |
---|
| 515 | If a vertexNormal line is found this function will inject it into the vertexNormal-Array |
---|
| 516 | */ |
---|
| 517 | bool Model::addVertexNormal(float x, float y, float z) |
---|
| 518 | { |
---|
| 519 | PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z); |
---|
| 520 | this->normals->addEntry(x, y, z); |
---|
[4106] | 521 | this->normalCount++; |
---|
| 522 | return true; |
---|
[3912] | 523 | } |
---|
| 524 | |
---|
| 525 | /** |
---|
[4836] | 526 | * parses a vertexTextureCoordinate-String |
---|
| 527 | * @param vTextureString The String that will be parsed. |
---|
[3912] | 528 | |
---|
| 529 | If a vertexTextureCoordinate line is found, |
---|
| 530 | this function will inject it into the vertexTexture-Array |
---|
[4357] | 531 | |
---|
| 532 | !! WARNING THIS IS DIFFERNT FROM addVervexTexture(float, float); because it changes the second entry to 1-v !! |
---|
[3912] | 533 | */ |
---|
| 534 | bool Model::addVertexTexture (const char* vTextureString) |
---|
| 535 | { |
---|
| 536 | float subbuffer1; |
---|
| 537 | float subbuffer2; |
---|
| 538 | sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2); |
---|
| 539 | this->vTexture->addEntry(subbuffer1); |
---|
[4357] | 540 | this->vTexture->addEntry(1 - subbuffer2); |
---|
[4106] | 541 | this->texCoordCount++; |
---|
[3912] | 542 | return true; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | /** |
---|
[4836] | 546 | * adds a Texture Coordinate |
---|
| 547 | * @param u The u coordinate of the TextureCoordinate. |
---|
| 548 | * @param v The y coordinate of the TextureCoordinate. |
---|
[3912] | 549 | |
---|
| 550 | If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array |
---|
| 551 | */ |
---|
| 552 | bool Model::addVertexTexture(float u, float v) |
---|
| 553 | { |
---|
| 554 | PRINTF(5)("found vertex-Texture %f, %f\n", u, v); |
---|
| 555 | this->vTexture->addEntry(u); |
---|
| 556 | this->vTexture->addEntry(v); |
---|
[4106] | 557 | this->texCoordCount++; |
---|
| 558 | return true; |
---|
[3912] | 559 | } |
---|
| 560 | |
---|
| 561 | /** |
---|
[4836] | 562 | * parses a face-string |
---|
| 563 | * @param faceString The String that will be parsed. |
---|
[3186] | 564 | |
---|
[2842] | 565 | If a face line is found this function will add it to the glList. |
---|
[4112] | 566 | |
---|
| 567 | String is different from the argument addFace, in this that the first Vertex/Normal/Texcoord is 1 instead of 0 |
---|
[2842] | 568 | */ |
---|
[3909] | 569 | bool Model::addFace (const char* faceString) |
---|
[2767] | 570 | { |
---|
[3195] | 571 | if (this->currentGroup->faceCount >0) |
---|
[4022] | 572 | this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; |
---|
[2767] | 573 | |
---|
[4022] | 574 | ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement; |
---|
[3068] | 575 | tmpElem->next = NULL; |
---|
[2934] | 576 | while(strcmp (faceString, "\0")) |
---|
[2767] | 577 | { |
---|
[3195] | 578 | if (this->currentGroup->currentFace->vertexCount>0) |
---|
[4577] | 579 | tmpElem = tmpElem->next = new ModelFaceElement; |
---|
[2934] | 580 | tmpElem->next = NULL; |
---|
| 581 | |
---|
[3064] | 582 | char tmpValue [50]; |
---|
[3072] | 583 | int tmpLen; |
---|
| 584 | char* vertex = NULL; |
---|
| 585 | char* texture = NULL; |
---|
| 586 | char* normal = NULL; |
---|
| 587 | |
---|
[3063] | 588 | sscanf (faceString, "%s", tmpValue); |
---|
[3072] | 589 | tmpLen = strlen(tmpValue); |
---|
| 590 | vertex = tmpValue; |
---|
[2934] | 591 | |
---|
[3072] | 592 | if ((texture = strstr (vertex, "/")) != NULL) |
---|
[4577] | 593 | { |
---|
| 594 | texture[0] = '\0'; |
---|
| 595 | texture ++; |
---|
| 596 | |
---|
| 597 | if ((normal = strstr (texture, "/")) !=NULL) |
---|
| 598 | { |
---|
| 599 | normal[0] = '\0'; |
---|
| 600 | normal ++; |
---|
| 601 | } |
---|
| 602 | } |
---|
[3072] | 603 | if (vertex) |
---|
[4577] | 604 | tmpElem->vertexNumber = atoi(vertex)-1; |
---|
[3072] | 605 | if (texture) |
---|
[4577] | 606 | tmpElem->texCoordNumber = atoi(texture)-1; |
---|
[3072] | 607 | if (normal) |
---|
[4577] | 608 | tmpElem->normalNumber = atoi(normal)-1; |
---|
[3072] | 609 | |
---|
| 610 | faceString += tmpLen; |
---|
[2934] | 611 | if (strcmp (faceString, "\0")) |
---|
[4577] | 612 | faceString++; |
---|
[3195] | 613 | this->currentGroup->currentFace->vertexCount++; |
---|
[2934] | 614 | } |
---|
| 615 | |
---|
[3195] | 616 | this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2; |
---|
[4677] | 617 | this->faceCount += this->currentGroup->currentFace->vertexCount -2; |
---|
[2754] | 618 | } |
---|
[2768] | 619 | |
---|
[2842] | 620 | /** |
---|
[4836] | 621 | * adds a new Face |
---|
| 622 | * @param faceElemCount the number of Vertices to add to the Face. |
---|
| 623 | * @param type The information Passed with each Vertex |
---|
[3400] | 624 | */ |
---|
[3895] | 625 | bool Model::addFace(int faceElemCount, VERTEX_FORMAT type, ...) |
---|
[3400] | 626 | { |
---|
[4022] | 627 | if (this->currentGroup->faceCount > 0) |
---|
| 628 | this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; |
---|
[4577] | 629 | |
---|
[4022] | 630 | ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement; |
---|
[4577] | 631 | |
---|
[3418] | 632 | va_list itemlist; |
---|
| 633 | va_start (itemlist, type); |
---|
| 634 | |
---|
| 635 | for (int i = 0; i < faceElemCount; i++) |
---|
| 636 | { |
---|
[4106] | 637 | if (this->currentGroup->currentFace->vertexCount > 0) |
---|
[4577] | 638 | tmpElem = tmpElem->next = new ModelFaceElement; |
---|
[3418] | 639 | |
---|
[4112] | 640 | tmpElem->vertexNumber = va_arg (itemlist, int); |
---|
[3657] | 641 | if (type & TEXCOORD) |
---|
[4577] | 642 | tmpElem->texCoordNumber = va_arg (itemlist, int); |
---|
[3657] | 643 | if (type & NORMAL) |
---|
[4577] | 644 | tmpElem->normalNumber = va_arg(itemlist, int); |
---|
[3418] | 645 | this->currentGroup->currentFace->vertexCount++; |
---|
| 646 | } |
---|
| 647 | va_end(itemlist); |
---|
| 648 | |
---|
| 649 | this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount - 2; |
---|
[4677] | 650 | this->faceCount += this->currentGroup->currentFace->vertexCount -2; |
---|
[3400] | 651 | } |
---|
| 652 | |
---|
| 653 | /** |
---|
[5308] | 654 | * Function that selects a material, if changed in the obj file. |
---|
[4836] | 655 | * @param matString the Material that will be set. |
---|
[3066] | 656 | */ |
---|
[3913] | 657 | bool Model::setMaterial(const char* matString) |
---|
[3063] | 658 | { |
---|
[3801] | 659 | if (this->currentGroup->faceCount > 0) |
---|
[4022] | 660 | this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; |
---|
[4577] | 661 | |
---|
[3914] | 662 | this->currentGroup->currentFace->material = this->findMaterialByName(matString); |
---|
[3801] | 663 | |
---|
| 664 | if (this->currentGroup->faceCount == 0) |
---|
| 665 | this->currentGroup->faceCount ++; |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | /** |
---|
[5308] | 669 | * Function that selects a material, if changed in the obj file. |
---|
[4836] | 670 | * @param mtl the Material that will be set. |
---|
[3801] | 671 | */ |
---|
[3913] | 672 | bool Model::setMaterial(Material* mtl) |
---|
[3801] | 673 | { |
---|
| 674 | if (this->currentGroup->faceCount > 0) |
---|
[4022] | 675 | this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace; |
---|
[4577] | 676 | |
---|
[3801] | 677 | this->currentGroup->currentFace->material = mtl; |
---|
| 678 | |
---|
[3195] | 679 | if (this->currentGroup->faceCount == 0) |
---|
| 680 | this->currentGroup->faceCount ++; |
---|
[3063] | 681 | } |
---|
| 682 | |
---|
[3066] | 683 | /** |
---|
[4836] | 684 | * A routine that is able to create normals. |
---|
[3912] | 685 | |
---|
| 686 | The algorithm does the following: |
---|
| 687 | 1. It calculates creates Vectors for each normale, and sets them to zero. |
---|
| 688 | 2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements |
---|
| 689 | 3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal. |
---|
| 690 | 4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array. |
---|
| 691 | */ |
---|
| 692 | bool Model::buildVertexNormals () |
---|
[4577] | 693 | { |
---|
[3912] | 694 | PRINTF(4)("Normals are being calculated.\n"); |
---|
| 695 | |
---|
| 696 | Vector* normArray = new Vector [vertices->getCount()/3]; |
---|
| 697 | for (int i=0; i<vertices->getCount()/3;i++) |
---|
| 698 | normArray[i] = Vector(.0,.0,.0); |
---|
[4577] | 699 | |
---|
[3912] | 700 | int firstTouch; |
---|
| 701 | int secondTouch; |
---|
| 702 | Vector prevV; |
---|
| 703 | Vector nextV; |
---|
| 704 | Vector curV; |
---|
| 705 | |
---|
[4022] | 706 | ModelGroup* tmpGroup = firstGroup; |
---|
[3912] | 707 | while (tmpGroup) |
---|
| 708 | { |
---|
[4022] | 709 | ModelFace* tmpFace = tmpGroup->firstFace; |
---|
[3912] | 710 | while (tmpFace) |
---|
[4577] | 711 | { |
---|
| 712 | if (tmpFace->firstElem) |
---|
| 713 | { |
---|
| 714 | ModelFaceElement* firstElem = tmpFace->firstElem; |
---|
| 715 | ModelFaceElement* prevElem; |
---|
| 716 | ModelFaceElement* curElem = firstElem; |
---|
| 717 | ModelFaceElement* nextElem; |
---|
| 718 | ModelFaceElement* lastElem; |
---|
| 719 | // find last Element of the Chain. !! IMPORTANT:the last Element of the Chain must point to NULL, or it will resolv into an infinity-loop. |
---|
| 720 | while (curElem) |
---|
| 721 | { |
---|
| 722 | prevElem = curElem; |
---|
| 723 | curElem = curElem->next; |
---|
| 724 | } |
---|
| 725 | lastElem = prevElem; |
---|
[3912] | 726 | |
---|
[4577] | 727 | curElem = firstElem; |
---|
| 728 | for (int j=0; j<tmpFace->vertexCount; j++) |
---|
| 729 | { |
---|
| 730 | if (!(nextElem = curElem->next)) |
---|
| 731 | nextElem = firstElem; |
---|
| 732 | curElem->normalNumber = curElem->vertexNumber; |
---|
| 733 | |
---|
| 734 | curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]); |
---|
| 735 | prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV; |
---|
| 736 | nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV; |
---|
| 737 | normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV); |
---|
| 738 | |
---|
| 739 | prevElem = curElem; |
---|
| 740 | curElem = curElem->next; |
---|
| 741 | } |
---|
| 742 | } |
---|
| 743 | tmpFace = tmpFace->next; |
---|
| 744 | } |
---|
[3912] | 745 | tmpGroup = tmpGroup->next; |
---|
| 746 | } |
---|
| 747 | |
---|
[4108] | 748 | for (int i=0; i < vertices->getCount()/3;i++) |
---|
[3912] | 749 | { |
---|
| 750 | normArray[i].normalize(); |
---|
| 751 | PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z); |
---|
[4577] | 752 | |
---|
[4108] | 753 | this->addVertexNormal(normArray[i].x, normArray[i].y, normArray[i].z); |
---|
[3912] | 754 | |
---|
| 755 | } |
---|
[4577] | 756 | delete []normArray; |
---|
[3912] | 757 | } |
---|
| 758 | |
---|
| 759 | //////////// |
---|
| 760 | // openGL // |
---|
| 761 | //////////// |
---|
| 762 | /** |
---|
[4836] | 763 | * reads and includes the Faces/Materials into the openGL state Machine |
---|
[3066] | 764 | */ |
---|
[4746] | 765 | bool Model::importToDisplayList() |
---|
[3063] | 766 | { |
---|
| 767 | // finalize the Arrays |
---|
[3195] | 768 | this->vertices->finalizeArray(); |
---|
| 769 | this->vTexture->finalizeArray(); |
---|
[3426] | 770 | if (normals->getCount() == 0) // vertices-Array must be built for this |
---|
[3195] | 771 | this->buildVertexNormals(); |
---|
| 772 | this->normals->finalizeArray(); |
---|
[3063] | 773 | |
---|
[3195] | 774 | this->currentGroup = this->firstGroup; |
---|
[3063] | 775 | |
---|
[3195] | 776 | while (this->currentGroup != NULL) |
---|
[3063] | 777 | { |
---|
| 778 | |
---|
| 779 | // creating a glList for the Group |
---|
[3195] | 780 | if ((this->currentGroup->listNumber = glGenLists(1)) == 0) |
---|
[4577] | 781 | { |
---|
| 782 | PRINTF(2)("glList could not be created for this Model\n"); |
---|
| 783 | return false; |
---|
| 784 | } |
---|
[3195] | 785 | glNewList (this->currentGroup->listNumber, GL_COMPILE); |
---|
[3063] | 786 | |
---|
| 787 | // Putting Faces to GL |
---|
[4022] | 788 | ModelFace* tmpFace = this->currentGroup->firstFace; |
---|
[3063] | 789 | while (tmpFace != NULL) |
---|
[4577] | 790 | { |
---|
| 791 | if (tmpFace->vertexCount == 0 && tmpFace->material != NULL) |
---|
| 792 | { |
---|
| 793 | if (this->currentGroup->faceMode != -1) |
---|
| 794 | glEnd(); |
---|
| 795 | this->currentGroup->faceMode = 0; |
---|
| 796 | Material* tmpMat; |
---|
| 797 | if (tmpFace->material != NULL) |
---|
| 798 | { |
---|
| 799 | tmpFace->material->select(); |
---|
| 800 | PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getName()); |
---|
| 801 | } |
---|
| 802 | } |
---|
[3065] | 803 | |
---|
[4577] | 804 | else if (tmpFace->vertexCount == 3) |
---|
| 805 | { |
---|
| 806 | if (this->currentGroup->faceMode != 3) |
---|
| 807 | { |
---|
| 808 | if (this->currentGroup->faceMode != -1) |
---|
| 809 | glEnd(); |
---|
| 810 | glBegin(GL_TRIANGLES); |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | this->currentGroup->faceMode = 3; |
---|
| 814 | PRINTF(5)("found triag.\n"); |
---|
| 815 | } |
---|
| 816 | |
---|
| 817 | else if (tmpFace->vertexCount == 4) |
---|
| 818 | { |
---|
| 819 | if (this->currentGroup->faceMode != 4) |
---|
| 820 | { |
---|
| 821 | if (this->currentGroup->faceMode != -1) |
---|
| 822 | glEnd(); |
---|
| 823 | glBegin(GL_QUADS); |
---|
| 824 | } |
---|
| 825 | this->currentGroup->faceMode = 4; |
---|
| 826 | PRINTF(5)("found quad.\n"); |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | else if (tmpFace->vertexCount > 4) |
---|
| 830 | { |
---|
| 831 | if (this->currentGroup->faceMode != -1) |
---|
| 832 | glEnd(); |
---|
| 833 | glBegin(GL_POLYGON); |
---|
| 834 | PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount); |
---|
| 835 | this->currentGroup->faceMode = tmpFace->vertexCount; |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | ModelFaceElement* tmpElem = tmpFace->firstElem; |
---|
| 839 | while (tmpElem != NULL) |
---|
| 840 | { |
---|
| 841 | // PRINTF(2)("%s\n", tmpElem->value); |
---|
| 842 | this->addGLElement(tmpElem); |
---|
| 843 | tmpElem = tmpElem->next; |
---|
| 844 | } |
---|
| 845 | tmpFace = tmpFace->next; |
---|
| 846 | } |
---|
[3063] | 847 | glEnd(); |
---|
| 848 | glEndList(); |
---|
[3195] | 849 | |
---|
| 850 | this->currentGroup = this->currentGroup->next; |
---|
[4577] | 851 | } |
---|
[3063] | 852 | } |
---|
| 853 | |
---|
[3066] | 854 | /** |
---|
[4836] | 855 | * reads and includes the Faces/Materials into the openGL state Machine |
---|
[3916] | 856 | */ |
---|
[4746] | 857 | bool Model::importToVertexArray() |
---|
[3916] | 858 | { |
---|
| 859 | // finalize the Arrays |
---|
| 860 | this->vertices->finalizeArray(); |
---|
| 861 | this->vTexture->finalizeArray(); |
---|
| 862 | if (normals->getCount() == 0) // vertices-Array must be built for this |
---|
| 863 | this->buildVertexNormals(); |
---|
| 864 | this->normals->finalizeArray(); |
---|
| 865 | |
---|
| 866 | this->currentGroup = this->firstGroup; |
---|
[3917] | 867 | glVertexPointer(3, GL_FLOAT, 0, this->vertices->getArray()); |
---|
| 868 | glNormalPointer(3, 0, this->normals->getArray()); |
---|
| 869 | glTexCoordPointer(2, GL_FLOAT, 0, this->vTexture->getArray()); |
---|
[3916] | 870 | } |
---|
| 871 | |
---|
| 872 | |
---|
| 873 | |
---|
| 874 | /** |
---|
[4836] | 875 | * builds an array of triangles, that can later on be used for obb separation and octree separation |
---|
[4791] | 876 | */ |
---|
| 877 | bool Model::buildTriangleList() |
---|
| 878 | { |
---|
[4798] | 879 | if( unlikely(this->triangles != NULL)) |
---|
| 880 | return true; |
---|
[4793] | 881 | /* make sure, that all the arrays are finalized */ |
---|
[4797] | 882 | if( unlikely(!this->vertices->isFinalized())) |
---|
| 883 | this->vertices->finalizeArray(); |
---|
| 884 | if( unlikely(!this->vTexture->isFinalized())) |
---|
| 885 | this->vTexture->finalizeArray(); |
---|
[4793] | 886 | if( normals->getCount() == 0) // vertices-Array must be built for this |
---|
| 887 | this->buildVertexNormals(); |
---|
[4797] | 888 | if( unlikely(!this->normals->isFinalized())) |
---|
| 889 | this->normals->finalizeArray(); |
---|
[4791] | 890 | |
---|
[4796] | 891 | |
---|
| 892 | int index = 0; //!< the counter for the triangle array |
---|
| 893 | ModelFaceElement* tmpElem; //!< the temporary faceelement reference |
---|
| 894 | ModelFace* tmpFace; //!< the temporary face referece |
---|
| 895 | |
---|
| 896 | /* count the number of triangles */ |
---|
| 897 | /* now iterate through all groups and build up the triangle list */ |
---|
[4798] | 898 | this->triangleCount = 0; |
---|
[4796] | 899 | this->currentGroup = this->firstGroup; |
---|
| 900 | while( this->currentGroup != NULL) |
---|
| 901 | { |
---|
| 902 | tmpFace = this->currentGroup->firstFace; |
---|
| 903 | while( tmpFace != NULL) |
---|
| 904 | { |
---|
| 905 | |
---|
| 906 | /* if its a triangle just add it to the list */ |
---|
| 907 | if( tmpFace->vertexCount == 3) |
---|
| 908 | { |
---|
| 909 | ++this->triangleCount; |
---|
| 910 | } /* if the polygon is a quad */ |
---|
| 911 | else if( tmpFace->vertexCount == 4) |
---|
| 912 | { |
---|
| 913 | this->triangleCount += 2; |
---|
| 914 | } |
---|
[4798] | 915 | else if( tmpFace->vertexCount > 4) |
---|
| 916 | { |
---|
[5676] | 917 | PRINTF(1)("This model (%s) got over 4 vertices per face <=> conflicts in the CD engine!\n", this->getName()); |
---|
[4798] | 918 | //exit(0); |
---|
| 919 | } |
---|
| 920 | tmpFace = tmpFace->next; |
---|
[4796] | 921 | } |
---|
| 922 | this->currentGroup = this->currentGroup->next; |
---|
| 923 | } |
---|
| 924 | |
---|
[4844] | 925 | PRINTF(3)("got %i triangles, %i vertices\n", this->triangleCount, this->vertexCount); |
---|
[4797] | 926 | |
---|
[5774] | 927 | |
---|
[5676] | 928 | /* write MODELINFO structure */ |
---|
[4798] | 929 | |
---|
[4796] | 930 | /* allocate memory for the new triangle structures */ |
---|
[4799] | 931 | if( (this->triangles = new sTriangleExt[this->triangleCount]) == NULL) |
---|
[4793] | 932 | { |
---|
[4799] | 933 | PRINTF(1)("Could not allocate memory for triangle list\n"); |
---|
[4793] | 934 | return false; |
---|
| 935 | } |
---|
| 936 | |
---|
| 937 | /* now iterate through all groups and build up the triangle list */ |
---|
| 938 | this->currentGroup = this->firstGroup; |
---|
| 939 | while( this->currentGroup != NULL) |
---|
| 940 | { |
---|
| 941 | // Putting Faces to GL |
---|
[4795] | 942 | tmpFace = this->currentGroup->firstFace; |
---|
| 943 | while( tmpFace != NULL) |
---|
[4793] | 944 | { |
---|
| 945 | tmpElem = tmpFace->firstElem; |
---|
| 946 | |
---|
| 947 | /* if its a triangle just add it to the list */ |
---|
[4795] | 948 | if( tmpFace->vertexCount == 3) |
---|
[4793] | 949 | { |
---|
[4795] | 950 | for( int j = 0; j < 3; ++j) |
---|
[4793] | 951 | { |
---|
[4800] | 952 | this->triangles[index].indexToVertices[j] = (unsigned int)tmpElem->vertexNumber * 3; |
---|
[4802] | 953 | this->triangles[index].indexToNormals[j] = (unsigned int)tmpElem->normalNumber * 3; |
---|
[4800] | 954 | this->triangles[index].indexToTexCoor[j] = (unsigned int)tmpElem->texCoordNumber * 3; |
---|
[4793] | 955 | tmpElem = tmpElem->next; |
---|
| 956 | } |
---|
[4796] | 957 | ++index; |
---|
[4795] | 958 | } /* if the polygon is a quad */ |
---|
| 959 | else if( tmpFace->vertexCount == 4) |
---|
| 960 | { |
---|
[4793] | 961 | |
---|
[4800] | 962 | this->triangles[index].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber * 3; |
---|
[4802] | 963 | this->triangles[index].indexToNormals[0] = (unsigned int)tmpElem->normalNumber * 3; |
---|
[4800] | 964 | this->triangles[index].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber * 3; |
---|
[4799] | 965 | |
---|
[4800] | 966 | this->triangles[index + 1].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber * 3; |
---|
[4802] | 967 | this->triangles[index + 1].indexToNormals[0] = (unsigned int)tmpElem->normalNumber * 3; |
---|
[4800] | 968 | this->triangles[index + 1].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber * 3; |
---|
[4793] | 969 | tmpElem = tmpElem->next; |
---|
[4795] | 970 | |
---|
[4800] | 971 | this->triangles[index].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber * 3; |
---|
[4802] | 972 | this->triangles[index].indexToNormals[1] = (unsigned int)tmpElem->normalNumber * 3; |
---|
[4800] | 973 | this->triangles[index].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber * 3; |
---|
[4795] | 974 | tmpElem = tmpElem->next; |
---|
| 975 | |
---|
[4800] | 976 | this->triangles[index].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber * 3; |
---|
[4802] | 977 | this->triangles[index].indexToNormals[2] = (unsigned int)tmpElem->normalNumber * 3; |
---|
[4800] | 978 | this->triangles[index].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber * 3; |
---|
[4795] | 979 | |
---|
[4800] | 980 | this->triangles[index + 1].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber * 3; |
---|
[4802] | 981 | this->triangles[index + 1].indexToNormals[2] = (unsigned int)tmpElem->normalNumber * 3; |
---|
[4800] | 982 | this->triangles[index + 1].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber * 3; |
---|
[4801] | 983 | tmpElem = tmpElem->next; |
---|
[4795] | 984 | |
---|
[4801] | 985 | this->triangles[index + 1].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber * 3; |
---|
[4802] | 986 | this->triangles[index + 1].indexToNormals[1] = (unsigned int)tmpElem->normalNumber * 3; |
---|
[4801] | 987 | this->triangles[index + 1].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber * 3; |
---|
| 988 | |
---|
[4799] | 989 | index += 2; |
---|
[4793] | 990 | } |
---|
[4799] | 991 | tmpFace = tmpFace->next; |
---|
[4793] | 992 | } |
---|
| 993 | this->currentGroup = this->currentGroup->next; |
---|
| 994 | } |
---|
[4799] | 995 | return true; |
---|
[4791] | 996 | } |
---|
| 997 | |
---|
| 998 | |
---|
| 999 | /** |
---|
[4836] | 1000 | * Adds a Face-element (one vertex of a face) with all its information. |
---|
| 1001 | * @param elem The FaceElement to add to the OpenGL-environment. |
---|
[3186] | 1002 | |
---|
[3066] | 1003 | It does this by searching: |
---|
| 1004 | 1. The Vertex itself |
---|
| 1005 | 2. The VertexNormale |
---|
| 1006 | 3. The VertexTextureCoordinate |
---|
| 1007 | merging this information, the face will be drawn. |
---|
[2842] | 1008 | */ |
---|
[4022] | 1009 | bool Model::addGLElement (ModelFaceElement* elem) |
---|
[2776] | 1010 | { |
---|
[3548] | 1011 | PRINTF(5)("importing grafical Element to openGL.\n"); |
---|
[3066] | 1012 | |
---|
[3073] | 1013 | if (elem->texCoordNumber != -1) |
---|
[4108] | 1014 | { |
---|
[4109] | 1015 | if (likely(elem->texCoordNumber < this->texCoordCount)) |
---|
[4577] | 1016 | glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2); |
---|
[4108] | 1017 | else |
---|
[4577] | 1018 | PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n", |
---|
| 1019 | elem->texCoordNumber, this->texCoordCount); |
---|
[4108] | 1020 | } |
---|
[3073] | 1021 | if (elem->normalNumber != -1) |
---|
[4108] | 1022 | { |
---|
[4109] | 1023 | if (likely(elem->normalNumber < this->normalCount)) |
---|
[4108] | 1024 | glNormal3fv(this->normals->getArray() + elem->normalNumber * 3); |
---|
| 1025 | else |
---|
[4577] | 1026 | PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete", |
---|
| 1027 | elem->normalNumber, this->normalCount); |
---|
[4108] | 1028 | } |
---|
[3073] | 1029 | if (elem->vertexNumber != -1) |
---|
[4108] | 1030 | { |
---|
[4110] | 1031 | if (likely(elem->vertexNumber < this->vertexCount)) |
---|
[4577] | 1032 | glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3); |
---|
[4108] | 1033 | else |
---|
[4577] | 1034 | PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete", |
---|
| 1035 | elem->vertexNumber, this->vertexCount); |
---|
| 1036 | } |
---|
[4108] | 1037 | |
---|
[2776] | 1038 | } |
---|
| 1039 | |
---|
[3079] | 1040 | /** |
---|
[4836] | 1041 | * Includes a default model |
---|
[3186] | 1042 | |
---|
[3360] | 1043 | This will inject a Cube, because this is the most basic model. |
---|
[2842] | 1044 | */ |
---|
[4746] | 1045 | void Model::cubeModel() |
---|
[2821] | 1046 | { |
---|
[3656] | 1047 | this->addVertex (-0.5, -0.5, 0.5); |
---|
| 1048 | this->addVertex (0.5, -0.5, 0.5); |
---|
| 1049 | this->addVertex (-0.5, 0.5, 0.5); |
---|
| 1050 | this->addVertex (0.5, 0.5, 0.5); |
---|
| 1051 | this->addVertex (-0.5, 0.5, -0.5); |
---|
| 1052 | this->addVertex (0.5, 0.5, -0.5); |
---|
| 1053 | this->addVertex (-0.5, -0.5, -0.5); |
---|
| 1054 | this->addVertex (0.5, -0.5, -0.5); |
---|
[2967] | 1055 | |
---|
[3656] | 1056 | this->addVertexTexture (0.0, 0.0); |
---|
| 1057 | this->addVertexTexture (1.0, 0.0); |
---|
| 1058 | this->addVertexTexture (0.0, 1.0); |
---|
| 1059 | this->addVertexTexture (1.0, 1.0); |
---|
| 1060 | this->addVertexTexture (0.0, 2.0); |
---|
| 1061 | this->addVertexTexture (1.0, 2.0); |
---|
| 1062 | this->addVertexTexture (0.0, 3.0); |
---|
| 1063 | this->addVertexTexture (1.0, 3.0); |
---|
| 1064 | this->addVertexTexture (0.0, 4.0); |
---|
| 1065 | this->addVertexTexture (1.0, 4.0); |
---|
| 1066 | this->addVertexTexture (2.0, 0.0); |
---|
| 1067 | this->addVertexTexture (2.0, 1.0); |
---|
| 1068 | this->addVertexTexture (-1.0, 0.0); |
---|
| 1069 | this->addVertexTexture (-1.0, 1.0); |
---|
[3081] | 1070 | |
---|
[3656] | 1071 | this->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 1072 | this->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 1073 | this->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 1074 | this->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 1075 | this->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 1076 | this->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 1077 | this->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 1078 | this->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 1079 | this->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 1080 | this->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 1081 | this->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 1082 | this->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 1083 | this->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 1084 | this->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 1085 | this->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 1086 | this->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 1087 | this->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 1088 | this->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 1089 | this->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 1090 | this->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 1091 | this->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 1092 | this->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 1093 | this->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 1094 | this->addVertexNormal (-1.0, 0.0, 0.0); |
---|
[2821] | 1095 | |
---|
[4112] | 1096 | this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3); |
---|
| 1097 | this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7); |
---|
| 1098 | this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11); |
---|
| 1099 | this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15); |
---|
| 1100 | this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19); |
---|
| 1101 | this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23); |
---|
[2821] | 1102 | } |
---|