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