[2835] | 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 | |
---|
| 16 | int verbose = 0; |
---|
| 17 | |
---|
| 18 | #include "object.h" |
---|
| 19 | |
---|
| 20 | Object::Object () |
---|
| 21 | { |
---|
| 22 | |
---|
| 23 | initialize(); |
---|
| 24 | |
---|
| 25 | importFile ("reaphigh.obj"); |
---|
| 26 | |
---|
| 27 | finalize(); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | Object::Object(char* fileName) |
---|
| 31 | { |
---|
| 32 | initialize(); |
---|
| 33 | |
---|
| 34 | importFile (fileName); |
---|
| 35 | |
---|
| 36 | finalize(); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | Object::Object(char* fileName, float scaling) |
---|
| 40 | { |
---|
| 41 | initialize(); |
---|
| 42 | scaleFactor = scaling; |
---|
| 43 | |
---|
| 44 | importFile (fileName); |
---|
| 45 | |
---|
| 46 | finalize(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | bool Object::initialize (void) |
---|
| 50 | { |
---|
| 51 | if (verbose >=3) |
---|
| 52 | printf("new 3D-Object is being created\n"); |
---|
| 53 | faceMode = -1; |
---|
| 54 | if ( (listNumber = glGenLists(1)) == 0 ) |
---|
| 55 | { |
---|
| 56 | printf ("list could not be created for this Object\n"); |
---|
| 57 | return false; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | mtlFileName = ""; |
---|
| 61 | scaleFactor = 1; |
---|
| 62 | vertices = new Array(); |
---|
| 63 | normals = new Array(); |
---|
| 64 | vTexture = new Array(); |
---|
| 65 | |
---|
| 66 | glNewList (listNumber, GL_COMPILE); |
---|
| 67 | glEnableClientState (GL_VERTEX_ARRAY); |
---|
| 68 | glEnableClientState (GL_NORMAL_ARRAY); |
---|
| 69 | // glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
---|
| 70 | |
---|
| 71 | return true; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | bool Object::importFile (char* fileName) |
---|
| 75 | { |
---|
| 76 | if (verbose >=3) |
---|
| 77 | printf("preparing to read in file: %s\n", fileName); |
---|
| 78 | objFileName = fileName; |
---|
| 79 | this->readFromObjFile (objFileName); |
---|
| 80 | return true; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | bool Object::finalize(void) |
---|
| 84 | { |
---|
| 85 | if (verbose >=3) |
---|
| 86 | printf("finalizing the 3D-Object\n"); |
---|
| 87 | OBJ_FILE->close(); |
---|
| 88 | glEnd(); |
---|
| 89 | glEndList(); |
---|
| 90 | return true; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void Object::draw (void) |
---|
| 94 | { |
---|
| 95 | if (verbose >=3) |
---|
| 96 | printf("drawing the 3D-Object\n"); |
---|
| 97 | glCallList (listNumber); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | bool Object::readFromObjFile (char* fileName) |
---|
| 102 | { |
---|
| 103 | OBJ_FILE = new ifstream(fileName); |
---|
| 104 | if (!OBJ_FILE->is_open()) |
---|
| 105 | { |
---|
| 106 | if (verbose >=1) |
---|
| 107 | printf ("unable to open .OBJ file: %s\n Loading Box Object instead.\n", fileName); |
---|
| 108 | BoxObject(); |
---|
| 109 | return false; |
---|
| 110 | } |
---|
| 111 | objFileName = fileName; |
---|
| 112 | char Buffer[500]; |
---|
| 113 | while(!OBJ_FILE->eof()) |
---|
| 114 | { |
---|
| 115 | OBJ_FILE->getline(Buffer, 500); |
---|
| 116 | if (verbose >=4) |
---|
| 117 | printf ("Read input line: %s\n",Buffer); |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | // case vertice |
---|
| 121 | if (!strncmp(Buffer, "v ", 2)) |
---|
| 122 | { |
---|
| 123 | readVertex(Buffer+2); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // case face |
---|
| 127 | else if (!strncmp(Buffer, "f ", 2)) |
---|
| 128 | { |
---|
| 129 | readFace (Buffer+2); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | else if (!strncmp(Buffer, "mtllib", 6)) |
---|
| 133 | { |
---|
| 134 | readMtlLib (Buffer+7); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | else if (!strncmp(Buffer, "usemtl", 6)) |
---|
| 138 | { |
---|
| 139 | readUseMtl (Buffer+7); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | // case VertexNormal |
---|
| 143 | else if (!strncmp(Buffer, "vn ", 2)) |
---|
| 144 | { |
---|
| 145 | readVertexNormal(Buffer+3); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | // case vt |
---|
| 149 | else if (!strncmp(Buffer, "vt ", 2)) |
---|
| 150 | { |
---|
| 151 | readVertexTexture(Buffer+3); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | bool Object::readVertex (char* vertexString) |
---|
| 164 | { |
---|
| 165 | readVertices = true; |
---|
| 166 | char subbuffer1[20]; |
---|
| 167 | char subbuffer2[20]; |
---|
| 168 | char subbuffer3[20]; |
---|
| 169 | sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
| 170 | if (verbose >= 3) |
---|
| 171 | printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3); |
---|
| 172 | vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor); |
---|
| 173 | return true; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | bool Object::readFace (char* faceString) |
---|
| 177 | { |
---|
| 178 | if (readVertices == true) |
---|
| 179 | { |
---|
| 180 | vertices->finalizeArray(); |
---|
| 181 | glVertexPointer(3, GL_FLOAT, 0, vertices->getArray()); |
---|
| 182 | normals->finalizeArray(); |
---|
| 183 | glNormalPointer(GL_FLOAT, 0, normals->getArray()); |
---|
| 184 | vTexture->finalizeArray(); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | readVertices = false; |
---|
| 188 | char subbuffer1[20]; |
---|
| 189 | char subbuffer2[20]; |
---|
| 190 | char subbuffer3[20]; |
---|
| 191 | char subbuffer4[20] =""; |
---|
| 192 | sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4); |
---|
| 193 | if (!strcmp(subbuffer4, "")) |
---|
| 194 | { |
---|
| 195 | if (faceMode != 3) |
---|
| 196 | { |
---|
| 197 | if (faceMode != -1) |
---|
| 198 | glEnd(); |
---|
| 199 | glBegin(GL_TRIANGLES); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | faceMode = 3; |
---|
| 203 | if (verbose >=3) |
---|
| 204 | printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3); |
---|
| 205 | addGLElement(subbuffer1); |
---|
| 206 | addGLElement(subbuffer2); |
---|
| 207 | addGLElement(subbuffer3); |
---|
| 208 | return true; |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | if (faceMode != 4) |
---|
| 213 | { |
---|
| 214 | if (faceMode != -1) |
---|
| 215 | glEnd(); |
---|
| 216 | glBegin(GL_QUADS); |
---|
| 217 | } |
---|
| 218 | faceMode = 4; |
---|
| 219 | if (verbose >=3 ) |
---|
| 220 | printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4); |
---|
| 221 | addGLElement(subbuffer1); |
---|
| 222 | addGLElement(subbuffer2); |
---|
| 223 | addGLElement(subbuffer3); |
---|
| 224 | addGLElement(subbuffer4); |
---|
| 225 | return true; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | bool Object::addGLElement (char* elementString) |
---|
| 230 | { |
---|
| 231 | if (verbose >=3) |
---|
| 232 | printf ("importing grafical Element.... including to openGL\n"); |
---|
| 233 | char* vertex = elementString; |
---|
| 234 | |
---|
| 235 | char* texture; |
---|
| 236 | texture = strstr (vertex, "/"); |
---|
| 237 | texture[0] = '\0'; |
---|
| 238 | texture ++; |
---|
| 239 | glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2); |
---|
| 240 | |
---|
| 241 | char* normal; |
---|
| 242 | if ((normal = strstr (texture, "/")) !=NULL) |
---|
| 243 | { |
---|
| 244 | normal[0] = '\0'; |
---|
| 245 | normal ++; |
---|
| 246 | //glArrayElement(atoi(vertex)-1); |
---|
| 247 | glNormal3fv(normals->getArray() +(atoi(normal)-1)*3); |
---|
| 248 | } |
---|
| 249 | glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3); |
---|
| 250 | |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | bool Object::readVertexNormal (char* normalString) |
---|
| 254 | { |
---|
| 255 | readVertices = true; |
---|
| 256 | char subbuffer1[20]; |
---|
| 257 | char subbuffer2[20]; |
---|
| 258 | char subbuffer3[20]; |
---|
| 259 | sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
| 260 | if (verbose >=3 ) |
---|
| 261 | printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3); |
---|
| 262 | normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3)); |
---|
| 263 | return true; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | bool Object::readVertexTexture (char* vTextureString) |
---|
| 267 | { |
---|
| 268 | readVertices = true; |
---|
| 269 | char subbuffer1[20]; |
---|
| 270 | char subbuffer2[20]; |
---|
| 271 | sscanf (vTextureString, "%s %s", subbuffer1, subbuffer2); |
---|
| 272 | if (verbose >=3 ) |
---|
| 273 | printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2); |
---|
| 274 | vTexture->addEntry(atof(subbuffer1)); |
---|
| 275 | vTexture->addEntry(atof(subbuffer2)); |
---|
| 276 | return true; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | bool Object::readMtlLib (char* mtlFile) |
---|
| 281 | { |
---|
| 282 | MTL_FILE = new ifstream (mtlFile); |
---|
| 283 | if (!MTL_FILE->is_open()) |
---|
| 284 | { |
---|
| 285 | if (verbose >= 1) |
---|
| 286 | printf ("unable to open file: %s\n", mtlFile); |
---|
| 287 | return false; |
---|
| 288 | } |
---|
| 289 | mtlFileName = mtlFile; |
---|
| 290 | if (verbose >=2) |
---|
| 291 | printf ("Opening mtlFile: %s\n", mtlFileName); |
---|
| 292 | char Buffer[500]; |
---|
| 293 | vertices = new Array(); |
---|
| 294 | material = new Material(); |
---|
| 295 | Material* tmpMat = material; |
---|
| 296 | while(!MTL_FILE->eof()) |
---|
| 297 | { |
---|
| 298 | MTL_FILE->getline(Buffer, 500); |
---|
| 299 | if (verbose >= 4) |
---|
| 300 | printf("found line in mtlFile: %s\n", Buffer); |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | // create new Material |
---|
| 304 | if (!strncmp(Buffer, "newmtl ", 2)) |
---|
| 305 | { |
---|
| 306 | tmpMat = tmpMat->addMaterial(Buffer+7); |
---|
| 307 | // printf ("%s, %p\n", tmpMat->getName(), tmpMat); |
---|
| 308 | } |
---|
| 309 | // setting a illumMode |
---|
| 310 | else if (!strncmp(Buffer, "illum", 5)) |
---|
| 311 | { |
---|
| 312 | tmpMat->setIllum(Buffer+6); |
---|
| 313 | |
---|
| 314 | } |
---|
| 315 | // setting Diffuse Color |
---|
| 316 | else if (!strncmp(Buffer, "Kd", 2)) |
---|
| 317 | { |
---|
| 318 | tmpMat->setDiffuse(Buffer+3); |
---|
| 319 | } |
---|
| 320 | // setting Ambient Color |
---|
| 321 | else if (!strncmp(Buffer, "Ka", 2)) |
---|
| 322 | { |
---|
| 323 | tmpMat->setAmbient(Buffer+3); |
---|
| 324 | } |
---|
| 325 | // setting Specular Color |
---|
| 326 | else if (!strncmp(Buffer, "Ks", 2)) |
---|
| 327 | { |
---|
| 328 | tmpMat->setSpecular(Buffer+3); |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | return true; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | bool Object::readUseMtl (char* matString) |
---|
| 335 | { |
---|
| 336 | if (!strcmp (mtlFileName, "")) |
---|
| 337 | { |
---|
| 338 | if (verbose >= 1) |
---|
| 339 | printf ("Not using new defined material, because no mtlFile found yet\n"); |
---|
| 340 | return false; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | if (faceMode != -1) |
---|
| 344 | glEnd(); |
---|
| 345 | faceMode = 0; |
---|
| 346 | if (verbose >= 2) |
---|
| 347 | printf ("using material %s for coming Faces.\n", matString); |
---|
| 348 | material->search(matString)->select(); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | void Object::BoxObject(void) |
---|
| 353 | { |
---|
| 354 | readVertex ("-0.500000 -0.500000 0.500000"); |
---|
| 355 | readVertex ("0.500000 -0.500000 0.500000"); |
---|
| 356 | readVertex ("-0.500000 0.500000 0.500000"); |
---|
| 357 | readVertex ("0.500000 0.500000 0.500000"); |
---|
| 358 | readVertex ("-0.500000 0.500000 -0.500000"); |
---|
| 359 | readVertex ("0.500000 0.500000 -0.500000"); |
---|
| 360 | readVertex ("-0.500000 -0.500000 -0.500000"); |
---|
| 361 | readVertex ("0.500000 -0.500000 -0.500000"); |
---|
| 362 | readVertexTexture ("0.000000 0.000000"); |
---|
| 363 | readVertexTexture ("1.000000 0.000000"); |
---|
| 364 | readVertexTexture ("0.000000 1.000000"); |
---|
| 365 | readVertexTexture ("1.000000 1.000000"); |
---|
| 366 | readVertexTexture ("0.000000 2.000000"); |
---|
| 367 | readVertexTexture ("1.000000 2.000000"); |
---|
| 368 | readVertexTexture ("0.000000 3.000000"); |
---|
| 369 | readVertexTexture ("1.000000 3.000000"); |
---|
| 370 | readVertexTexture ("0.000000 4.000000"); |
---|
| 371 | readVertexTexture ("1.000000 4.000000"); |
---|
| 372 | readVertexTexture ("2.000000 0.000000"); |
---|
| 373 | readVertexTexture ("2.000000 1.000000"); |
---|
| 374 | readVertexTexture ("-1.000000 0.000000"); |
---|
| 375 | readVertexTexture ("-1.000000 1.000000"); |
---|
| 376 | |
---|
| 377 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 378 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 379 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 380 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
| 381 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 382 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 383 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 384 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
| 385 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 386 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 387 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 388 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
| 389 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 390 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 391 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 392 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
| 393 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 394 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 395 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 396 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
| 397 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 398 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 399 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 400 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
| 401 | |
---|
| 402 | readFace ("1/1/1 2/2/2 4/4/3 3/3/4"); |
---|
| 403 | readFace ("3/3/5 4/4/6 6/6/7 5/5/8"); |
---|
| 404 | readFace ("5/5/9 6/6/10 8/8/11 7/7/12"); |
---|
| 405 | readFace ("7/7/13 8/8/14 2/10/15 1/9/16"); |
---|
| 406 | readFace ("2/2/17 8/11/18 6/12/19 4/4/20"); |
---|
| 407 | readFace ("7/13/21 1/1/22 3/3/23 5/14/24"); |
---|
| 408 | } |
---|