[2823] | 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 | |
---|
[2748] | 16 | #include "object.h" |
---|
[2776] | 17 | |
---|
[2842] | 18 | /** |
---|
| 19 | \brief Creates a 3D-Object, but does not load any 3D-models |
---|
| 20 | pretty useless |
---|
| 21 | */ |
---|
[2748] | 22 | Object::Object () |
---|
| 23 | { |
---|
[2765] | 24 | |
---|
[2767] | 25 | initialize(); |
---|
[2847] | 26 | |
---|
[2846] | 27 | BoxObject(); |
---|
[2765] | 28 | |
---|
[2767] | 29 | finalize(); |
---|
| 30 | } |
---|
| 31 | |
---|
[2842] | 32 | /** |
---|
| 33 | \brief Crates a 3D-Object and loads in a File |
---|
| 34 | \param fileName file to parse and load (must be a .obj file) |
---|
| 35 | */ |
---|
[2773] | 36 | Object::Object(char* fileName) |
---|
| 37 | { |
---|
| 38 | initialize(); |
---|
| 39 | |
---|
| 40 | importFile (fileName); |
---|
| 41 | |
---|
| 42 | finalize(); |
---|
| 43 | } |
---|
| 44 | |
---|
[2842] | 45 | /** |
---|
| 46 | \brief Crates a 3D-Object, loads in a File and scales it. |
---|
| 47 | \param fileName file to parse and load (must be a .obj file) |
---|
| 48 | \param scaling The factor that the object will be scaled with. |
---|
| 49 | */ |
---|
[2833] | 50 | Object::Object(char* fileName, float scaling) |
---|
[2767] | 51 | { |
---|
[2833] | 52 | initialize(); |
---|
| 53 | scaleFactor = scaling; |
---|
| 54 | |
---|
| 55 | importFile (fileName); |
---|
| 56 | |
---|
| 57 | finalize(); |
---|
[2767] | 58 | } |
---|
| 59 | |
---|
[2847] | 60 | /** |
---|
| 61 | \brief deletes an Object |
---|
| 62 | */ |
---|
| 63 | Object::~Object() |
---|
| 64 | { |
---|
| 65 | if (verbose >= 2) |
---|
[2850] | 66 | printf ("Deleting display List.\n"); |
---|
| 67 | Group* walker = firstGroup; |
---|
| 68 | while (walker != NULL) |
---|
| 69 | { |
---|
| 70 | glDeleteLists (walker->listNumber, 1); |
---|
| 71 | Group* lastWalker = walker; |
---|
| 72 | walker = walker->nextGroup; |
---|
| 73 | delete lastWalker; |
---|
| 74 | } |
---|
[2847] | 75 | } |
---|
| 76 | |
---|
[2842] | 77 | /** |
---|
| 78 | \brief initializes the Object |
---|
| 79 | This Function initializes all the needed arrays, Lists and clientStates |
---|
| 80 | */ |
---|
[2767] | 81 | bool Object::initialize (void) |
---|
| 82 | { |
---|
[2804] | 83 | if (verbose >=3) |
---|
| 84 | printf("new 3D-Object is being created\n"); |
---|
[2833] | 85 | |
---|
[2850] | 86 | // setting the start group; |
---|
| 87 | firstGroup = new Group; |
---|
| 88 | currentGroup = firstGroup; |
---|
| 89 | groupCount = 0; |
---|
| 90 | |
---|
[2967] | 91 | initGroup (firstGroup); |
---|
[2833] | 92 | mtlFileName = ""; |
---|
| 93 | scaleFactor = 1; |
---|
[2847] | 94 | material = new Material(); |
---|
| 95 | |
---|
[2967] | 96 | // glEnableClientState (GL_VERTEX_ARRAY); |
---|
[2850] | 97 | // glEnableClientState (GL_NORMAL_ARRAY); |
---|
[2794] | 98 | // glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
---|
[2759] | 99 | |
---|
[2850] | 100 | |
---|
[2767] | 101 | return true; |
---|
| 102 | } |
---|
| 103 | |
---|
[2842] | 104 | /** |
---|
| 105 | \brief Imports a obj file and handles the the relative location |
---|
| 106 | \param fileName The file to import |
---|
| 107 | */ |
---|
[2833] | 108 | bool Object::importFile (char* fileName) |
---|
| 109 | { |
---|
| 110 | if (verbose >=3) |
---|
| 111 | printf("preparing to read in file: %s\n", fileName); |
---|
| 112 | objFileName = fileName; |
---|
| 113 | this->readFromObjFile (objFileName); |
---|
| 114 | return true; |
---|
| 115 | } |
---|
| 116 | |
---|
[2842] | 117 | /** |
---|
[2863] | 118 | \brief finalizes an Object. |
---|
[2842] | 119 | This funcion is needed, to close the glList and all the other lists. |
---|
| 120 | */ |
---|
[2767] | 121 | bool Object::finalize(void) |
---|
| 122 | { |
---|
[2850] | 123 | // if (verbose >=3) |
---|
[2804] | 124 | printf("finalizing the 3D-Object\n"); |
---|
[2850] | 125 | finalizeGroup (currentGroup); |
---|
[2847] | 126 | if (material != NULL) |
---|
| 127 | delete material; |
---|
[2767] | 128 | return true; |
---|
[2748] | 129 | } |
---|
| 130 | |
---|
[2842] | 131 | /** |
---|
[2851] | 132 | \brief Draws the Objects of all Groups. |
---|
| 133 | It does this by just calling the Lists that must have been created earlier. |
---|
[2842] | 134 | */ |
---|
[2767] | 135 | void Object::draw (void) |
---|
[2748] | 136 | { |
---|
[2851] | 137 | if (verbose >=2) |
---|
| 138 | printf("drawing the 3D-Objects\n"); |
---|
[2850] | 139 | Group* walker = firstGroup; |
---|
| 140 | while (walker != NULL) |
---|
| 141 | { |
---|
[2851] | 142 | if (verbose >= 3) |
---|
| 143 | printf ("Drawing object %s\n", walker->name); |
---|
[2850] | 144 | glCallList (walker->listNumber); |
---|
| 145 | walker = walker->nextGroup; |
---|
| 146 | } |
---|
[2748] | 147 | } |
---|
[2754] | 148 | |
---|
[2842] | 149 | /** |
---|
[2851] | 150 | \brief Draws the Object number groupNumber |
---|
| 151 | It does this by just calling the List that must have been created earlier. |
---|
[2852] | 152 | \param groupNumber The number of the group that will be displayed. |
---|
[2851] | 153 | */ |
---|
| 154 | void Object::draw (int groupNumber) |
---|
| 155 | { |
---|
[2852] | 156 | if (groupNumber >= groupCount) |
---|
| 157 | { |
---|
| 158 | if (verbose>=2) |
---|
| 159 | printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount); |
---|
| 160 | return; |
---|
| 161 | } |
---|
[2851] | 162 | if (verbose >=2) |
---|
| 163 | printf("drawing the requested 3D-Objects if found.\n"); |
---|
| 164 | Group* walker = firstGroup; |
---|
| 165 | int counter = 0; |
---|
| 166 | while (walker != NULL) |
---|
| 167 | { |
---|
| 168 | if (counter == groupNumber) |
---|
| 169 | { |
---|
| 170 | if (verbose >= 2) |
---|
| 171 | printf ("Drawing object number %s named %s\n", counter, walker->name); |
---|
| 172 | glCallList (walker->listNumber); |
---|
| 173 | return; |
---|
| 174 | } |
---|
| 175 | ++counter; |
---|
| 176 | walker = walker->nextGroup; |
---|
| 177 | } |
---|
| 178 | if (verbose >= 2) |
---|
| 179 | printf("Object number %i in %s not Found.\n", groupNumber, objFileName); |
---|
| 180 | return; |
---|
| 181 | |
---|
| 182 | } |
---|
[2852] | 183 | |
---|
| 184 | /** |
---|
| 185 | \brief Draws the Object with a specific groupname |
---|
| 186 | It does this by just calling the List that must have been created earlier. |
---|
| 187 | \param groupName The name of the group that will be displayed. |
---|
| 188 | */ |
---|
[2851] | 189 | void Object::draw (char* groupName) |
---|
| 190 | { |
---|
| 191 | if (verbose >=2) |
---|
| 192 | printf("drawing the requested 3D-Objects if found.\n"); |
---|
| 193 | Group* walker = firstGroup; |
---|
| 194 | while (walker != NULL) |
---|
| 195 | { |
---|
| 196 | if (!strcmp(walker->name, groupName)) |
---|
| 197 | { |
---|
| 198 | if (verbose >= 2) |
---|
| 199 | printf ("Drawing object %s\n", walker->name); |
---|
| 200 | glCallList (walker->listNumber); |
---|
| 201 | return; |
---|
| 202 | } |
---|
| 203 | walker = walker->nextGroup; |
---|
| 204 | } |
---|
| 205 | if (verbose >= 2) |
---|
| 206 | printf("Object Named %s in %s not Found.\n", groupName, objFileName); |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | |
---|
[2852] | 210 | /** |
---|
| 211 | \returns Count of the Objects in this File |
---|
| 212 | */ |
---|
| 213 | int Object::getGroupCount (void) |
---|
| 214 | { |
---|
| 215 | return groupCount; |
---|
| 216 | } |
---|
[2851] | 217 | |
---|
| 218 | /** |
---|
[2850] | 219 | \brief initializes a new Group object |
---|
| 220 | */ |
---|
| 221 | bool Object::initGroup(Group* group) |
---|
| 222 | { |
---|
| 223 | if (verbose >= 2) |
---|
| 224 | printf("Adding new Group\n"); |
---|
[2863] | 225 | group->name = ""; |
---|
[2850] | 226 | group->faceMode = -1; |
---|
[2967] | 227 | group->faceCount = 0; |
---|
[2850] | 228 | if ((group->listNumber = glGenLists(1)) == 0 ) |
---|
| 229 | { |
---|
| 230 | printf ("list could not be created for this Object\n"); |
---|
| 231 | return false; |
---|
| 232 | } |
---|
[2851] | 233 | |
---|
[2850] | 234 | if (groupCount == 0) |
---|
| 235 | { |
---|
| 236 | group->firstVertex = 0; |
---|
| 237 | group->firstNormal = 0; |
---|
[2967] | 238 | group->firstVertexTexture = 0; |
---|
[2850] | 239 | } |
---|
| 240 | else |
---|
| 241 | { |
---|
| 242 | group->firstVertex = currentGroup->firstVertex + currentGroup->vertices->getCount()/3; |
---|
| 243 | group->firstNormal = currentGroup->firstNormal + currentGroup->normals->getCount()/3; |
---|
| 244 | group->firstVertexTexture = currentGroup->firstVertexTexture + currentGroup->vTexture->getCount()/2; |
---|
| 245 | } |
---|
[2863] | 246 | if (verbose >=2) |
---|
| 247 | printf ("Creating new Arrays, with starting points v:%i, vt:%i, vn:%i .\n", group->firstVertex, group->firstVertexTexture, group->firstNormal); |
---|
[2850] | 248 | group->vertices = new Array(); |
---|
| 249 | group->normals = new Array(); |
---|
| 250 | group->vTexture = new Array(); |
---|
| 251 | |
---|
| 252 | glNewList (group->listNumber, GL_COMPILE); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | \brief finalizes a Group. |
---|
[2863] | 257 | \param group the group to finalize. |
---|
[2850] | 258 | */ |
---|
| 259 | bool Object::finalizeGroup(Group* group) |
---|
| 260 | { |
---|
[2863] | 261 | if (verbose >=2) |
---|
| 262 | printf ("Finalize group %s.\n", group->name); |
---|
[2850] | 263 | glEnd(); |
---|
| 264 | glEndList(); |
---|
[2863] | 265 | } |
---|
[2967] | 266 | |
---|
[2863] | 267 | /** |
---|
| 268 | \brief deletes the Arrays of the Group to save space. |
---|
| 269 | \param group the group to delete the arrays from. |
---|
| 270 | */ |
---|
| 271 | bool Object::cleanupGroup(Group* group) |
---|
| 272 | { |
---|
| 273 | if (verbose >=2) |
---|
| 274 | printf ("cleaning up group %s.\n", group->name); |
---|
[2850] | 275 | |
---|
| 276 | delete group->vertices; |
---|
| 277 | delete group->normals; |
---|
| 278 | delete group->vTexture; |
---|
| 279 | } |
---|
[2863] | 280 | |
---|
[2850] | 281 | /** |
---|
[2842] | 282 | \brief Reads in the .obj File and sets all the Values. |
---|
| 283 | This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks |
---|
| 284 | \param fileName the File that will be parsed (.obj-file) |
---|
| 285 | */ |
---|
[2754] | 286 | bool Object::readFromObjFile (char* fileName) |
---|
| 287 | { |
---|
[2823] | 288 | OBJ_FILE = new ifstream(fileName); |
---|
[2775] | 289 | if (!OBJ_FILE->is_open()) |
---|
| 290 | { |
---|
[2804] | 291 | if (verbose >=1) |
---|
[2821] | 292 | printf ("unable to open .OBJ file: %s\n Loading Box Object instead.\n", fileName); |
---|
| 293 | BoxObject(); |
---|
[2775] | 294 | return false; |
---|
| 295 | } |
---|
[2804] | 296 | objFileName = fileName; |
---|
[2931] | 297 | char Buffer[10000]; |
---|
[2765] | 298 | while(!OBJ_FILE->eof()) |
---|
| 299 | { |
---|
[2931] | 300 | OBJ_FILE->getline(Buffer, 10000); |
---|
[2804] | 301 | if (verbose >=4) |
---|
| 302 | printf ("Read input line: %s\n",Buffer); |
---|
[2765] | 303 | |
---|
| 304 | |
---|
| 305 | // case vertice |
---|
| 306 | if (!strncmp(Buffer, "v ", 2)) |
---|
| 307 | { |
---|
[2767] | 308 | readVertex(Buffer+2); |
---|
[2765] | 309 | } |
---|
| 310 | |
---|
| 311 | // case face |
---|
| 312 | else if (!strncmp(Buffer, "f ", 2)) |
---|
| 313 | { |
---|
[2767] | 314 | readFace (Buffer+2); |
---|
[2765] | 315 | } |
---|
[2776] | 316 | |
---|
| 317 | else if (!strncmp(Buffer, "mtllib", 6)) |
---|
| 318 | { |
---|
| 319 | readMtlLib (Buffer+7); |
---|
| 320 | } |
---|
[2765] | 321 | |
---|
[2769] | 322 | else if (!strncmp(Buffer, "usemtl", 6)) |
---|
| 323 | { |
---|
| 324 | readUseMtl (Buffer+7); |
---|
| 325 | } |
---|
[2794] | 326 | |
---|
| 327 | // case VertexNormal |
---|
| 328 | else if (!strncmp(Buffer, "vn ", 2)) |
---|
| 329 | { |
---|
| 330 | readVertexNormal(Buffer+3); |
---|
| 331 | } |
---|
| 332 | |
---|
[2848] | 333 | // case VertexTextureCoordinate |
---|
[2765] | 334 | else if (!strncmp(Buffer, "vt ", 2)) |
---|
| 335 | { |
---|
[2820] | 336 | readVertexTexture(Buffer+3); |
---|
[2765] | 337 | } |
---|
[2848] | 338 | // case group |
---|
| 339 | else if (!strncmp(Buffer, "g", 1)) |
---|
| 340 | { |
---|
[2850] | 341 | readGroup (Buffer+2); |
---|
[2848] | 342 | } |
---|
[2765] | 343 | } |
---|
[2846] | 344 | OBJ_FILE->close(); |
---|
[2863] | 345 | return true; |
---|
[2765] | 346 | |
---|
[2767] | 347 | } |
---|
[2765] | 348 | |
---|
[2842] | 349 | /** |
---|
| 350 | \brief parses a vertex-String |
---|
| 351 | If a vertex line is found this function will inject it into the vertex-Array |
---|
| 352 | \param vertexString The String that will be parsed. |
---|
| 353 | */ |
---|
[2767] | 354 | bool Object::readVertex (char* vertexString) |
---|
| 355 | { |
---|
[2850] | 356 | readingVertices = true; |
---|
[2768] | 357 | char subbuffer1[20]; |
---|
| 358 | char subbuffer2[20]; |
---|
| 359 | char subbuffer3[20]; |
---|
[2767] | 360 | sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
[2804] | 361 | if (verbose >= 3) |
---|
| 362 | printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3); |
---|
[2850] | 363 | currentGroup->vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor); |
---|
[2767] | 364 | return true; |
---|
| 365 | } |
---|
| 366 | |
---|
[2842] | 367 | /** |
---|
| 368 | \brief parses a face-string |
---|
| 369 | If a face line is found this function will add it to the glList. |
---|
| 370 | The function makes a difference between QUADS and TRIANGLES, and will if changed re-open, set and re-close the gl-processe. |
---|
| 371 | \param faceString The String that will be parsed. |
---|
| 372 | */ |
---|
[2767] | 373 | bool Object::readFace (char* faceString) |
---|
| 374 | { |
---|
[2850] | 375 | // finalize the Arrays; |
---|
| 376 | if (readingVertices == true) |
---|
[2757] | 377 | { |
---|
[2850] | 378 | currentGroup->vertices->finalizeArray(); |
---|
[2934] | 379 | // glVertexPointer(3, GL_FLOAT, 0, currentGroup->vertices->getArray()); |
---|
[2850] | 380 | currentGroup->normals->finalizeArray(); |
---|
[2934] | 381 | // glNormalPointer(GL_FLOAT, 0, currentGroup->normals->getArray()); |
---|
[2850] | 382 | currentGroup->vTexture->finalizeArray(); |
---|
[2757] | 383 | } |
---|
[2767] | 384 | |
---|
[2850] | 385 | readingVertices = false; |
---|
[2863] | 386 | currentGroup->faceCount++; |
---|
[2934] | 387 | |
---|
| 388 | int elemCount = 0; |
---|
| 389 | |
---|
| 390 | FaceElement* firstElem = new FaceElement; |
---|
| 391 | FaceElement* tmpElem = firstElem; |
---|
| 392 | |
---|
| 393 | |
---|
| 394 | while(strcmp (faceString, "\0")) |
---|
[2767] | 395 | { |
---|
[2934] | 396 | if (elemCount>0) |
---|
| 397 | tmpElem = tmpElem->next = new FaceElement; |
---|
| 398 | tmpElem->next = NULL; |
---|
| 399 | |
---|
| 400 | |
---|
| 401 | sscanf (faceString, "%s", tmpElem->value); |
---|
| 402 | faceString += strlen(tmpElem->value); |
---|
| 403 | if (strcmp (faceString, "\0")) |
---|
| 404 | faceString++; |
---|
| 405 | elemCount++; |
---|
| 406 | |
---|
| 407 | |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | |
---|
| 411 | if (elemCount == 3) |
---|
| 412 | { |
---|
[2850] | 413 | if (currentGroup->faceMode != 3) |
---|
[2767] | 414 | { |
---|
[2850] | 415 | if (currentGroup->faceMode != -1) |
---|
[2767] | 416 | glEnd(); |
---|
| 417 | glBegin(GL_TRIANGLES); |
---|
| 418 | } |
---|
| 419 | |
---|
[2850] | 420 | currentGroup->faceMode = 3; |
---|
[2804] | 421 | if (verbose >=3) |
---|
[2934] | 422 | printf ("found triag.\n"); |
---|
[2767] | 423 | } |
---|
[2934] | 424 | |
---|
| 425 | else if (elemCount == 4) |
---|
[2757] | 426 | { |
---|
[2850] | 427 | if (currentGroup->faceMode != 4) |
---|
[2757] | 428 | { |
---|
[2850] | 429 | if (currentGroup->faceMode != -1) |
---|
[2767] | 430 | glEnd(); |
---|
| 431 | glBegin(GL_QUADS); |
---|
[2757] | 432 | } |
---|
[2850] | 433 | currentGroup->faceMode = 4; |
---|
[2804] | 434 | if (verbose >=3 ) |
---|
[2934] | 435 | printf ("found quad.\n"); |
---|
[2757] | 436 | } |
---|
[2934] | 437 | |
---|
| 438 | else if (elemCount > 4) |
---|
| 439 | { |
---|
| 440 | if (currentGroup->faceMode != -1) |
---|
| 441 | glEnd(); |
---|
| 442 | glBegin(GL_POLYGON); |
---|
| 443 | if (verbose >=3) |
---|
| 444 | printf ("Polygon with %i faces found.", elemCount); |
---|
| 445 | currentGroup->faceMode = elemCount; |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | tmpElem = firstElem; |
---|
[2937] | 449 | FaceElement* delElem; |
---|
[2934] | 450 | while (tmpElem != NULL) |
---|
| 451 | { |
---|
| 452 | // printf ("%s\n", tmpElem->value); |
---|
| 453 | addGLElement(tmpElem->value); |
---|
[2937] | 454 | delElem = tmpElem; |
---|
[2934] | 455 | tmpElem = tmpElem->next; |
---|
[2937] | 456 | delete delElem; |
---|
[2934] | 457 | } |
---|
| 458 | |
---|
[2754] | 459 | } |
---|
[2768] | 460 | |
---|
[2842] | 461 | /** |
---|
| 462 | \brief Adds a Face-element (one vertex of a face) with all its information. |
---|
| 463 | It does this by searching: |
---|
| 464 | 1. The Vertex itself |
---|
| 465 | 2. The VertexNormale |
---|
| 466 | 3. The VertexTextureCoordinate |
---|
| 467 | merging this information, the face will be drawn. |
---|
| 468 | |
---|
| 469 | */ |
---|
[2768] | 470 | bool Object::addGLElement (char* elementString) |
---|
| 471 | { |
---|
[2833] | 472 | if (verbose >=3) |
---|
[2863] | 473 | printf ("importing grafical Element to openGL\n"); |
---|
[2794] | 474 | char* vertex = elementString; |
---|
[2833] | 475 | |
---|
[2794] | 476 | char* texture; |
---|
[2932] | 477 | if ((texture = strstr (vertex, "/")) != NULL) |
---|
| 478 | { |
---|
| 479 | texture[0] = '\0'; |
---|
| 480 | texture ++; |
---|
| 481 | if (verbose>=3) |
---|
[2967] | 482 | printf ("includeing texture #%i, and mapping it to group texture #%i, textureArray has %i entries.\n", atoi(texture), (atoi(texture)-1 - currentGroup->firstVertexTexture)*2, currentGroup->vTexture->getCount()); |
---|
[2932] | 483 | glTexCoord2fv(currentGroup->vTexture->getArray()+(atoi(texture)-1 - currentGroup->firstVertexTexture)*2); |
---|
[2833] | 484 | |
---|
[2932] | 485 | char* normal; |
---|
| 486 | if ((normal = strstr (texture, "/")) !=NULL) |
---|
| 487 | { |
---|
| 488 | normal[0] = '\0'; |
---|
| 489 | normal ++; |
---|
| 490 | //glArrayElement(atoi(vertex)-1); |
---|
| 491 | glNormal3fv(currentGroup->normals->getArray() +(atoi(normal)-1 - currentGroup->firstNormal)*3); |
---|
| 492 | } |
---|
[2833] | 493 | } |
---|
[2863] | 494 | if (verbose>=3) |
---|
| 495 | printf ("includeing vertex #%i, and mapping it to group vertex #%i, vertexArray has %i entries.\n", atoi(vertex), (atoi(vertex)-1 - currentGroup->firstVertex)*3, currentGroup->vertices->getCount()); |
---|
[2850] | 496 | glVertex3fv(currentGroup->vertices->getArray() +(atoi(vertex)-1 - currentGroup->firstVertex)*3); |
---|
[2768] | 497 | |
---|
| 498 | } |
---|
[2769] | 499 | |
---|
[2842] | 500 | /** |
---|
| 501 | \brief parses a vertexNormal-String |
---|
| 502 | If a vertexNormal line is found this function will inject it into the vertexNormal-Array |
---|
| 503 | \param normalString The String that will be parsed. |
---|
| 504 | */ |
---|
[2794] | 505 | bool Object::readVertexNormal (char* normalString) |
---|
| 506 | { |
---|
[2850] | 507 | readingVertices = true; |
---|
[2794] | 508 | char subbuffer1[20]; |
---|
| 509 | char subbuffer2[20]; |
---|
| 510 | char subbuffer3[20]; |
---|
| 511 | sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
[2804] | 512 | if (verbose >=3 ) |
---|
| 513 | printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3); |
---|
[2850] | 514 | currentGroup->normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3)); |
---|
[2794] | 515 | return true; |
---|
| 516 | } |
---|
| 517 | |
---|
[2842] | 518 | /** |
---|
| 519 | \brief parses a vertexTextureCoordinate-String |
---|
| 520 | If a vertexTextureCoordinate line is found this function will inject it into the vertexTexture-Array |
---|
| 521 | \param vTextureString The String that will be parsed. |
---|
| 522 | */ |
---|
[2820] | 523 | bool Object::readVertexTexture (char* vTextureString) |
---|
| 524 | { |
---|
[2850] | 525 | readingVertices = true; |
---|
[2820] | 526 | char subbuffer1[20]; |
---|
| 527 | char subbuffer2[20]; |
---|
| 528 | sscanf (vTextureString, "%s %s", subbuffer1, subbuffer2); |
---|
| 529 | if (verbose >=3 ) |
---|
| 530 | printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2); |
---|
[2850] | 531 | currentGroup->vTexture->addEntry(atof(subbuffer1)); |
---|
| 532 | currentGroup->vTexture->addEntry(atof(subbuffer2)); |
---|
[2820] | 533 | return true; |
---|
| 534 | } |
---|
| 535 | |
---|
[2850] | 536 | /** |
---|
| 537 | \brief parses a group String |
---|
| 538 | This function initializes a new Group. |
---|
| 539 | With it you should be able to import .obj-files with more than one Objects inside. |
---|
| 540 | \param groupString the new Group to create |
---|
| 541 | */ |
---|
| 542 | bool Object::readGroup (char* groupString) |
---|
| 543 | { |
---|
[2863] | 544 | // setting the group name if not default. |
---|
| 545 | if (strcmp(currentGroup->name, "default")) |
---|
[2850] | 546 | { |
---|
[2863] | 547 | currentGroup->name = (char*) malloc ( strlen(groupString) * sizeof (char)); |
---|
| 548 | strcpy(currentGroup->name, groupString); |
---|
[2850] | 549 | } |
---|
[2863] | 550 | if (groupCount != 0 && currentGroup->faceCount>0) |
---|
[2850] | 551 | { |
---|
[2863] | 552 | Group* newGroup = new Group; |
---|
| 553 | finalizeGroup(currentGroup); |
---|
| 554 | currentGroup->nextGroup = newGroup; |
---|
| 555 | initGroup(newGroup); |
---|
| 556 | cleanupGroup(currentGroup); // deletes the arrays of the group; must be after initGroup. |
---|
| 557 | currentGroup = newGroup; // must be after init see initGroup for more info |
---|
[2850] | 558 | } |
---|
[2863] | 559 | |
---|
| 560 | ++groupCount; |
---|
| 561 | |
---|
[2850] | 562 | } |
---|
| 563 | |
---|
[2842] | 564 | /** |
---|
| 565 | \brief Function to read in a mtl File. |
---|
| 566 | this Function parses all Lines of an mtl File |
---|
| 567 | \param mtlFile The .mtl file to read |
---|
| 568 | */ |
---|
[2776] | 569 | bool Object::readMtlLib (char* mtlFile) |
---|
| 570 | { |
---|
| 571 | MTL_FILE = new ifstream (mtlFile); |
---|
| 572 | if (!MTL_FILE->is_open()) |
---|
| 573 | { |
---|
[2804] | 574 | if (verbose >= 1) |
---|
| 575 | printf ("unable to open file: %s\n", mtlFile); |
---|
[2776] | 576 | return false; |
---|
| 577 | } |
---|
[2804] | 578 | mtlFileName = mtlFile; |
---|
| 579 | if (verbose >=2) |
---|
| 580 | printf ("Opening mtlFile: %s\n", mtlFileName); |
---|
[2776] | 581 | char Buffer[500]; |
---|
[2778] | 582 | Material* tmpMat = material; |
---|
[2776] | 583 | while(!MTL_FILE->eof()) |
---|
| 584 | { |
---|
| 585 | MTL_FILE->getline(Buffer, 500); |
---|
[2804] | 586 | if (verbose >= 4) |
---|
| 587 | printf("found line in mtlFile: %s\n", Buffer); |
---|
[2776] | 588 | |
---|
[2769] | 589 | |
---|
[2776] | 590 | // create new Material |
---|
| 591 | if (!strncmp(Buffer, "newmtl ", 2)) |
---|
| 592 | { |
---|
[2778] | 593 | tmpMat = tmpMat->addMaterial(Buffer+7); |
---|
[2779] | 594 | // printf ("%s, %p\n", tmpMat->getName(), tmpMat); |
---|
[2776] | 595 | } |
---|
| 596 | // setting a illumMode |
---|
| 597 | else if (!strncmp(Buffer, "illum", 5)) |
---|
| 598 | { |
---|
[2778] | 599 | tmpMat->setIllum(Buffer+6); |
---|
| 600 | |
---|
[2776] | 601 | } |
---|
| 602 | // setting Diffuse Color |
---|
| 603 | else if (!strncmp(Buffer, "Kd", 2)) |
---|
| 604 | { |
---|
[2778] | 605 | tmpMat->setDiffuse(Buffer+3); |
---|
[2776] | 606 | } |
---|
| 607 | // setting Ambient Color |
---|
| 608 | else if (!strncmp(Buffer, "Ka", 2)) |
---|
| 609 | { |
---|
[2778] | 610 | tmpMat->setAmbient(Buffer+3); |
---|
[2776] | 611 | } |
---|
| 612 | // setting Specular Color |
---|
| 613 | else if (!strncmp(Buffer, "Ks", 2)) |
---|
| 614 | { |
---|
[2778] | 615 | tmpMat->setSpecular(Buffer+3); |
---|
[2776] | 616 | } |
---|
[2836] | 617 | // setting The Specular Shininess |
---|
| 618 | else if (!strncmp(Buffer, "Ns", 2)) |
---|
| 619 | { |
---|
| 620 | tmpMat->setShininess(Buffer+3); |
---|
| 621 | } |
---|
[2837] | 622 | // setting up transparency |
---|
| 623 | else if (!strncmp(Buffer, "d", 1)) |
---|
| 624 | { |
---|
| 625 | tmpMat->setTransparency(Buffer+2); |
---|
| 626 | } |
---|
| 627 | else if (!strncpy(Buffer, "Tf", 2)) |
---|
| 628 | { |
---|
| 629 | tmpMat->setTransparency(Buffer+3); |
---|
| 630 | } |
---|
| 631 | |
---|
[2776] | 632 | } |
---|
| 633 | return true; |
---|
| 634 | } |
---|
| 635 | |
---|
[2842] | 636 | /** |
---|
| 637 | \brief Function that selects a material, if changed in the obj file. |
---|
| 638 | \param matString the Material that will be set. |
---|
| 639 | */ |
---|
[2769] | 640 | bool Object::readUseMtl (char* matString) |
---|
| 641 | { |
---|
[2833] | 642 | if (!strcmp (mtlFileName, "")) |
---|
| 643 | { |
---|
| 644 | if (verbose >= 1) |
---|
| 645 | printf ("Not using new defined material, because no mtlFile found yet\n"); |
---|
| 646 | return false; |
---|
| 647 | } |
---|
| 648 | |
---|
[2850] | 649 | if (currentGroup->faceMode != -1) |
---|
[2769] | 650 | glEnd(); |
---|
[2850] | 651 | currentGroup->faceMode = 0; |
---|
[2804] | 652 | if (verbose >= 2) |
---|
| 653 | printf ("using material %s for coming Faces.\n", matString); |
---|
[2779] | 654 | material->search(matString)->select(); |
---|
[2769] | 655 | } |
---|
[2776] | 656 | |
---|
[2842] | 657 | /** |
---|
| 658 | \brief Includes a default object |
---|
| 659 | This will inject a Cube, because this is the most basic object. |
---|
| 660 | */ |
---|
[2821] | 661 | void Object::BoxObject(void) |
---|
| 662 | { |
---|
| 663 | readVertex ("-0.500000 -0.500000 0.500000"); |
---|
| 664 | readVertex ("0.500000 -0.500000 0.500000"); |
---|
| 665 | readVertex ("-0.500000 0.500000 0.500000"); |
---|
| 666 | readVertex ("0.500000 0.500000 0.500000"); |
---|
| 667 | readVertex ("-0.500000 0.500000 -0.500000"); |
---|
| 668 | readVertex ("0.500000 0.500000 -0.500000"); |
---|
| 669 | readVertex ("-0.500000 -0.500000 -0.500000"); |
---|
| 670 | readVertex ("0.500000 -0.500000 -0.500000"); |
---|
[2967] | 671 | |
---|
[2821] | 672 | readVertexTexture ("0.000000 0.000000"); |
---|
| 673 | readVertexTexture ("1.000000 0.000000"); |
---|
| 674 | readVertexTexture ("0.000000 1.000000"); |
---|
| 675 | readVertexTexture ("1.000000 1.000000"); |
---|
| 676 | readVertexTexture ("0.000000 2.000000"); |
---|
| 677 | readVertexTexture ("1.000000 2.000000"); |
---|
| 678 | readVertexTexture ("0.000000 3.000000"); |
---|
| 679 | readVertexTexture ("1.000000 3.000000"); |
---|
| 680 | readVertexTexture ("0.000000 4.000000"); |
---|
| 681 | readVertexTexture ("1.000000 4.000000"); |
---|
| 682 | readVertexTexture ("2.000000 0.000000"); |
---|
| 683 | readVertexTexture ("2.000000 1.000000"); |
---|
| 684 | readVertexTexture ("-1.000000 0.000000"); |
---|
| 685 | readVertexTexture ("-1.000000 1.000000"); |
---|
| 686 | |
---|
| 687 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 688 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 689 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 690 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 691 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 692 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 693 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 694 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 695 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 696 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 697 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 698 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 699 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 700 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 701 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 702 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 703 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 704 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 705 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 706 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 707 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 708 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 709 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 710 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 711 | |
---|
| 712 | readFace ("1/1/1 2/2/2 4/4/3 3/3/4"); |
---|
| 713 | readFace ("3/3/5 4/4/6 6/6/7 5/5/8"); |
---|
| 714 | readFace ("5/5/9 6/6/10 8/8/11 7/7/12"); |
---|
| 715 | readFace ("7/7/13 8/8/14 2/10/15 1/9/16"); |
---|
| 716 | readFace ("2/2/17 8/11/18 6/12/19 4/4/20"); |
---|
| 717 | readFace ("7/13/21 1/1/22 3/3/23 5/14/24"); |
---|
| 718 | } |
---|