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