| [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 | 
|---|
| [6812] | 110 |   if (this->transparency < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */ | 
|---|
 | 111 |       (this->diffuseTexture && this->diffuseTexture->hasAlpha())) | 
|---|
 | 112 |   { | 
|---|
 | 113 |     glEnable(GL_BLEND); | 
|---|
 | 114 |     glBlendFunc(GL_SRC_ALPHA, GL_ONE/*_MINUS_SRC_ALPHA*/); | 
|---|
 | 115 |   } | 
|---|
| [3966] | 116 |   else | 
|---|
 | 117 |     { | 
|---|
 | 118 |       glDisable(GL_BLEND); | 
|---|
| [6799] | 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()); | 
|---|
 | 133 |     } | 
|---|
| [3140] | 134 |   else | 
|---|
| [3536] | 135 |     { | 
|---|
 | 136 |       glDisable(GL_TEXTURE_2D); | 
|---|
 | 137 |       glBindTexture(GL_TEXTURE_2D, 0); | 
|---|
 | 138 |     } | 
|---|
| [3140] | 139 | } | 
|---|
 | 140 |  | 
|---|
 | 141 | /** | 
|---|
| [4836] | 142 |  *  Sets the Material Illumination Model. | 
|---|
 | 143 |  *  illu illumination Model in int form | 
|---|
| [5308] | 144 |  */ | 
|---|
| [2776] | 145 | void Material::setIllum (int illum) | 
|---|
 | 146 | { | 
|---|
| [4584] | 147 |   PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum); | 
|---|
| [3195] | 148 |   this->illumModel = illum; | 
|---|
| [2776] | 149 | } | 
|---|
| [5308] | 150 |  | 
|---|
| [2842] | 151 | /** | 
|---|
| [4836] | 152 |  *  Sets the Material Illumination Model. | 
|---|
 | 153 |  *  illu illumination Model in char* form | 
|---|
| [5308] | 154 |  */ | 
|---|
 | 155 | void Material::setIllum (char* illum) | 
|---|
| [2776] | 156 | { | 
|---|
| [3195] | 157 |   this->setIllum (atoi(illum)); | 
|---|
| [2776] | 158 | } | 
|---|
 | 159 |  | 
|---|
| [2842] | 160 | /** | 
|---|
| [4836] | 161 |  *  Sets the Material Diffuse Color. | 
|---|
 | 162 |  * @param r Red Color Channel. | 
|---|
 | 163 |  * @param g Green Color Channel. | 
|---|
 | 164 |  * @param b Blue Color Channel. | 
|---|
| [5308] | 165 |  */ | 
|---|
| [2776] | 166 | void Material::setDiffuse (float r, float g, float b) | 
|---|
 | 167 | { | 
|---|
| [4584] | 168 |   PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); | 
|---|
| [3195] | 169 |   this->diffuse[0] = r; | 
|---|
 | 170 |   this->diffuse[1] = g; | 
|---|
| [4584] | 171 |   this->diffuse[2] = b; | 
|---|
| [3195] | 172 |   this->diffuse[3] = 1.0; | 
|---|
| [2780] | 173 |  | 
|---|
| [2776] | 174 | } | 
|---|
| [5308] | 175 |  | 
|---|
| [2842] | 176 | /** | 
|---|
| [4836] | 177 |  *  Sets the Material Diffuse Color. | 
|---|
 | 178 |  * @param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| [5308] | 179 |  */ | 
|---|
| [2776] | 180 | void Material::setDiffuse (char* rgb) | 
|---|
 | 181 | { | 
|---|
| [3140] | 182 |   float r,g,b; | 
|---|
 | 183 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| [3195] | 184 |   this->setDiffuse (r, g, b); | 
|---|
| [2776] | 185 | } | 
|---|
 | 186 |  | 
|---|
| [2842] | 187 | /** | 
|---|
| [4836] | 188 |  *  Sets the Material Ambient Color. | 
|---|
 | 189 |  * @param r Red Color Channel. | 
|---|
 | 190 |  * @param g Green Color Channel. | 
|---|
 | 191 |  * @param b Blue Color Channel. | 
|---|
| [2842] | 192 | */ | 
|---|
| [2776] | 193 | void Material::setAmbient (float r, float g, float b) | 
|---|
 | 194 | { | 
|---|
| [4584] | 195 |   PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); | 
|---|
| [3195] | 196 |   this->ambient[0] = r; | 
|---|
 | 197 |   this->ambient[1] = g; | 
