| 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 |  | 
|---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER | 
|---|
| 18 |  | 
|---|
| 19 | #include "material.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "texture.h" | 
|---|
| 22 | #include "debug.h" | 
|---|
| 23 | #include "resource_manager.h" | 
|---|
| 24 | #include <stdlib.h> | 
|---|
| 25 | #include <string.h> | 
|---|
| 26 |  | 
|---|
| 27 | //! \todo check if we are in RESOURCE MANAGER-mode | 
|---|
| 28 | #include "resource_manager.h" | 
|---|
| 29 |  | 
|---|
| 30 | using namespace std; | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 |    \brief creates a Material. | 
|---|
| 34 |    \param mtlName Name of the Material to be added to the Material List | 
|---|
| 35 | */ | 
|---|
| 36 | Material::Material (const char* mtlName) | 
|---|
| 37 | { | 
|---|
| 38 |    PRINTF(4)("initializing new Material.\n"); | 
|---|
| 39 |   this->name = NULL; | 
|---|
| 40 |   this->setIllum(3); | 
|---|
| 41 |   this->setDiffuse(0,0,0); | 
|---|
| 42 |   this->setAmbient(0,0,0); | 
|---|
| 43 |   this->setSpecular(.5,.5,.5); | 
|---|
| 44 |   this->setShininess(2.0); | 
|---|
| 45 |   this->setTransparency(1.0); | 
|---|
| 46 |  | 
|---|
| 47 |   this->diffuseTexture = NULL; | 
|---|
| 48 |   this->ambientTexture = NULL; | 
|---|
| 49 |   this->specularTexture = NULL; | 
|---|
| 50 |  | 
|---|
| 51 |   this->setName(mtlName); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | /**  | 
|---|
| 55 |     \brief deletes a Material | 
|---|
| 56 | */ | 
|---|
| 57 | Material::~Material() | 
|---|
| 58 | { | 
|---|
| 59 |   PRINTF(4)("delete Material %s.\n", this->name); | 
|---|
| 60 |   if (this->name) | 
|---|
| 61 |     delete []this->name; | 
|---|
| 62 |   if (this->diffuseTexture) | 
|---|
| 63 |     ResourceManager::getInstance()->unload(this->diffuseTexture); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | /** | 
|---|
| 67 |    \brief sets the material with which the following Faces will be painted | 
|---|
| 68 | */ | 
|---|
| 69 | bool Material::select (void) | 
|---|
| 70 | { | 
|---|
| 71 |   // setting diffuse color | 
|---|
| 72 |   //  glColor3f (diffuse[0], diffuse[1], diffuse[2]); | 
|---|
| 73 |   glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse); | 
|---|
| 74 |  | 
|---|
| 75 |   // setting ambient color | 
|---|
| 76 |   glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); | 
|---|
| 77 |  | 
|---|
| 78 |   // setting up Sprecular | 
|---|
| 79 |   glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); | 
|---|
| 80 |  | 
|---|
| 81 |   // setting up Shininess | 
|---|
| 82 |   glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); | 
|---|
| 83 |    | 
|---|
| 84 |   // setting the transparency | 
|---|
| 85 |   if (this->transparency < 1.0) | 
|---|
| 86 |     { | 
|---|
| 87 |       glEnable(GL_BLEND); | 
|---|
| 88 |       glColor4f(1.0f, 1.0f, 1.0f, this->transparency); | 
|---|
| 89 |       glBlendFunc(GL_SRC_ALPHA, GL_ONE); | 
|---|
| 90 |     } | 
|---|
| 91 |   else | 
|---|
| 92 |     { | 
|---|
| 93 |       glDisable(GL_BLEND); | 
|---|
| 94 |       glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | 
|---|
| 95 |     } | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 |   // setting illumination Model  | 
|---|
| 99 |   if (this->illumModel == 1) //! \todo make this work, if no vertex-normals are read. | 
|---|
| 100 |     glShadeModel(GL_FLAT); | 
|---|
| 101 |   else if (this->illumModel >= 2) | 
|---|
| 102 |     glShadeModel(GL_SMOOTH); | 
|---|
| 103 |  | 
|---|
| 104 |   if (this->diffuseTexture) | 
|---|
| 105 |     { | 
|---|
| 106 |       glEnable(GL_TEXTURE_2D); | 
|---|
| 107 |       glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); | 
|---|
| 108 |  | 
|---|
| 109 |       /* This allows alpha blending of 2D textures with the scene */ | 
|---|
| 110 |       if (this->diffuseTexture->hasAlpha()) | 
|---|
| 111 |         { | 
|---|
| 112 |           glEnable(GL_BLEND); | 
|---|
| 113 |           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 114 |         } | 
|---|
| 115 |     } | 
|---|
| 116 |   else | 
|---|
| 117 |     { | 
|---|
| 118 |       glDisable(GL_TEXTURE_2D); | 
|---|
| 119 |       glBindTexture(GL_TEXTURE_2D, 0); | 
|---|
| 120 |     } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /** | 
|---|
| 124 |    \brief Set the Name of the Material. (Important for searching) | 
|---|
| 125 |    \param mtlName the Name of the Material to be set. | 
|---|
| 126 | */  | 
|---|
| 127 | void Material::setName (const char* mtlName) | 
|---|
| 128 | { | 
|---|
| 129 |   if (this->name) | 
|---|
| 130 |     delete this->name; | 
|---|
| 131 |   if (mtlName) | 
|---|
| 132 |     { | 
|---|
| 133 |       this->name = new char [strlen(mtlName)+1]; | 
|---|
| 134 |       strcpy(this->name, mtlName); | 
|---|
| 135 |     } | 
|---|
| 136 |   else | 
|---|
| 137 |     { | 
|---|
| 138 |       this->name = new char[2]; | 
|---|
| 139 |       strcpy(this->name, ""); | 
|---|
| 140 |     } | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /** | 
|---|
| 144 |    \returns The Name of The Material | 
|---|
| 145 | */ | 
|---|
| 146 | char* Material::getName (void) | 
|---|
| 147 | { | 
|---|
| 148 |   return this->name; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | /** | 
|---|
| 152 |    \brief Sets the Material Illumination Model.  | 
|---|
| 153 |    \brief illu illumination Model in int form | 
|---|
| 154 | */ | 
|---|
| 155 | void Material::setIllum (int illum) | 
|---|
| 156 | { | 
|---|
| 157 |   PRINTF(4)("setting illumModel of Material %s to %i\n", this->name, illum); | 
|---|
| 158 |   this->illumModel = illum; | 
|---|
| 159 | } | 
|---|
| 160 | /** | 
|---|
| 161 |    \brief Sets the Material Illumination Model.  | 
|---|
| 162 |    \brief illu illumination Model in char* form | 
|---|
| 163 | */void Material::setIllum (char* illum) | 
|---|
| 164 | { | 
|---|
| 165 |   this->setIllum (atoi(illum)); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | /** | 
|---|
| 169 |    \brief Sets the Material Diffuse Color. | 
|---|
| 170 |    \param r Red Color Channel. | 
|---|
| 171 |    \param g Green Color Channel. | 
|---|
| 172 |    \param b Blue Color Channel. | 
|---|
| 173 | */ | 
|---|
| 174 | void Material::setDiffuse (float r, float g, float b) | 
|---|
| 175 | { | 
|---|
| 176 |   PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); | 
|---|
| 177 |   this->diffuse[0] = r; | 
|---|
| 178 |   this->diffuse[1] = g; | 
|---|
| 179 |   this->diffuse[2] = b;   | 
|---|
| 180 |   this->diffuse[3] = 1.0; | 
|---|
| 181 |  | 
|---|
| 182 | } | 
|---|
| 183 | /** | 
|---|
| 184 |    \brief Sets the Material Diffuse Color. | 
|---|
| 185 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 186 | */ | 
|---|
| 187 | void Material::setDiffuse (char* rgb) | 
|---|
| 188 | { | 
|---|
| 189 |   float r,g,b; | 
|---|
| 190 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| 191 |   this->setDiffuse (r, g, b); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | /** | 
|---|
| 195 |    \brief Sets the Material Ambient Color.  | 
|---|
| 196 |    \param r Red Color Channel. | 
|---|
| 197 |    \param g Green Color Channel. | 
|---|
| 198 |    \param b Blue Color Channel. | 
|---|
| 199 | */ | 
|---|
| 200 | void Material::setAmbient (float r, float g, float b) | 
|---|
| 201 | { | 
|---|
| 202 |   PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); | 
|---|
| 203 |   this->ambient[0] = r; | 
|---|
| 204 |   this->ambient[1] = g; | 
|---|
| 205 |   this->ambient[2] = b; | 
|---|
| 206 |   this->ambient[3] = 1.0; | 
|---|
| 207 | } | 
|---|
| 208 | /** | 
|---|
| 209 |    \brief Sets the Material Ambient Color. | 
|---|
| 210 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 211 | */ | 
|---|
| 212 | void Material::setAmbient (char* rgb) | 
|---|
| 213 | { | 
|---|
| 214 |   float r,g,b; | 
|---|
| 215 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| 216 |   this->setAmbient (r, g, b); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | /** | 
|---|
| 220 |    \brief Sets the Material Specular Color.  | 
|---|
| 221 |    \param r Red Color Channel. | 
|---|
| 222 |    \param g Green Color Channel. | 
|---|
| 223 |    \param b Blue Color Channel. | 
|---|
| 224 | */ | 
|---|
| 225 | void Material::setSpecular (float r, float g, float b) | 
|---|
| 226 | { | 
|---|
| 227 |   PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); | 
|---|
| 228 |   this->specular[0] = r; | 
|---|
| 229 |   this->specular[1] = g; | 
|---|
| 230 |   this->specular[2] = b; | 
|---|
| 231 |   this->specular[3] = 1.0; | 
|---|
| 232 |  } | 
|---|
| 233 | /** | 
|---|
| 234 |    \brief Sets the Material Specular Color. | 
|---|
| 235 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 236 | */ | 
|---|
| 237 | void Material::setSpecular (char* rgb) | 
|---|
| 238 | { | 
|---|
| 239 |   float r,g,b; | 
|---|
| 240 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| 241 |   this->setSpecular (r, g, b); | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | /** | 
|---|
| 245 |    \brief Sets the Material Shininess. | 
|---|
| 246 |    \param shini stes the Shininess from float. | 
|---|
| 247 | */ | 
|---|
| 248 | void Material::setShininess (float shini) | 
|---|
| 249 | { | 
|---|
| 250 |   this->shininess = shini; | 
|---|
| 251 | } | 
|---|
| 252 | /** | 
|---|
| 253 |    \brief Sets the Material Shininess. | 
|---|
| 254 |    \param shini stes the Shininess from char*. | 
|---|
| 255 | */ | 
|---|
| 256 | void Material::setShininess (char* shini) | 
|---|
| 257 | { | 
|---|
| 258 |   this->setShininess (atof(shini)); | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | /** | 
|---|
| 262 |    \brief Sets the Material Transparency. | 
|---|
| 263 |    \param trans stes the Transparency from int. | 
|---|
| 264 | */ | 
|---|
| 265 | void Material::setTransparency (float trans) | 
|---|
| 266 | { | 
|---|
| 267 |   PRINTF(4)("setting Transparency of Material %s to %f.\n", this->name, trans); | 
|---|
| 268 |   this->transparency = trans; | 
|---|
| 269 | } | 
|---|
| 270 | /** | 
|---|
| 271 |    \brief Sets the Material Transparency. | 
|---|
| 272 |    \param trans stes the Transparency from char*. | 
|---|
| 273 | */ | 
|---|
| 274 | void Material::setTransparency (char* trans) | 
|---|
| 275 | { | 
|---|
| 276 |   this->setTransparency (atof(trans)); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | /** | 
|---|
| 280 |    \brief Adds a Texture Path to the List of already existing Paths | 
|---|
| 281 |    \param pathName The Path to add. | 
|---|
| 282 | */ | 
|---|
| 283 | void Material::addTexturePath(const char* pathName) | 
|---|
| 284 | { | 
|---|
| 285 |   ResourceManager::getInstance()->addImageDir(pathName); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | // MAPPING // | 
|---|
| 289 |  | 
|---|
| 290 | /** | 
|---|
| 291 |    \brief Sets the Materials Diffuse Map | 
|---|
| 292 |    \param dMap the Name of the Image to Use | 
|---|
| 293 | */ | 
|---|
| 294 | void Material::setDiffuseMap(const char* dMap) | 
|---|
| 295 | { | 
|---|
| 296 |   PRINTF(4)("setting Diffuse Map %s\n", dMap); | 
|---|
| 297 |  | 
|---|
| 298 |   //! \todo check if RESOURCE MANAGER is availiable | 
|---|
| 299 |   //! \todo Textures from .mtl-file need special care. | 
|---|
| 300 |   this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE); | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | /** | 
|---|
| 304 |    \brief Sets the Materials Ambient Map | 
|---|
| 305 |    \param aMap the Name of the Image to Use | 
|---|
| 306 |    \todo implement this | 
|---|
| 307 | */ | 
|---|
| 308 | void Material::setAmbientMap(const char* aMap) | 
|---|
| 309 | { | 
|---|
| 310 |   SDL_Surface* ambientMap; | 
|---|
| 311 |  | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | /** | 
|---|
| 315 |    \brief Sets the Materials Specular Map | 
|---|
| 316 |    \param sMap the Name of the Image to Use | 
|---|
| 317 |    \todo implement this | 
|---|
| 318 | */ | 
|---|
| 319 | void Material::setSpecularMap(const char* sMap) | 
|---|
| 320 | { | 
|---|
| 321 |   SDL_Surface* specularMap; | 
|---|
| 322 |  | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | /** | 
|---|
| 326 |    \brief Sets the Materials Bumpiness | 
|---|
| 327 |    \param bump the Name of the Image to Use | 
|---|
| 328 |    \todo implemet this | 
|---|
| 329 | */ | 
|---|
| 330 | void Material::setBump(const char* bump) | 
|---|
| 331 | { | 
|---|
| 332 |  | 
|---|
| 333 | } | 
|---|