[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 | |
---|
[3186] | 25 | /** |
---|
[7788] | 26 | * @brief creates a Material. |
---|
[4836] | 27 | * @param mtlName Name of the Material to be added to the Material List |
---|
[5306] | 28 | */ |
---|
[7221] | 29 | Material::Material (const std::string& mtlName) |
---|
[2776] | 30 | { |
---|
[5303] | 31 | this->setClassID(CL_MATERIAL, "Material"); |
---|
| 32 | |
---|
[3790] | 33 | this->setIllum(3); |
---|
[5374] | 34 | this->setDiffuse(1,1,1); |
---|
[3790] | 35 | this->setAmbient(0,0,0); |
---|
| 36 | this->setSpecular(.5,.5,.5); |
---|
| 37 | this->setShininess(2.0); |
---|
| 38 | this->setTransparency(1.0); |
---|
| 39 | |
---|
| 40 | this->ambientTexture = NULL; |
---|
| 41 | this->specularTexture = NULL; |
---|
[7057] | 42 | this->sFactor = GL_SRC_ALPHA; |
---|
| 43 | this->tFactor = GL_ONE; |
---|
[3790] | 44 | |
---|
[3894] | 45 | this->setName(mtlName); |
---|
[2776] | 46 | } |
---|
| 47 | |
---|
[4584] | 48 | /** |
---|
[7788] | 49 | * @brief deletes a Material |
---|
| 50 | */ |
---|
[2847] | 51 | Material::~Material() |
---|
| 52 | { |
---|
[5308] | 53 | PRINTF(5)("delete Material %s.\n", this->getName()); |
---|
[4834] | 54 | |
---|
[5303] | 55 | if (this->ambientTexture != NULL) |
---|
[4834] | 56 | ResourceManager::getInstance()->unload(this->ambientTexture); |
---|
[5303] | 57 | if (this->specularTexture != NULL) |
---|
[4834] | 58 | ResourceManager::getInstance()->unload(this->specularTexture); |
---|
[7785] | 59 | |
---|
| 60 | if (this == Material::selectedMaterial) |
---|
| 61 | Material::selectedMaterial = NULL; |
---|
[2847] | 62 | } |
---|
| 63 | |
---|
[7785] | 64 | |
---|
| 65 | const Material* Material::selectedMaterial = NULL; |
---|
| 66 | |
---|
| 67 | const GLenum Material::glTextureArbs[] = |
---|
[6622] | 68 | { |
---|
[7785] | 69 | GL_TEXTURE0, |
---|
| 70 | GL_TEXTURE1, |
---|
| 71 | GL_TEXTURE2, |
---|
| 72 | GL_TEXTURE3, |
---|
| 73 | GL_TEXTURE4, |
---|
| 74 | GL_TEXTURE5, |
---|
| 75 | GL_TEXTURE6, |
---|
| 76 | GL_TEXTURE7 |
---|
| 77 | }; |
---|
[6622] | 78 | |
---|
| 79 | |
---|
[7785] | 80 | /// TODO FIX THIS |
---|
| 81 | // Material& Material::operator=(const Material& m) |
---|
| 82 | // { |
---|
| 83 | // this->setIllum(m.illumModel); |
---|
| 84 | // this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]); |
---|
| 85 | // this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]); |
---|
| 86 | // this->setSpecular(m.specular[0],m.specular[1],m.specular[2]); |
---|
| 87 | // this->setShininess(m.shininess); |
---|
| 88 | // this->setTransparency(m.transparency); |
---|
| 89 | // |
---|
| 90 | // if (this->diffuseTexture != NULL) |
---|
| 91 | // ResourceManager::getInstance()->unload(this->diffuseTexture); |
---|
| 92 | // if (m.diffuseTexture != NULL) |
---|
| 93 | // this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture); |
---|
| 94 | // this->ambientTexture = NULL; /// FIXME |
---|
| 95 | // this->specularTexture = NULL; /// FIXME |
---|
| 96 | // |
---|
| 97 | // this->setName(m.getName()); |
---|
| 98 | // } |
---|
[6622] | 99 | |
---|
| 100 | |
---|
[7785] | 101 | |
---|
[2842] | 102 | /** |
---|
[7788] | 103 | * @brief sets the material with which the following Faces will be painted |
---|
[5308] | 104 | */ |
---|
[7785] | 105 | bool Material::select() const |
---|
[3140] | 106 | { |
---|
[7785] | 107 | if (unlikely(this == Material::selectedMaterial)) |
---|
| 108 | return true; |
---|
| 109 | |
---|
[7919] | 110 | if (likely(Material::selectedMaterial != NULL)) |
---|
| 111 | { |
---|
| 112 | for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i) |
---|
| 113 | { |
---|
[7922] | 114 | glActiveTexture(Material::glTextureArbs[i]); |
---|
[7919] | 115 | //glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 116 | glDisable(GL_TEXTURE_2D); |
---|
| 117 | } |
---|
| 118 | } |
---|
[7785] | 119 | |
---|
[7919] | 120 | // setting diffuse color |
---|
[6763] | 121 | glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency); |
---|
[3140] | 122 | // setting ambient color |
---|
[3195] | 123 | glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); |
---|
[3140] | 124 | // setting up Sprecular |
---|
[3195] | 125 | glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); |
---|
[3140] | 126 | // setting up Shininess |
---|
[3195] | 127 | glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); |
---|
[4584] | 128 | |
---|
[3790] | 129 | // setting the transparency |
---|
[6812] | 130 | if (this->transparency < 1.0 || /* This allows alpha blending of 2D textures with the scene */ |
---|
[7788] | 131 | (likely(!this->textures.empty() && this->textures[0].hasAlpha()))) |
---|
[6812] | 132 | { |
---|
| 133 | glEnable(GL_BLEND); |
---|
[7057] | 134 | glBlendFunc(this->sFactor, this->tFactor); |
---|
[6812] | 135 | } |
---|
[3966] | 136 | else |
---|
[7785] | 137 | { |
---|
| 138 | glDisable(GL_BLEND); |
---|
| 139 | } |
---|
[3790] | 140 | |
---|
[4371] | 141 | |
---|
[4584] | 142 | // setting illumination Model |
---|
[4836] | 143 | if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read. |
---|
[3140] | 144 | glShadeModel(GL_FLAT); |
---|
[3195] | 145 | else if (this->illumModel >= 2) |
---|
[3140] | 146 | glShadeModel(GL_SMOOTH); |
---|
| 147 | |
---|
[7785] | 148 | |
---|
| 149 | for(unsigned int i = 0; i < this->textures.size(); ++i) |
---|
| 150 | { |
---|
[7922] | 151 | glActiveTexture(Material::glTextureArbs[i]); |
---|
[7785] | 152 | glEnable(GL_TEXTURE_2D); |
---|
[7788] | 153 | if(this->textures[i].hasAlpha()) |
---|
[7785] | 154 | { |
---|
| 155 | glEnable(GL_BLEND); |
---|
| 156 | } |
---|
[7788] | 157 | glBindTexture(GL_TEXTURE_2D, this->textures[i].getTexture()); |
---|
[7785] | 158 | } |
---|
| 159 | Material::selectedMaterial = this; |
---|
[3140] | 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
[4836] | 163 | * Sets the Material Illumination Model. |
---|
| 164 | * illu illumination Model in int form |
---|
[5308] | 165 | */ |
---|
[2776] | 166 | void Material::setIllum (int illum) |
---|
| 167 | { |
---|
[4584] | 168 | PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum); |
---|
[3195] | 169 | this->illumModel = illum; |
---|
[2776] | 170 | } |
---|
[5308] | 171 | |
---|
[2842] | 172 | /** |
---|
[4836] | 173 | * Sets the Material Illumination Model. |
---|
| 174 | * illu illumination Model in char* form |
---|
[5308] | 175 | */ |
---|
| 176 | void Material::setIllum (char* illum) |
---|
[2776] | 177 | { |
---|
[3195] | 178 | this->setIllum (atoi(illum)); |
---|
[2776] | 179 | } |
---|
| 180 | |
---|
[2842] | 181 | /** |
---|
[4836] | 182 | * Sets the Material Diffuse Color. |
---|
[7785] | 183 | * @param r Red Color Channel.a |
---|
[4836] | 184 | * @param g Green Color Channel. |
---|
| 185 | * @param b Blue Color Channel. |
---|
[5308] | 186 | */ |
---|
[2776] | 187 | void Material::setDiffuse (float r, float g, float b) |
---|
| 188 | { |
---|
[4584] | 189 | PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
[3195] | 190 | this->diffuse[0] = r; |
---|
| 191 | this->diffuse[1] = g; |
---|
[4584] | 192 | this->diffuse[2] = b; |
---|
[3195] | 193 | this->diffuse[3] = 1.0; |
---|
[2780] | 194 | |
---|
[2776] | 195 | } |
---|
[5308] | 196 | |
---|
[2842] | 197 | /** |
---|
[4836] | 198 | * Sets the Material Diffuse Color. |
---|
| 199 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
[5308] | 200 | */ |
---|
[2776] | 201 | void Material::setDiffuse (char* rgb) |
---|
| 202 | { |
---|
[3140] | 203 | float r,g,b; |
---|
| 204 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 205 | this->setDiffuse (r, g, b); |
---|
[2776] | 206 | } |
---|
| 207 | |
---|
[2842] | 208 | /** |
---|
[4836] | 209 | * Sets the Material Ambient Color. |
---|
| 210 | * @param r Red Color Channel. |
---|
| 211 | * @param g Green Color Channel. |
---|
| 212 | * @param b Blue Color Channel. |
---|
[2842] | 213 | */ |
---|
[2776] | 214 | void Material::setAmbient (float r, float g, float b) |
---|
| 215 | { |
---|
[4584] | 216 | PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
[3195] | 217 | this->ambient[0] = r; |
---|
| 218 | this->ambient[1] = g; |
---|
| 219 | this->ambient[2] = b; |
---|
| 220 | this->ambient[3] = 1.0; |
---|
[2776] | 221 | } |
---|
[5308] | 222 | |
---|
[2842] | 223 | /** |
---|
[4836] | 224 | * Sets the Material Ambient Color. |
---|
| 225 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
[5308] | 226 | */ |
---|
[2776] | 227 | void Material::setAmbient (char* rgb) |
---|
| 228 | { |
---|
[3140] | 229 | float r,g,b; |
---|
| 230 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 231 | this->setAmbient (r, g, b); |
---|
[2776] | 232 | } |
---|
| 233 | |
---|
[2842] | 234 | /** |
---|
[4836] | 235 | * Sets the Material Specular Color. |
---|
| 236 | * @param r Red Color Channel. |
---|
| 237 | * @param g Green Color Channel. |
---|
| 238 | * @param b Blue Color Channel. |
---|
[5308] | 239 | */ |
---|
[2776] | 240 | void Material::setSpecular (float r, float g, float b) |
---|
| 241 | { |
---|
[4584] | 242 | PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
[3195] | 243 | this->specular[0] = r; |
---|
| 244 | this->specular[1] = g; |
---|
| 245 | this->specular[2] = b; |
---|
| 246 | this->specular[3] = 1.0; |
---|
[7785] | 247 | } |
---|
[5308] | 248 | |
---|
[2842] | 249 | /** |
---|
[4836] | 250 | * Sets the Material Specular Color. |
---|
| 251 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
[2842] | 252 | */ |
---|
[2776] | 253 | void Material::setSpecular (char* rgb) |
---|
| 254 | { |
---|
[3140] | 255 | float r,g,b; |
---|
| 256 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 257 | this->setSpecular (r, g, b); |
---|
[2776] | 258 | } |
---|
| 259 | |
---|
[2842] | 260 | /** |
---|
[4836] | 261 | * Sets the Material Shininess. |
---|
| 262 | * @param shini stes the Shininess from float. |
---|
[2842] | 263 | */ |
---|
[2836] | 264 | void Material::setShininess (float shini) |
---|
| 265 | { |
---|
[3195] | 266 | this->shininess = shini; |
---|
[2836] | 267 | } |
---|
[2842] | 268 | /** |
---|
[4836] | 269 | * Sets the Material Shininess. |
---|
| 270 | * @param shini stes the Shininess from char*. |
---|
[2842] | 271 | */ |
---|
[2836] | 272 | void Material::setShininess (char* shini) |
---|
| 273 | { |
---|
[3195] | 274 | this->setShininess (atof(shini)); |
---|
[2836] | 275 | } |
---|
[2776] | 276 | |
---|
[2842] | 277 | /** |
---|
[4836] | 278 | * Sets the Material Transparency. |
---|
| 279 | * @param trans stes the Transparency from int. |
---|
[2842] | 280 | */ |
---|
[2776] | 281 | void Material::setTransparency (float trans) |
---|
| 282 | { |
---|
[4584] | 283 | PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans); |
---|
[3195] | 284 | this->transparency = trans; |
---|
[2776] | 285 | } |
---|
[2842] | 286 | /** |
---|
[4836] | 287 | * Sets the Material Transparency. |
---|
| 288 | * @param trans stes the Transparency from char*. |
---|
[2842] | 289 | */ |
---|
[2776] | 290 | void Material::setTransparency (char* trans) |
---|
| 291 | { |
---|
[3195] | 292 | this->setTransparency (atof(trans)); |
---|
[2776] | 293 | } |
---|
[2778] | 294 | |
---|
[3140] | 295 | /** |
---|
[4836] | 296 | * Adds a Texture Path to the List of already existing Paths |
---|
| 297 | * @param pathName The Path to add. |
---|
[3140] | 298 | */ |
---|
[7221] | 299 | void Material::addTexturePath(const std::string& pathName) |
---|
[3140] | 300 | { |
---|
[3658] | 301 | ResourceManager::getInstance()->addImageDir(pathName); |
---|
[3140] | 302 | } |
---|
| 303 | |
---|
[3070] | 304 | // MAPPING // |
---|
| 305 | |
---|
[7788] | 306 | void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber) |
---|
[7785] | 307 | { |
---|
| 308 | assert(textureNumber < Material::getMaxTextureUnits()); |
---|
| 309 | |
---|
| 310 | if (this->textures.size() <= textureNumber) |
---|
[7788] | 311 | this->textures.resize(textureNumber+1, Texture()); |
---|
[7785] | 312 | |
---|
| 313 | //! @todo check if RESOURCE MANAGER is availiable |
---|
| 314 | this->textures[textureNumber] = texture; |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | |
---|
[2842] | 318 | /** |
---|
[7785] | 319 | * @brief Sets the Materials Diffuse Map |
---|
[4836] | 320 | * @param dMap the Name of the Image to Use |
---|
[3070] | 321 | */ |
---|
[7785] | 322 | void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber) |
---|
[3070] | 323 | { |
---|
[7785] | 324 | assert(textureNumber < Material::getMaxTextureUnits()); |
---|
| 325 | |
---|
[7676] | 326 | PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str()); |
---|
[7785] | 327 | if (this->textures.size() <= textureNumber) |
---|
[7788] | 328 | this->textures.resize(textureNumber+1, Texture()); |
---|
[7785] | 329 | |
---|
[4834] | 330 | //! @todo check if RESOURCE MANAGER is availiable |
---|
[7221] | 331 | if (!dMap.empty()) |
---|
[7785] | 332 | { |
---|
[7848] | 333 | Texture* tex = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target)); |
---|
| 334 | if (tex != NULL) |
---|
| 335 | this->textures[textureNumber] = *tex; |
---|
| 336 | else |
---|
| 337 | this->textures[textureNumber] = Texture(); |
---|
[7785] | 338 | } |
---|
[4834] | 339 | else |
---|
[7785] | 340 | { |
---|
[7788] | 341 | this->textures[textureNumber] = Texture(); |
---|
[7785] | 342 | } |
---|
[3070] | 343 | } |
---|
| 344 | |
---|
| 345 | /** |
---|
[7788] | 346 | * @brief Sets the Materials Diffuse Map |
---|
[7785] | 347 | * @param surface pointer to SDL_Surface which shall be used as texture |
---|
| 348 | */ |
---|
| 349 | void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber) |
---|
| 350 | { |
---|
| 351 | assert(textureNumber < Material::getMaxTextureUnits()); |
---|
| 352 | |
---|
| 353 | |
---|
| 354 | if (this->textures.size() <= textureNumber) |
---|
[7788] | 355 | this->textures.resize(textureNumber+1, Texture()); |
---|
[7785] | 356 | |
---|
| 357 | if(surface != NULL) |
---|
| 358 | { |
---|
[7788] | 359 | this->textures[textureNumber] = Texture(surface, GL_TEXTURE_2D); |
---|
[7785] | 360 | } |
---|
| 361 | else |
---|
| 362 | { |
---|
[7788] | 363 | this->textures[textureNumber] = Texture(); |
---|
[7785] | 364 | } |
---|
| 365 | |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | |
---|
| 369 | /** |
---|
[7788] | 370 | * @brief Sets the Materials Ambient Map |
---|
[4836] | 371 | * @param aMap the Name of the Image to Use |
---|
| 372 | @todo implement this |
---|
[3070] | 373 | */ |
---|
[7221] | 374 | void Material::setAmbientMap(const std::string& aMap, GLenum target) |
---|
[3070] | 375 | { |
---|
| 376 | SDL_Surface* ambientMap; |
---|
| 377 | |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | /** |
---|
[7788] | 381 | * @brief Sets the Materials Specular Map |
---|
[4836] | 382 | * @param sMap the Name of the Image to Use |
---|
| 383 | @todo implement this |
---|
[3070] | 384 | */ |
---|
[7221] | 385 | void Material::setSpecularMap(const std::string& sMap, GLenum target) |
---|
[3070] | 386 | { |
---|
| 387 | SDL_Surface* specularMap; |
---|
| 388 | |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | /** |
---|
[7788] | 392 | * @brief Sets the Materials Bumpiness |
---|
[4836] | 393 | * @param bump the Name of the Image to Use |
---|
[7788] | 394 | * @todo implemet this |
---|
[3070] | 395 | */ |
---|
[7221] | 396 | void Material::setBump(const std::string& bump) |
---|
[3070] | 397 | { |
---|
[7785] | 398 | } |
---|
[3070] | 399 | |
---|
[7785] | 400 | |
---|
| 401 | |
---|
| 402 | int Material::getMaxTextureUnits() |
---|
| 403 | { |
---|
| 404 | int maxTexUnits = 0; |
---|
| 405 | glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits); |
---|
| 406 | return maxTexUnits; |
---|
[3070] | 407 | } |
---|