[3396] | 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 | |
---|
[3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 17 | |
---|
[3396] | 18 | #include "objModel.h" |
---|
| 19 | |
---|
[3908] | 20 | #include <stdio.h> |
---|
| 21 | #include <string.h> |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | |
---|
| 24 | #define PARSELINELENGTH 8192 |
---|
| 25 | |
---|
[3475] | 26 | #include "debug.h" |
---|
[3427] | 27 | |
---|
[3396] | 28 | /** |
---|
| 29 | \brief Crates a 3D-Model, loads in a File and scales it. |
---|
| 30 | \param fileName file to parse and load (must be a .obj file) |
---|
| 31 | \param scaling The factor that the model will be scaled with. |
---|
| 32 | */ |
---|
| 33 | OBJModel::OBJModel(char* fileName, float scaling) |
---|
| 34 | { |
---|
| 35 | this->initializeOBJ(); |
---|
| 36 | this->scaleFactor = scaling; |
---|
| 37 | |
---|
| 38 | this->importFile (fileName); |
---|
| 39 | |
---|
| 40 | this->importToGL (); |
---|
| 41 | |
---|
| 42 | this->cleanup(); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | \brief deletes an OBJModel. |
---|
| 47 | |
---|
| 48 | Looks if any from model allocated space is still in use, and if so deleted it. |
---|
| 49 | */ |
---|
| 50 | OBJModel::~OBJModel() |
---|
| 51 | { |
---|
| 52 | PRINTF(4)("Deleting the obj-names\n"); |
---|
| 53 | if (this->objPath) |
---|
| 54 | delete []this->objPath; |
---|
| 55 | if (this->objFileName) |
---|
| 56 | delete []this->objFileName; |
---|
| 57 | if (this->mtlFileName) |
---|
| 58 | delete []this->mtlFileName; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | \brief Initializes an obj-model |
---|
| 63 | */ |
---|
| 64 | void OBJModel::initializeOBJ(void) |
---|
| 65 | { |
---|
| 66 | this->objPath = NULL; |
---|
| 67 | this->objFileName = NULL; |
---|
| 68 | this->mtlFileName = NULL; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | \brief Imports a obj file and handles the the relative location |
---|
| 73 | \param fileName The file to import |
---|
| 74 | */ |
---|
| 75 | bool OBJModel::importFile (char* fileName) |
---|
| 76 | { |
---|
[3548] | 77 | PRINTF(4)("preparing to read in file: %s\n", fileName); |
---|
[3396] | 78 | |
---|
| 79 | |
---|
| 80 | #ifdef __WIN32__ |
---|
| 81 | // win32 path reading |
---|
| 82 | char pathSplitter= '\\'; |
---|
| 83 | #else /* __WIN32__ */ |
---|
| 84 | // unix path reading |
---|
| 85 | char pathSplitter='/'; |
---|
| 86 | #endif /* __WIN32__ */ |
---|
| 87 | char* tmpName = fileName; |
---|
| 88 | if (tmpName[0] == pathSplitter) |
---|
| 89 | tmpName++; |
---|
| 90 | char* name = tmpName; |
---|
| 91 | while (( tmpName = strchr (tmpName+1, pathSplitter))) |
---|
| 92 | { |
---|
| 93 | name = tmpName+1; |
---|
| 94 | } |
---|
| 95 | this->objPath = new char[name-fileName+1]; |
---|
| 96 | strncpy(this->objPath, fileName, name-fileName); |
---|
| 97 | this->objPath[name-fileName] = '\0'; |
---|
[3548] | 98 | if (strlen(objPath)> 0) |
---|
| 99 | PRINTF(5)("Resolved file %s to folder: %s.\n", name, objPath); |
---|
| 100 | else |
---|
| 101 | PRINTF(5)("Resolved file %s.\n", name); |
---|
[3396] | 102 | |
---|
[3398] | 103 | this->setName(name); |
---|
[3396] | 104 | if (this->material) |
---|
| 105 | this->material->addTexturePath(this->objPath); |
---|
| 106 | this->objFileName = new char[strlen(name)+1]; |
---|
| 107 | strcpy (this->objFileName, name); |
---|
| 108 | this->readFromObjFile (); |
---|
| 109 | return true; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | \brief Reads in the .obj File and sets all the Values. |
---|
| 114 | This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks |
---|
| 115 | */ |
---|
| 116 | bool OBJModel::readFromObjFile (void) |
---|
| 117 | { |
---|
| 118 | char* fileName = new char [strlen(objPath)+strlen(objFileName)+1]; |
---|
| 119 | if (this->objFileName != NULL && !strcmp(this->objFileName, "")) |
---|
| 120 | return false; |
---|
| 121 | strcpy(fileName, this->objPath); |
---|
| 122 | strcat(fileName, this->objFileName); |
---|
| 123 | |
---|
[3908] | 124 | FILE* stream; |
---|
| 125 | if( (stream = fopen (fileName, "r")) == NULL) |
---|
[3396] | 126 | { |
---|
[3908] | 127 | printf("IniParser could not open %s\n", fileName); |
---|
[3396] | 128 | return false; |
---|
| 129 | } |
---|
| 130 | |
---|
[3908] | 131 | char buffer[PARSELINELENGTH]; |
---|
[3910] | 132 | while(fgets(buffer, PARSELINELENGTH, stream)) |
---|
| 133 | { |
---|
| 134 | // line termiated with \0 not \n |
---|
| 135 | if (buffer[strlen(buffer)-1] == '\n') |
---|
| 136 | buffer[strlen(buffer)-1] = '\0'; |
---|
| 137 | |
---|
[3396] | 138 | // case vertice |
---|
[3908] | 139 | if (!strncmp(buffer, "v ", 2)) |
---|
[3396] | 140 | { |
---|
[3908] | 141 | this->addVertex(buffer+2); |
---|
[3396] | 142 | } |
---|
| 143 | |
---|
| 144 | // case face |
---|
[3908] | 145 | else if (!strncmp(buffer, "f ", 2)) |
---|
[3396] | 146 | { |
---|
[3908] | 147 | this->addFace (buffer+2); |
---|
[3396] | 148 | } |
---|
| 149 | |
---|
[3908] | 150 | else if (!strncmp(buffer, "mtllib ", 7)) |
---|
[3396] | 151 | { |
---|
[3908] | 152 | this->readMtlLib (buffer+7); |
---|
[3396] | 153 | } |
---|
| 154 | |
---|
[3908] | 155 | else if (!strncmp(buffer, "usemtl ", 7)) |
---|
[3396] | 156 | { |
---|
[3908] | 157 | this->addUseMtl (buffer+7); |
---|
[3396] | 158 | } |
---|
| 159 | |
---|
| 160 | // case VertexNormal |
---|
[3908] | 161 | else if (!strncmp(buffer, "vn ", 3)) |
---|
[3396] | 162 | { |
---|
[3908] | 163 | this->addVertexNormal(buffer+3); |
---|
[3396] | 164 | } |
---|
| 165 | |
---|
| 166 | // case VertexTextureCoordinate |
---|
[3908] | 167 | else if (!strncmp(buffer, "vt ", 3)) |
---|
[3396] | 168 | { |
---|
[3908] | 169 | this->addVertexTexture(buffer+3); |
---|
[3396] | 170 | } |
---|
| 171 | // case group |
---|
[3908] | 172 | else if (!strncmp(buffer, "g ", 2)) |
---|
[3396] | 173 | { |
---|
[3908] | 174 | this->addGroup (buffer+2); |
---|
[3396] | 175 | } |
---|
[3908] | 176 | else if (!strncmp(buffer, "s ", 2)) //! \todo smoothing groups have to be implemented |
---|
[3396] | 177 | { |
---|
[3908] | 178 | PRINTF(2)("smoothing groups not supportet yet. line: %s\n", buffer); |
---|
[3396] | 179 | } |
---|
| 180 | } |
---|
[3908] | 181 | fclose (stream); |
---|
[3396] | 182 | delete []fileName; |
---|
| 183 | return true; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | \brief Function to read in a mtl File. |
---|
| 188 | \param mtlFile The .mtl file to read |
---|
| 189 | |
---|
| 190 | This Function parses all Lines of an mtl File. |
---|
| 191 | The reason for it not to be in the materials-class is, |
---|
| 192 | that a material does not have to be able to read itself in from a File. |
---|
| 193 | |
---|
| 194 | */ |
---|
| 195 | bool OBJModel::readMtlLib (char* mtlFile) |
---|
| 196 | { |
---|
| 197 | this->mtlFileName = new char [strlen(mtlFile)+1]; |
---|
| 198 | strcpy(this->mtlFileName, mtlFile); |
---|
| 199 | char* fileName = new char [strlen(objPath) + strlen(this->mtlFileName)+1]; |
---|
| 200 | strcpy(fileName, this->objPath); |
---|
| 201 | strcat(fileName, this->mtlFileName); |
---|
| 202 | |
---|
| 203 | |
---|
[3909] | 204 | FILE* stream; |
---|
| 205 | if( (stream = fopen (fileName, "r")) == NULL) |
---|
[3396] | 206 | { |
---|
[3909] | 207 | printf("IniParser could not open %s\n", fileName); |
---|
[3396] | 208 | return false; |
---|
| 209 | } |
---|
[3909] | 210 | |
---|
| 211 | char buffer[PARSELINELENGTH]; |
---|
[3396] | 212 | Material* tmpMat = material; |
---|
[3910] | 213 | while(fgets(buffer, PARSELINELENGTH, stream)) |
---|
[3396] | 214 | { |
---|
[3908] | 215 | PRINTF(5)("found line in mtlFile: %s\n", buffer); |
---|
[3396] | 216 | |
---|
[3910] | 217 | // line termiated with \0 not \n |
---|
| 218 | if (buffer[strlen(buffer)-1] == '\n') |
---|
| 219 | buffer[strlen(buffer)-1] = '\0'; |
---|
| 220 | |
---|
[3396] | 221 | // create new Material |
---|
[3908] | 222 | if (!strncmp(buffer, "newmtl ", 7)) |
---|
[3396] | 223 | { |
---|
[3908] | 224 | tmpMat = tmpMat->addMaterial(buffer+7); |
---|
[3396] | 225 | // PRINTF(2)("%s, %p\n", tmpMat->getName(), tmpMat); |
---|
| 226 | } |
---|
| 227 | // setting a illumMode |
---|
[3908] | 228 | else if (!strncmp(buffer, "illum ", 6)) |
---|
[3396] | 229 | { |
---|
[3908] | 230 | tmpMat->setIllum(buffer+6); |
---|
[3396] | 231 | |
---|
| 232 | } |
---|
| 233 | // setting Diffuse Color |
---|
[3908] | 234 | else if (!strncmp(buffer, "Kd ", 3)) |
---|
[3396] | 235 | { |
---|
[3908] | 236 | tmpMat->setDiffuse(buffer+3); |
---|
[3396] | 237 | } |
---|
| 238 | // setting Ambient Color |
---|
[3908] | 239 | else if (!strncmp(buffer, "Ka ", 3)) |
---|
[3396] | 240 | { |
---|
[3908] | 241 | tmpMat->setAmbient(buffer+3); |
---|
[3396] | 242 | } |
---|
| 243 | // setting Specular Color |
---|
[3908] | 244 | else if (!strncmp(buffer, "Ks ", 3)) |
---|
[3396] | 245 | { |
---|
[3908] | 246 | tmpMat->setSpecular(buffer+3); |
---|
[3396] | 247 | } |
---|
| 248 | // setting The Specular Shininess |
---|
[3908] | 249 | else if (!strncmp(buffer, "Ns ", 3)) |
---|
[3396] | 250 | { |
---|
[3908] | 251 | tmpMat->setShininess(buffer+3); |
---|
[3396] | 252 | } |
---|
| 253 | // setting up transparency |
---|
[3908] | 254 | else if (!strncmp(buffer, "d ", 2)) |
---|
[3396] | 255 | { |
---|
[3908] | 256 | tmpMat->setTransparency(buffer+2); |
---|
[3396] | 257 | } |
---|
[3908] | 258 | else if (!strncmp(buffer, "Tf ", 3)) |
---|
[3396] | 259 | { |
---|
[3908] | 260 | tmpMat->setTransparency(buffer+3); |
---|
[3396] | 261 | } |
---|
| 262 | |
---|
[3908] | 263 | else if (!strncmp(buffer, "map_Kd ", 7)) |
---|
[3396] | 264 | { |
---|
[3908] | 265 | tmpMat->setDiffuseMap(buffer+7); |
---|
[3396] | 266 | } |
---|
[3908] | 267 | else if (!strncmp(buffer, "map_Ka ", 7)) |
---|
[3396] | 268 | { |
---|
[3908] | 269 | tmpMat->setAmbientMap(buffer+7); |
---|
[3396] | 270 | } |
---|
[3908] | 271 | else if (!strncmp(buffer, "map_Ks ", 7)) |
---|
[3396] | 272 | { |
---|
[3908] | 273 | tmpMat->setSpecularMap(buffer+7); |
---|
[3396] | 274 | } |
---|
[3908] | 275 | else if (!strncmp(buffer, "bump ", 5)) |
---|
[3396] | 276 | { |
---|
[3908] | 277 | tmpMat->setBump(buffer+7); |
---|
[3396] | 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | } |
---|
[3909] | 282 | fclose(stream); |
---|
[3396] | 283 | delete []fileName; |
---|
| 284 | return true; |
---|
| 285 | } |
---|