[5014] | 1 | /* |
---|
[3396] | 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 <string.h> |
---|
[9869] | 21 | #include "loading/resource_manager.h" |
---|
[3908] | 22 | |
---|
| 23 | #define PARSELINELENGTH 8192 |
---|
| 24 | |
---|
[3475] | 25 | #include "debug.h" |
---|
[3915] | 26 | #include "compiler.h" |
---|
[3427] | 27 | |
---|
[9869] | 28 | ObjectListDefinition(OBJModel); |
---|
[3396] | 29 | /** |
---|
[8369] | 30 | * @brief Crates a 3D-Model, loads in a File and scales it. |
---|
[4836] | 31 | * @param fileName file to parse and load (must be a .obj file) |
---|
| 32 | * @param scaling The factor that the model will be scaled with. |
---|
[3396] | 33 | */ |
---|
[7221] | 34 | OBJModel::OBJModel(const std::string& fileName, float scaling) |
---|
| 35 | : StaticModel(fileName) |
---|
[3396] | 36 | { |
---|
[9869] | 37 | this->registerObject(this, OBJModel::_objectList); |
---|
[6162] | 38 | |
---|
[4117] | 39 | this->objPath = "./"; |
---|
| 40 | |
---|
[9869] | 41 | this->setScaleFactor(scaling); |
---|
[3396] | 42 | |
---|
| 43 | this->importFile (fileName); |
---|
| 44 | |
---|
[3911] | 45 | this->finalize(); |
---|
[10090] | 46 | |
---|
| 47 | this->extractMountPoints(); |
---|
[3396] | 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
[8369] | 51 | * @brief deletes an OBJModel. |
---|
| 52 | */ |
---|
[3396] | 53 | OBJModel::~OBJModel() |
---|
[7221] | 54 | { } |
---|
[3396] | 55 | |
---|
| 56 | /** |
---|
[4836] | 57 | * Imports a obj file and handles the the relative location |
---|
| 58 | * @param fileName The file to import |
---|
[4117] | 59 | |
---|
| 60 | Splits the FileName from the DirectoryName |
---|
[3396] | 61 | */ |
---|
[7221] | 62 | bool OBJModel::importFile (const std::string& fileNameInput) |
---|
[3396] | 63 | { |
---|
[7221] | 64 | const char* fileName = fileNameInput.c_str(); |
---|
[3548] | 65 | PRINTF(4)("preparing to read in file: %s\n", fileName); |
---|
[5014] | 66 | // splitting the |
---|
[4117] | 67 | char* split = NULL; |
---|
[3396] | 68 | |
---|
[4117] | 69 | if (!(split = strrchr(fileName, '/'))) |
---|
| 70 | split = strrchr(fileName, '\\'); // windows Case |
---|
| 71 | if (split) |
---|
[3396] | 72 | { |
---|
[4117] | 73 | int len = split - fileName+1; |
---|
[7221] | 74 | this->objPath = fileName; |
---|
| 75 | this->objPath.erase(len, this->objPath.size()); |
---|
[4118] | 76 | this->objPath[len] = '\0'; |
---|
[7677] | 77 | PRINTF(4)("Resolved file %s to Path %s.\n", fileName, this->objPath.c_str()); |
---|
[3396] | 78 | } |
---|
[4371] | 79 | else |
---|
| 80 | this->objPath = "./"; |
---|
[9869] | 81 | Resources::ResourceManager::getInstance()->addResourcePath("Texture", this->objPath); |
---|
[4371] | 82 | |
---|
[4117] | 83 | this->readFromObjFile (fileName); |
---|
[3396] | 84 | return true; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
[8369] | 88 | * @brief Reads in the .obj File and sets all the Values. |
---|
| 89 | * @param fileName the FileName of the Model. |
---|
| 90 | * |
---|
| 91 | * This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks |
---|
| 92 | */ |
---|
[7221] | 93 | bool OBJModel::readFromObjFile(const std::string& fileName) |
---|
[3396] | 94 | { |
---|
[3908] | 95 | FILE* stream; |
---|
[7221] | 96 | if( (stream = fopen (fileName.c_str(), "r")) == NULL) |
---|
[3396] | 97 | { |
---|
[7221] | 98 | PRINTF(2)("Object File Could not be Opened %s\n", fileName.c_str()); |
---|
[3396] | 99 | return false; |
---|
| 100 | } |
---|
| 101 | |
---|
[3908] | 102 | char buffer[PARSELINELENGTH]; |
---|
[3910] | 103 | while(fgets(buffer, PARSELINELENGTH, stream)) |
---|
| 104 | { |
---|
| 105 | // line termiated with \0 not \n |
---|
| 106 | if (buffer[strlen(buffer)-1] == '\n') |
---|
[5014] | 107 | buffer[strlen(buffer)-1] = '\0'; |
---|
[3910] | 108 | |
---|
[3396] | 109 | // case vertice |
---|
[3908] | 110 | if (!strncmp(buffer, "v ", 2)) |
---|
[5014] | 111 | { |
---|
| 112 | this->addVertex(buffer+2); |
---|
| 113 | } |
---|
[3396] | 114 | |
---|
| 115 | // case face |
---|
[3908] | 116 | else if (!strncmp(buffer, "f ", 2)) |
---|
[5014] | 117 | { |
---|
| 118 | this->addFace (buffer+2); |
---|
| 119 | } |
---|
| 120 | |
---|
[3908] | 121 | else if (!strncmp(buffer, "mtllib ", 7)) |
---|
[5014] | 122 | { |
---|
| 123 | this->readMtlLib (buffer+7); |
---|
| 124 | } |
---|
[3396] | 125 | |
---|
[3908] | 126 | else if (!strncmp(buffer, "usemtl ", 7)) |
---|
[5014] | 127 | { |
---|
| 128 | this->setMaterial (buffer+7); |
---|
| 129 | } |
---|
[3396] | 130 | |
---|
| 131 | // case VertexNormal |
---|
[3908] | 132 | else if (!strncmp(buffer, "vn ", 3)) |
---|
[5014] | 133 | { |
---|
| 134 | this->addVertexNormal(buffer+3); |
---|
| 135 | } |
---|
| 136 | |
---|
[3396] | 137 | // case VertexTextureCoordinate |
---|
[3908] | 138 | else if (!strncmp(buffer, "vt ", 3)) |
---|
[5014] | 139 | { |
---|
| 140 | this->addVertexTexture(buffer+3); |
---|
| 141 | } |
---|
[10070] | 142 | // case group or object (depends on the exporter program) |
---|
| 143 | else if (!strncmp(buffer, "g ", 2) || !strncmp(buffer, "o ", 2)) |
---|
[5014] | 144 | { |
---|
| 145 | this->addGroup (buffer+2); |
---|
| 146 | } |
---|
[4836] | 147 | else if (!strncmp(buffer, "s ", 2)) //! @todo smoothing groups have to be implemented |
---|
[5014] | 148 | { |
---|
[5300] | 149 | PRINTF(3)("smoothing groups not supportet yet. line: %s\n", buffer); |
---|
[5014] | 150 | } |
---|
[3396] | 151 | } |
---|
[3908] | 152 | fclose (stream); |
---|
[3396] | 153 | return true; |
---|
| 154 | } |
---|
| 155 | |
---|
[5014] | 156 | /** |
---|
[8369] | 157 | * @brief Function to read in a mtl File. |
---|
[5300] | 158 | * @param mtlFile The .mtl file to read |
---|
| 159 | * |
---|
| 160 | * This Function parses all Lines of an mtl File. |
---|
| 161 | * The reason for it not to be in the materials-class is, |
---|
| 162 | * that a material does not have to be able to read itself in from a File. |
---|
| 163 | */ |
---|
[7221] | 164 | bool OBJModel::readMtlLib (const std::string& mtlFile) |
---|
[3396] | 165 | { |
---|
[7221] | 166 | std::string fileName = this->objPath + mtlFile; |
---|
[3396] | 167 | |
---|
[3909] | 168 | FILE* stream; |
---|
[7221] | 169 | if( (stream = fopen (fileName.c_str(), "r")) == NULL) |
---|
[3396] | 170 | { |
---|
[7221] | 171 | PRINTF(2)("MaterialLibrary could not be opened %s\n", fileName.c_str()); |
---|
[3396] | 172 | return false; |
---|
| 173 | } |
---|
[3909] | 174 | |
---|
| 175 | char buffer[PARSELINELENGTH]; |
---|
[3915] | 176 | Material* tmpMat = NULL; |
---|
[3911] | 177 | |
---|
[5319] | 178 | while(fgets(buffer, PARSELINELENGTH, stream) != NULL) |
---|
[3396] | 179 | { |
---|
[3908] | 180 | PRINTF(5)("found line in mtlFile: %s\n", buffer); |
---|
[3396] | 181 | |
---|
[3910] | 182 | // line termiated with \0 not \n |
---|
| 183 | if (buffer[strlen(buffer)-1] == '\n') |
---|
[5014] | 184 | buffer[strlen(buffer)-1] = '\0'; |
---|
[3910] | 185 | |
---|
[3396] | 186 | // create new Material |
---|
[3908] | 187 | if (!strncmp(buffer, "newmtl ", 7)) |
---|
[5014] | 188 | { |
---|
| 189 | tmpMat = this->addMaterial(buffer+7);//tmpMat->addMaterial(buffer+7); |
---|
| 190 | } |
---|
[3396] | 191 | // setting a illumMode |
---|
[3908] | 192 | else if (!strncmp(buffer, "illum ", 6)) |
---|
[5014] | 193 | { |
---|
| 194 | if (likely(tmpMat != NULL)) |
---|
[8369] | 195 | setIllum(tmpMat, buffer+6); |
---|
[3396] | 196 | |
---|
[5014] | 197 | } |
---|
[3396] | 198 | // setting Diffuse Color |
---|
[3908] | 199 | else if (!strncmp(buffer, "Kd ", 3)) |
---|
[5014] | 200 | { |
---|
| 201 | if (likely(tmpMat != NULL)) |
---|
[8369] | 202 | setDiffuse(tmpMat, buffer+3); |
---|
[5014] | 203 | } |
---|
[3396] | 204 | // setting Ambient Color |
---|
[3908] | 205 | else if (!strncmp(buffer, "Ka ", 3)) |
---|
[5014] | 206 | { |
---|
| 207 | if (likely(tmpMat != NULL)) |
---|
[8369] | 208 | setAmbient(tmpMat, buffer+3); |
---|
[5014] | 209 | } |
---|
[3396] | 210 | // setting Specular Color |
---|
[3908] | 211 | else if (!strncmp(buffer, "Ks ", 3)) |
---|
[5014] | 212 | { |
---|
| 213 | if (likely(tmpMat != NULL)) |
---|
[8369] | 214 | setSpecular(tmpMat, buffer+3); |
---|
[5014] | 215 | } |
---|
[3396] | 216 | // setting The Specular Shininess |
---|
[3908] | 217 | else if (!strncmp(buffer, "Ns ", 3)) |
---|
[5014] | 218 | { |
---|
| 219 | if (likely(tmpMat != NULL)) |
---|
[8369] | 220 | setShininess(tmpMat, buffer+3); |
---|
[5014] | 221 | } |
---|
[3396] | 222 | // setting up transparency |
---|
[3908] | 223 | else if (!strncmp(buffer, "d ", 2)) |
---|
[5014] | 224 | { |
---|
| 225 | if (likely(tmpMat != NULL)) |
---|
[8369] | 226 | setTransparency(tmpMat, buffer+2); |
---|
[5014] | 227 | } |
---|
[3908] | 228 | else if (!strncmp(buffer, "Tf ", 3)) |
---|
[5014] | 229 | { |
---|
| 230 | if (likely(tmpMat != NULL)) |
---|
[8369] | 231 | setTransparency(tmpMat, buffer+3); |
---|
[5014] | 232 | } |
---|
| 233 | |
---|
[3908] | 234 | else if (!strncmp(buffer, "map_Kd ", 7)) |
---|
[5014] | 235 | { |
---|
| 236 | if (likely(tmpMat != NULL)) |
---|
| 237 | tmpMat->setDiffuseMap(buffer+7); |
---|
| 238 | } |
---|
[3908] | 239 | else if (!strncmp(buffer, "map_Ka ", 7)) |
---|
[5014] | 240 | { |
---|
| 241 | if (likely(tmpMat != NULL)) |
---|
| 242 | tmpMat->setAmbientMap(buffer+7); |
---|
| 243 | } |
---|
[3908] | 244 | else if (!strncmp(buffer, "map_Ks ", 7)) |
---|
[5014] | 245 | { |
---|
| 246 | if (likely(tmpMat != NULL)) |
---|
| 247 | tmpMat->setSpecularMap(buffer+7); |
---|
| 248 | } |
---|
[3908] | 249 | else if (!strncmp(buffer, "bump ", 5)) |
---|
[5014] | 250 | { |
---|
| 251 | if (likely(tmpMat != NULL)) |
---|
| 252 | tmpMat->setBump(buffer+7); |
---|
| 253 | } |
---|
[3396] | 254 | |
---|
[5014] | 255 | |
---|
[3396] | 256 | } |
---|
[3909] | 257 | fclose(stream); |
---|
[3396] | 258 | return true; |
---|
| 259 | } |
---|
[8369] | 260 | |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * @brief Sets the Material Illumination Model. |
---|
| 264 | * @param material: the Material to apply the change to. |
---|
| 265 | * @param illu: illumination Model in char* form |
---|
| 266 | */ |
---|
| 267 | void OBJModel::setIllum (Material* material, const char* illum) |
---|
| 268 | { |
---|
| 269 | material->setIllum (atoi(illum)); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | /** |
---|
| 273 | * @brief Sets the Material Diffuse Color. |
---|
| 274 | * @param material: the Material to apply the change to. |
---|
| 275 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 276 | */ |
---|
| 277 | void OBJModel::setDiffuse (Material* material, const char* rgb) |
---|
| 278 | { |
---|
| 279 | float r,g,b; |
---|
| 280 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
| 281 | material->setDiffuse (r, g, b); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /** |
---|
| 285 | * @brief Sets the Material Ambient Color. |
---|
| 286 | * @param material: the Material to apply the change to. |
---|
| 287 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 288 | */ |
---|
| 289 | void OBJModel::setAmbient (Material* material, const char* rgb) |
---|
| 290 | { |
---|
| 291 | float r,g,b; |
---|
| 292 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
| 293 | material->setAmbient (r, g, b); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | /** |
---|
| 297 | * @brief Sets the Material Specular Color. |
---|
| 298 | * @param material: the Material to apply the change to. |
---|
| 299 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 300 | */ |
---|
| 301 | void OBJModel::setSpecular (Material* material, const char* rgb) |
---|
| 302 | { |
---|
| 303 | float r,g,b; |
---|
| 304 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
| 305 | material->setSpecular (r, g, b); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | /** |
---|
| 309 | * @brief Sets the Material Shininess. |
---|
| 310 | * @param material: the Material to apply the change to. |
---|
| 311 | * @param shini stes the Shininess from char*. |
---|
| 312 | */ |
---|
| 313 | void OBJModel::setShininess (Material* material, const char* shini) |
---|
| 314 | { |
---|
| 315 | material->setShininess (atof(shini)); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | * @brief Sets the Material Transparency. |
---|
| 320 | * @param material: the Material to apply the change to. |
---|
| 321 | * @param trans stes the Transparency from char*. |
---|
| 322 | */ |
---|
| 323 | void OBJModel::setTransparency (Material* material, const char* trans) |
---|
| 324 | { |
---|
| 325 | material->setTransparency (atof(trans)); |
---|
| 326 | } |
---|