[2795] | 1 | #include "object.h" |
---|
| 2 | |
---|
[2810] | 3 | int verbose = 1; |
---|
[2795] | 4 | Object::Object () |
---|
| 5 | { |
---|
| 6 | |
---|
| 7 | initialize(); |
---|
| 8 | |
---|
| 9 | importFile ("reaphigh.obj"); |
---|
| 10 | |
---|
| 11 | finalize(); |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | Object::Object(char* fileName) |
---|
| 15 | { |
---|
| 16 | initialize(); |
---|
| 17 | |
---|
| 18 | importFile (fileName); |
---|
| 19 | |
---|
| 20 | finalize(); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | bool Object::importFile (char* fileName) |
---|
| 24 | { |
---|
[2810] | 25 | if (verbose >=3) |
---|
| 26 | printf("preparing to read in file: %s\n", fileName); |
---|
| 27 | objFileName = fileName; |
---|
| 28 | this->readFromObjFile (objFileName); |
---|
[2795] | 29 | return true; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | bool Object::initialize (void) |
---|
| 33 | { |
---|
[2810] | 34 | if (verbose >=3) |
---|
| 35 | printf("new 3D-Object is being created\n"); |
---|
[2795] | 36 | faceMode = -1; |
---|
| 37 | if ( (listNumber = glGenLists(1)) == 0 ) |
---|
| 38 | { |
---|
| 39 | printf ("list could not be created for this Object\n"); |
---|
| 40 | return false; |
---|
| 41 | } |
---|
| 42 | glNewList (listNumber, GL_COMPILE); |
---|
| 43 | glEnableClientState (GL_VERTEX_ARRAY); |
---|
| 44 | // glEnableClientState (GL_NORMAL_ARRAY); |
---|
| 45 | // glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
---|
| 46 | |
---|
| 47 | return true; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | bool Object::finalize(void) |
---|
| 51 | { |
---|
[2810] | 52 | if (verbose >=3) |
---|
| 53 | printf("finalizing the 3D-Object\n"); |
---|
[2795] | 54 | glEndList(); |
---|
| 55 | return true; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void Object::draw (void) |
---|
| 59 | { |
---|
[2810] | 60 | if (verbose >=3) |
---|
| 61 | printf("drawing the 3D-Object\n"); |
---|
[2795] | 62 | glCallList (listNumber); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | bool Object::readFromObjFile (char* fileName) |
---|
| 67 | { |
---|
| 68 | OBJ_FILE = new ifstream (fileName); |
---|
| 69 | if (!OBJ_FILE->is_open()) |
---|
| 70 | { |
---|
[2810] | 71 | if (verbose >=1) |
---|
| 72 | printf ("unable to open .OBJ file: %s\n", fileName); |
---|
[2795] | 73 | return false; |
---|
| 74 | } |
---|
[2810] | 75 | objFileName = fileName; |
---|
[2795] | 76 | char Buffer[500]; |
---|
| 77 | vertices = new Array(); |
---|
| 78 | normals = new Array(); |
---|
| 79 | while(!OBJ_FILE->eof()) |
---|
| 80 | { |
---|
| 81 | OBJ_FILE->getline(Buffer, 500); |
---|
[2810] | 82 | if (verbose >=4) |
---|
| 83 | printf ("Read input line: %s\n",Buffer); |
---|
[2795] | 84 | |
---|
| 85 | |
---|
| 86 | // case vertice |
---|
| 87 | if (!strncmp(Buffer, "v ", 2)) |
---|
| 88 | { |
---|
| 89 | readVertex(Buffer+2); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | // case face |
---|
| 93 | else if (!strncmp(Buffer, "f ", 2)) |
---|
| 94 | { |
---|
| 95 | readFace (Buffer+2); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | else if (!strncmp(Buffer, "mtllib", 6)) |
---|
| 99 | { |
---|
| 100 | readMtlLib (Buffer+7); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | else if (!strncmp(Buffer, "usemtl", 6)) |
---|
| 104 | { |
---|
| 105 | readUseMtl (Buffer+7); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // case VertexNormal |
---|
| 109 | else if (!strncmp(Buffer, "vn ", 2)) |
---|
| 110 | { |
---|
| 111 | readVertexNormal(Buffer+3); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // case vt |
---|
| 115 | else if (!strncmp(Buffer, "vt ", 2)) |
---|
| 116 | { |
---|
| 117 | // printf ("verticeTangent found"); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | OBJ_FILE->close(); |
---|
| 126 | glEnd(); |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | bool Object::readVertex (char* vertexString) |
---|
| 132 | { |
---|
| 133 | readVertices = true; |
---|
| 134 | char subbuffer1[20]; |
---|
| 135 | char subbuffer2[20]; |
---|
| 136 | char subbuffer3[20]; |
---|
| 137 | sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
[2810] | 138 | if (verbose >= 3) |
---|
| 139 | printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3); |
---|
[2795] | 140 | vertices->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3)); |
---|
| 141 | return true; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | bool Object::readFace (char* faceString) |
---|
| 145 | { |
---|
| 146 | if (readVertices == true) |
---|
| 147 | { |
---|
| 148 | vertices->finalizeArray(); |
---|
| 149 | glVertexPointer(3, GL_FLOAT, 0, vertices->getArray()); |
---|
| 150 | normals->finalizeArray(); |
---|
| 151 | glNormalPointer(GL_FLOAT, 0, normals->getArray()); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | readVertices = false; |
---|
| 155 | char subbuffer1[20]; |
---|
| 156 | char subbuffer2[20]; |
---|
| 157 | char subbuffer3[20]; |
---|
| 158 | char subbuffer4[20] =""; |
---|
| 159 | sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4); |
---|
| 160 | if (!strcmp(subbuffer4, "")) |
---|
| 161 | { |
---|
| 162 | if (faceMode != 3) |
---|
| 163 | { |
---|
| 164 | if (faceMode != -1) |
---|
| 165 | glEnd(); |
---|
| 166 | glBegin(GL_TRIANGLES); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | faceMode = 3; |
---|
[2810] | 170 | if (verbose >=3) |
---|
| 171 | printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3); |
---|
[2795] | 172 | addGLElement(subbuffer1); |
---|
| 173 | addGLElement(subbuffer2); |
---|
| 174 | addGLElement(subbuffer3); |
---|
| 175 | return true; |
---|
| 176 | } |
---|
| 177 | else |
---|
| 178 | { |
---|
| 179 | if (faceMode != 4) |
---|
| 180 | { |
---|
| 181 | if (faceMode != -1) |
---|
| 182 | glEnd(); |
---|
| 183 | glBegin(GL_QUADS); |
---|
| 184 | } |
---|
| 185 | faceMode = 4; |
---|
[2810] | 186 | if (verbose >=3 ) |
---|
| 187 | printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4); |
---|
[2795] | 188 | addGLElement(subbuffer1); |
---|
| 189 | addGLElement(subbuffer2); |
---|
| 190 | addGLElement(subbuffer3); |
---|
| 191 | addGLElement(subbuffer4); |
---|
| 192 | return true; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | bool Object::addGLElement (char* elementString) |
---|
| 197 | { |
---|
| 198 | char* vertex = elementString; |
---|
| 199 | char* texture; |
---|
| 200 | char* normal; |
---|
| 201 | texture = strstr (vertex, "/"); |
---|
| 202 | texture[0] = '\0'; |
---|
| 203 | texture ++; |
---|
| 204 | normal = strstr (texture, "/"); |
---|
| 205 | normal[0] = '\0'; |
---|
| 206 | normal ++; |
---|
[2810] | 207 | if (verbose >= 4) |
---|
| 208 | printf ("importing grafical Element.... including to openGL\n"); |
---|
[2795] | 209 | glNormal3fv(normals->getArray() +(atoi(normal)-1)*3); |
---|
| 210 | glArrayElement(atoi(vertex)-1); // glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3); |
---|
| 211 | |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | bool Object::readVertexNormal (char* normalString) |
---|
| 215 | { |
---|
| 216 | readVertices = true; |
---|
| 217 | char subbuffer1[20]; |
---|
| 218 | char subbuffer2[20]; |
---|
| 219 | char subbuffer3[20]; |
---|
| 220 | sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
[2810] | 221 | if (verbose >=3 ) |
---|
| 222 | printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3); |
---|
[2795] | 223 | normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3)); |
---|
| 224 | return true; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | bool Object::readMtlLib (char* mtlFile) |
---|
| 228 | { |
---|
| 229 | MTL_FILE = new ifstream (mtlFile); |
---|
| 230 | if (!MTL_FILE->is_open()) |
---|
| 231 | { |
---|
[2810] | 232 | if (verbose >= 1) |
---|
| 233 | printf ("unable to open file: %s\n", mtlFile); |
---|
[2795] | 234 | return false; |
---|
| 235 | } |
---|
[2810] | 236 | mtlFileName = mtlFile; |
---|
| 237 | if (verbose >=2) |
---|
| 238 | printf ("Opening mtlFile: %s\n", mtlFileName); |
---|
[2795] | 239 | char Buffer[500]; |
---|
| 240 | vertices = new Array(); |
---|
| 241 | material = new Material(); |
---|
| 242 | Material* tmpMat = material; |
---|
| 243 | while(!MTL_FILE->eof()) |
---|
| 244 | { |
---|
| 245 | MTL_FILE->getline(Buffer, 500); |
---|
[2810] | 246 | if (verbose >= 4) |
---|
| 247 | printf("found line in mtlFile: %s\n", Buffer); |
---|
[2795] | 248 | |
---|
| 249 | |
---|
| 250 | // create new Material |
---|
| 251 | if (!strncmp(Buffer, "newmtl ", 2)) |
---|
| 252 | { |
---|
| 253 | tmpMat = tmpMat->addMaterial(Buffer+7); |
---|
| 254 | // printf ("%s, %p\n", tmpMat->getName(), tmpMat); |
---|
| 255 | } |
---|
| 256 | // setting a illumMode |
---|
| 257 | else if (!strncmp(Buffer, "illum", 5)) |
---|
| 258 | { |
---|
| 259 | tmpMat->setIllum(Buffer+6); |
---|
| 260 | |
---|
| 261 | } |
---|
| 262 | // setting Diffuse Color |
---|
| 263 | else if (!strncmp(Buffer, "Kd", 2)) |
---|
| 264 | { |
---|
| 265 | tmpMat->setDiffuse(Buffer+3); |
---|
| 266 | } |
---|
| 267 | // setting Ambient Color |
---|
| 268 | else if (!strncmp(Buffer, "Ka", 2)) |
---|
| 269 | { |
---|
| 270 | tmpMat->setAmbient(Buffer+3); |
---|
| 271 | } |
---|
| 272 | // setting Specular Color |
---|
| 273 | else if (!strncmp(Buffer, "Ks", 2)) |
---|
| 274 | { |
---|
| 275 | tmpMat->setSpecular(Buffer+3); |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | return true; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | bool Object::readUseMtl (char* matString) |
---|
| 282 | { |
---|
| 283 | if (faceMode != -1) |
---|
| 284 | glEnd(); |
---|
| 285 | faceMode = 0; |
---|
[2810] | 286 | if (verbose >= 2) |
---|
| 287 | printf ("using material %s for coming Faces.\n", matString); |
---|
[2795] | 288 | material->search(matString)->select(); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | |
---|