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