|---|
 | 198 |   this->ambient[2] = b; | 
|---|
 | 199 |   this->ambient[3] = 1.0; | 
|---|
| [2776] | 200 | } | 
|---|
| [5308] | 201 |  | 
|---|
| [2842] | 202 | /** | 
|---|
| [4836] | 203 |  *  Sets the Material Ambient Color. | 
|---|
 | 204 |  * @param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| [5308] | 205 |  */ | 
|---|
| [2776] | 206 | void Material::setAmbient (char* rgb) | 
|---|
 | 207 | { | 
|---|
| [3140] | 208 |   float r,g,b; | 
|---|
 | 209 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| [3195] | 210 |   this->setAmbient (r, g, b); | 
|---|
| [2776] | 211 | } | 
|---|
 | 212 |  | 
|---|
| [2842] | 213 | /** | 
|---|
| [4836] | 214 |  *  Sets the Material Specular Color. | 
|---|
 | 215 |  * @param r Red Color Channel. | 
|---|
 | 216 |  * @param g Green Color Channel. | 
|---|
 | 217 |  * @param b Blue Color Channel. | 
|---|
| [5308] | 218 |  */ | 
|---|
| [2776] | 219 | void Material::setSpecular (float r, float g, float b) | 
|---|
 | 220 | { | 
|---|
| [4584] | 221 |   PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); | 
|---|
| [3195] | 222 |   this->specular[0] = r; | 
|---|
 | 223 |   this->specular[1] = g; | 
|---|
 | 224 |   this->specular[2] = b; | 
|---|
 | 225 |   this->specular[3] = 1.0; | 
|---|
| [2804] | 226 |  } | 
|---|
| [5308] | 227 |  | 
|---|
| [2842] | 228 | /** | 
|---|
| [4836] | 229 |  *  Sets the Material Specular Color. | 
|---|
 | 230 |  * @param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| [2842] | 231 | */ | 
|---|
| [2776] | 232 | void Material::setSpecular (char* rgb) | 
|---|
 | 233 | { | 
|---|
| [3140] | 234 |   float r,g,b; | 
|---|
 | 235 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| [3195] | 236 |   this->setSpecular (r, g, b); | 
|---|
| [2776] | 237 | } | 
|---|
 | 238 |  | 
|---|
| [2842] | 239 | /** | 
|---|
| [4836] | 240 |  *  Sets the Material Shininess. | 
|---|
 | 241 |  * @param shini stes the Shininess from float. | 
|---|
| [2842] | 242 | */ | 
|---|
| [2836] | 243 | void Material::setShininess (float shini) | 
|---|
 | 244 | { | 
|---|
| [3195] | 245 |   this->shininess = shini; | 
|---|
| [2836] | 246 | } | 
|---|
| [2842] | 247 | /** | 
|---|
| [4836] | 248 |  *  Sets the Material Shininess. | 
|---|
 | 249 |  * @param shini stes the Shininess from char*. | 
|---|
| [2842] | 250 | */ | 
|---|
| [2836] | 251 | void Material::setShininess (char* shini) | 
|---|
 | 252 | { | 
|---|
| [3195] | 253 |   this->setShininess (atof(shini)); | 
|---|
| [2836] | 254 | } | 
|---|
| [2776] | 255 |  | 
|---|
| [2842] | 256 | /** | 
|---|
| [4836] | 257 |  *  Sets the Material Transparency. | 
|---|
 | 258 |  * @param trans stes the Transparency from int. | 
|---|
| [2842] | 259 | */ | 
|---|
| [2776] | 260 | void Material::setTransparency (float trans) | 
|---|
 | 261 | { | 
|---|
| [4584] | 262 |   PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans); | 
|---|
| [3195] | 263 |   this->transparency = trans; | 
|---|
| [2776] | 264 | } | 
|---|
| [2842] | 265 | /** | 
|---|
| [4836] | 266 |  *  Sets the Material Transparency. | 
|---|
 | 267 |  * @param trans stes the Transparency from char*. | 
|---|
| [2842] | 268 | */ | 
|---|
| [2776] | 269 | void Material::setTransparency (char* trans) | 
|---|
 | 270 | { | 
|---|
| [3195] | 271 |   this->setTransparency (atof(trans)); | 
|---|
| [2776] | 272 | } | 
|---|
| [2778] | 273 |  | 
|---|
| [3140] | 274 | /** | 
|---|
| [4836] | 275 |  *  Adds a Texture Path to the List of already existing Paths | 
|---|
 | 276 |  * @param pathName The Path to add. | 
|---|
| [3140] | 277 | */ | 
|---|
| [4370] | 278 | void Material::addTexturePath(const char* pathName) | 
|---|
| [3140] | 279 | { | 
|---|
| [3658] | 280 |   ResourceManager::getInstance()->addImageDir(pathName); | 
|---|
| [3140] | 281 | } | 
|---|
 | 282 |  | 
