[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" |
---|
[3915] | 27 | #include "compiler.h" |
---|
[3427] | 28 | |
---|
[3396] | 29 | /** |
---|
| 30 | \brief Crates a 3D-Model, loads in a File and scales it. |
---|
| 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. |
---|
| 33 | */ |
---|
[3916] | 34 | OBJModel::OBJModel(const char* fileName, float scaling) : Model(fileName) |
---|
[3396] | 35 | { |
---|
[4117] | 36 | this->objPath = "./"; |
---|
| 37 | |
---|
[3396] | 38 | this->scaleFactor = scaling; |
---|
| 39 | |
---|
| 40 | this->importFile (fileName); |
---|
| 41 | |
---|
[3911] | 42 | this->finalize(); |
---|
[3396] | 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 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | \brief Imports a obj file and handles the the relative location |
---|
| 59 | \param fileName The file to import |
---|
[4117] | 60 | |
---|
| 61 | Splits the FileName from the DirectoryName |
---|
[3396] | 62 | */ |
---|
[3916] | 63 | bool OBJModel::importFile (const char* fileName) |
---|
[3396] | 64 | { |
---|
[3548] | 65 | PRINTF(4)("preparing to read in file: %s\n", fileName); |
---|
[4117] | 66 | // splitting the |
---|
| 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; |
---|
| 74 | this->objPath = new char[len +2]; |
---|
| 75 | strncpy(this->objPath, fileName, len); |
---|
[4118] | 76 | this->objPath[len] = '\0'; |
---|
| 77 | PRINTF(1)("Resolved file %s to Path %s.\n", fileName, this->objPath); |
---|
[3396] | 78 | } |
---|
[4371] | 79 | else |
---|
| 80 | this->objPath = "./"; |
---|
| 81 | Material::addTexturePath(this->objPath); |
---|
| 82 | |
---|
[4117] | 83 | this->readFromObjFile (fileName); |
---|
[3396] | 84 | return true; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | \brief Reads in the .obj File and sets all the Values. |
---|
| 89 | This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks |
---|
| 90 | */ |
---|
[4117] | 91 | bool OBJModel::readFromObjFile(const char* fileName) |
---|
[3396] | 92 | { |
---|
[3908] | 93 | FILE* stream; |
---|
| 94 | if( (stream = fopen (fileName, "r")) == NULL) |
---|
[3396] | 95 | { |
---|
[4117] | 96 | printf("Object File Could not be Opened %s\n", fileName); |
---|
[3396] | 97 | return false; |
---|
| 98 | } |
---|
| 99 | |
---|
[3908] | 100 | char buffer[PARSELINELENGTH]; |
---|
[3910] | 101 | while(fgets(buffer, PARSELINELENGTH, stream)) |
---|
| 102 | { |
---|
| 103 | // line termiated with \0 not \n |
---|
| 104 | if (buffer[strlen(buffer)-1] == '\n') |
---|
| 105 | buffer[strlen(buffer)-1] = '\0'; |
---|
| 106 | |
---|
[3396] | 107 | // case vertice |
---|
[3908] | 108 | if (!strncmp(buffer, "v ", 2)) |
---|
[3396] | 109 | { |
---|
[3908] | 110 | this->addVertex(buffer+2); |
---|
[3396] | 111 | } |
---|
| 112 | |
---|
| 113 | // case face |
---|
[3908] | 114 | else if (!strncmp(buffer, "f ", 2)) |
---|
[3396] | 115 | { |
---|
[3908] | 116 | this->addFace (buffer+2); |
---|
[3396] | 117 | } |
---|
| 118 | |
---|
[3908] | 119 | else if (!strncmp(buffer, "mtllib ", 7)) |
---|
[3396] | 120 | { |
---|
[3908] | 121 | this->readMtlLib (buffer+7); |
---|
[3396] | 122 | } |
---|
| 123 | |
---|
[3908] | 124 | else if (!strncmp(buffer, "usemtl ", 7)) |
---|
[3396] | 125 | { |
---|
[3913] | 126 | this->setMaterial (buffer+7); |
---|
[3396] | 127 | } |
---|
| 128 | |
---|
| 129 | // case VertexNormal |
---|
[3908] | 130 | else if (!strncmp(buffer, "vn ", 3)) |
---|
[3396] | 131 | { |
---|
[3908] | 132 | this->addVertexNormal(buffer+3); |
---|
[3396] | 133 | } |
---|
| 134 | |
---|
| 135 | // case VertexTextureCoordinate |
---|
[3908] | 136 | else if (!strncmp(buffer, "vt ", 3)) |
---|
[3396] | 137 | { |
---|
[3908] | 138 | this->addVertexTexture(buffer+3); |
---|
[3396] | 139 | } |
---|
| 140 | // case group |
---|
[3908] | 141 | else if (!strncmp(buffer, "g ", 2)) |
---|
[3396] | 142 | { |
---|
[3908] | 143 | this->addGroup (buffer+2); |
---|
[3396] | 144 | } |
---|
[3908] | 145 | else if (!strncmp(buffer, "s ", 2)) //! \todo smoothing groups have to be implemented |
---|
[3396] | 146 | { |
---|
[3908] | 147 | PRINTF(2)("smoothing groups not supportet yet. line: %s\n", buffer); |
---|
[3396] | 148 | } |
---|
| 149 | } |
---|
[3908] | 150 | fclose (stream); |
---|
[3396] | 151 | return true; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | \brief Function to read in a mtl File. |
---|
| 156 | \param mtlFile The .mtl file to read |
---|
| 157 | |
---|
| 158 | This Function parses all Lines of an mtl File. |
---|
| 159 | The reason for it not to be in the materials-class is, |
---|
| 160 | that a material does not have to be able to read itself in from a File. |
---|
| 161 | |
---|
| 162 | */ |
---|
[3916] | 163 | bool OBJModel::readMtlLib (const char* mtlFile) |
---|
[3396] | 164 | { |
---|
[4117] | 165 | char* fileName = new char [strlen(this->objPath) + strlen(mtlFile)+1]; |
---|
| 166 | sprintf(fileName, "%s%s", this->objPath, mtlFile); |
---|
[3396] | 167 | |
---|
[3909] | 168 | FILE* stream; |
---|
| 169 | if( (stream = fopen (fileName, "r")) == NULL) |
---|
[3396] | 170 | { |
---|
[4117] | 171 | PRINTF(2)("MaterialLibrary could not be opened %s\n", fileName); |
---|
| 172 | delete []fileName; |
---|
[3396] | 173 | return false; |
---|
| 174 | } |
---|
[3909] | 175 | |
---|
| 176 | char buffer[PARSELINELENGTH]; |
---|
[3915] | 177 | Material* tmpMat = NULL; |
---|
[3911] | 178 | |
---|
[3910] | 179 | while(fgets(buffer, PARSELINELENGTH, stream)) |
---|
[3396] | 180 | { |
---|
[3908] | 181 | PRINTF(5)("found line in mtlFile: %s\n", buffer); |
---|
[3396] | 182 | |
---|
[3910] | 183 | // line termiated with \0 not \n |
---|
| 184 | if (buffer[strlen(buffer)-1] == '\n') |
---|
| 185 | buffer[strlen(buffer)-1] = '\0'; |
---|
| 186 | |
---|
[3396] | 187 | // create new Material |
---|
[3908] | 188 | if (!strncmp(buffer, "newmtl ", 7)) |
---|
[3396] | 189 | { |
---|
[3914] | 190 | tmpMat = this->addMaterial(buffer+7);//tmpMat->addMaterial(buffer+7); |
---|
[3396] | 191 | } |
---|
| 192 | // setting a illumMode |
---|
[3908] | 193 | else if (!strncmp(buffer, "illum ", 6)) |
---|
[3396] | 194 | { |
---|
[3915] | 195 | if (likely(tmpMat != NULL)) |
---|
| 196 | tmpMat->setIllum(buffer+6); |
---|
[3396] | 197 | |
---|
| 198 | } |
---|
| 199 | // setting Diffuse Color |
---|
[3908] | 200 | else if (!strncmp(buffer, "Kd ", 3)) |
---|
[3396] | 201 | { |
---|
[3915] | 202 | if (likely(tmpMat != NULL)) |
---|
| 203 | tmpMat->setDiffuse(buffer+3); |
---|
[3396] | 204 | } |
---|
| 205 | // setting Ambient Color |
---|
[3908] | 206 | else if (!strncmp(buffer, "Ka ", 3)) |
---|
[3396] | 207 | { |
---|
[3915] | 208 | if (likely(tmpMat != NULL)) |
---|
| 209 | tmpMat->setAmbient(buffer+3); |
---|
[3396] | 210 | } |
---|
| 211 | // setting Specular Color |
---|
[3908] | 212 | else if (!strncmp(buffer, "Ks ", 3)) |
---|
[3396] | 213 | { |
---|
[3915] | 214 | if (likely(tmpMat != NULL)) |
---|
| 215 | tmpMat->setSpecular(buffer+3); |
---|
[3396] | 216 | } |
---|
| 217 | // setting The Specular Shininess |
---|
[3908] | 218 | else if (!strncmp(buffer, "Ns ", 3)) |
---|
[3396] | 219 | { |
---|
[3915] | 220 | if (likely(tmpMat != NULL)) |
---|
| 221 | tmpMat->setShininess(buffer+3); |
---|
[3396] | 222 | } |
---|
| 223 | // setting up transparency |
---|
[3908] | 224 | else if (!strncmp(buffer, "d ", 2)) |
---|
[3396] | 225 | { |
---|
[3915] | 226 | if (likely(tmpMat != NULL)) |
---|
| 227 | tmpMat->setTransparency(buffer+2); |
---|
[3396] | 228 | } |
---|
[3908] | 229 | else if (!strncmp(buffer, "Tf ", 3)) |
---|
[3396] | 230 | { |
---|
[3915] | 231 | if (likely(tmpMat != NULL)) |
---|
| 232 | tmpMat->setTransparency(buffer+3); |
---|
[3396] | 233 | } |
---|
| 234 | |
---|
[3908] | 235 | else if (!strncmp(buffer, "map_Kd ", 7)) |
---|
[3396] | 236 | { |
---|
[3915] | 237 | if (likely(tmpMat != NULL)) |
---|
| 238 | tmpMat->setDiffuseMap(buffer+7); |
---|
[3396] | 239 | } |
---|
[3908] | 240 | else if (!strncmp(buffer, "map_Ka ", 7)) |
---|
[3396] | 241 | { |
---|
[3915] | 242 | if (likely(tmpMat != NULL)) |
---|
| 243 | tmpMat->setAmbientMap(buffer+7); |
---|
[3396] | 244 | } |
---|
[3908] | 245 | else if (!strncmp(buffer, "map_Ks ", 7)) |
---|
[3396] | 246 | { |
---|
[3915] | 247 | if (likely(tmpMat != NULL)) |
---|
| 248 | tmpMat->setSpecularMap(buffer+7); |
---|
[3396] | 249 | } |
---|
[3908] | 250 | else if (!strncmp(buffer, "bump ", 5)) |
---|
[3396] | 251 | { |
---|
[3915] | 252 | if (likely(tmpMat != NULL)) |
---|
| 253 | tmpMat->setBump(buffer+7); |
---|
[3396] | 254 | } |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | } |
---|
[3909] | 258 | fclose(stream); |
---|
[3396] | 259 | delete []fileName; |
---|
| 260 | return true; |
---|
| 261 | } |
---|