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