|---|
| [3070] | 283 | // MAPPING // | 
|---|
 | 284 |  | 
|---|
| [2842] | 285 | /** | 
|---|
| [4836] | 286 |  *  Sets the Materials Diffuse Map | 
|---|
 | 287 |  * @param dMap the Name of the Image to Use | 
|---|
| [3070] | 288 | */ | 
|---|
| [6467] | 289 | void Material::setDiffuseMap(const char* dMap, GLenum target) | 
|---|
| [3070] | 290 | { | 
|---|
| [5308] | 291 |   PRINTF(5)("setting Diffuse Map %s\n", dMap); | 
|---|
| [5302] | 292 |   if (this->diffuseTexture != NULL) | 
|---|
| [4539] | 293 |     ResourceManager::getInstance()->unload(this->diffuseTexture); | 
|---|
| [3070] | 294 |  | 
|---|
| [4834] | 295 |   //! @todo check if RESOURCE MANAGER is availiable | 
|---|
 | 296 |   //! @todo Textures from .mtl-file need special care. | 
|---|
| [6622] | 297 |   if (dMap != NULL) | 
|---|
| [6645] | 298 |     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target); | 
|---|
| [4834] | 299 |   else | 
|---|
 | 300 |     this->diffuseTexture = NULL; | 
|---|
| [3070] | 301 | } | 
|---|
 | 302 |  | 
|---|
 | 303 | /** | 
|---|
| [4836] | 304 |  *  Sets the Materials Ambient Map | 
|---|
 | 305 |  * @param aMap the Name of the Image to Use | 
|---|
 | 306 |    @todo implement this | 
|---|
| [3070] | 307 | */ | 
|---|
| [6467] | 308 | void Material::setAmbientMap(const char* aMap, GLenum target) | 
|---|
| [3070] | 309 | { | 
|---|
 | 310 |   SDL_Surface* ambientMap; | 
|---|
 | 311 |  | 
|---|
 | 312 | } | 
|---|
 | 313 |  | 
|---|
 | 314 | /** | 
|---|
| [4836] | 315 |  *  Sets the Materials Specular Map | 
|---|
 | 316 |  * @param sMap the Name of the Image to Use | 
|---|
 | 317 |    @todo implement this | 
|---|
| [3070] | 318 | */ | 
|---|
| [6467] | 319 | void Material::setSpecularMap(const char* sMap, GLenum target) | 
|---|
| [3070] | 320 | { | 
|---|
 | 321 |   SDL_Surface* specularMap; | 
|---|
 | 322 |  | 
|---|
 | 323 | } | 
|---|
 | 324 |  | 
|---|
 | 325 | /** | 
|---|
| [4836] | 326 |  *  Sets the Materials Bumpiness | 
|---|
 | 327 |  * @param bump the Name of the Image to Use | 
|---|
 | 328 |    @todo implemet this | 
|---|
| [3070] | 329 | */ | 
|---|
| [3803] | 330 | void Material::setBump(const char* bump) | 
|---|
| [3070] | 331 | { | 
|---|
 | 332 |  | 
|---|
 | 333 | } | 
|---|