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