[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 | */ |
---|
[8312] | 105 | bool Material::select() const |
---|
[3140] | 106 | { |
---|
[7785] | 107 | if (unlikely(this == Material::selectedMaterial)) |
---|
| 108 | return true; |
---|
| 109 | |
---|
[8037] | 110 | /// !! HACK !!! FIX THIS IN MODEL /// |
---|
| 111 | else if (likely(Material::selectedMaterial != NULL)) |
---|
| 112 | { |
---|
| 113 | Material::unselect(); |
---|
| 114 | // for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i) |
---|
| 115 | // { |
---|
| 116 | // glActiveTexture(Material::glTextureArbs[i]); |
---|
| 117 | // glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 118 | // glDisable(GL_TEXTURE_2D); |
---|
| 119 | // } |
---|
| 120 | } |
---|
| 121 | |
---|
[7919] | 122 | if (likely(Material::selectedMaterial != NULL)) |
---|
| 123 | { |
---|
| 124 | for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i) |
---|
| 125 | { |
---|
[7922] | 126 | glActiveTexture(Material::glTextureArbs[i]); |
---|
[7919] | 127 | //glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 128 | glDisable(GL_TEXTURE_2D); |
---|
| 129 | } |
---|
| 130 | } |
---|
[7785] | 131 | |
---|
[7919] | 132 | // setting diffuse color |
---|
[6763] | 133 | glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency); |
---|
[3140] | 134 | // setting ambient color |
---|
[3195] | 135 | glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); |
---|
[3140] | 136 | // setting up Sprecular |
---|
[3195] | 137 | glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); |
---|
[3140] | 138 | // setting up Shininess |
---|
[3195] | 139 | glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); |
---|
[4584] | 140 | |
---|
[3790] | 141 | // setting the transparency |
---|
[6812] | 142 | if (this->transparency < 1.0 || /* This allows alpha blending of 2D textures with the scene */ |
---|
[7788] | 143 | (likely(!this->textures.empty() && this->textures[0].hasAlpha()))) |
---|
[6812] | 144 | { |
---|
| 145 | glEnable(GL_BLEND); |
---|
[7057] | 146 | glBlendFunc(this->sFactor, this->tFactor); |
---|
[6812] | 147 | } |
---|
[3966] | 148 | else |
---|
[7785] | 149 | { |
---|
| 150 | glDisable(GL_BLEND); |
---|
| 151 | } |
---|
[3790] | 152 | |
---|
[4371] | 153 | |
---|
[4584] | 154 | // setting illumination Model |
---|
[4836] | 155 | if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read. |
---|
[3140] | 156 | glShadeModel(GL_FLAT); |
---|
[3195] | 157 | else if (this->illumModel >= 2) |
---|
[3140] | 158 | glShadeModel(GL_SMOOTH); |
---|
| 159 | |
---|
[7785] | 160 | for(unsigned int i = 0; i < this->textures.size(); ++i) |
---|
| 161 | { |
---|
[7922] | 162 | glActiveTexture(Material::glTextureArbs[i]); |
---|
[7785] | 163 | glEnable(GL_TEXTURE_2D); |
---|
[7788] | 164 | if(this->textures[i].hasAlpha()) |
---|
[7785] | 165 | { |
---|
| 166 | glEnable(GL_BLEND); |
---|
| 167 | } |
---|
[7788] | 168 | glBindTexture(GL_TEXTURE_2D, this->textures[i].getTexture()); |
---|
[7785] | 169 | } |
---|
| 170 | Material::selectedMaterial = this; |
---|
[8316] | 171 | |
---|
| 172 | return true; |
---|
[3140] | 173 | } |
---|
| 174 | |
---|
[8037] | 175 | void Material::unselect() |
---|
| 176 | { |
---|
| 177 | Material::selectedMaterial = NULL; |
---|
| 178 | for(unsigned int i = 0; i < 8; ++i) |
---|
| 179 | { |
---|
| 180 | glActiveTexture(Material::glTextureArbs[i]); |
---|
| 181 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 182 | glDisable(GL_TEXTURE_2D); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
[3140] | 186 | /** |
---|
[4836] | 187 | * Sets the Material Illumination Model. |
---|
| 188 | * illu illumination Model in int form |
---|
[5308] | 189 | */ |
---|
[2776] | 190 | void Material::setIllum (int illum) |
---|
| 191 | { |
---|
[4584] | 192 | PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum); |
---|
[3195] | 193 | this->illumModel = illum; |
---|
[2776] | 194 | } |
---|
[5308] | 195 | |
---|
[2842] | 196 | /** |
---|
[4836] | 197 | * Sets the Material Illumination Model. |
---|
| 198 | * illu illumination Model in char* form |
---|
[5308] | 199 | */ |
---|
| 200 | void Material::setIllum (char* illum) |
---|
[2776] | 201 | { |
---|
[3195] | 202 | this->setIllum (atoi(illum)); |
---|
[2776] | 203 | } |
---|
| 204 | |
---|
[2842] | 205 | /** |
---|
[4836] | 206 | * Sets the Material Diffuse Color. |
---|
[7785] | 207 | * @param r Red Color Channel.a |
---|
[4836] | 208 | * @param g Green Color Channel. |
---|
| 209 | * @param b Blue Color Channel. |
---|
[5308] | 210 | */ |
---|
[2776] | 211 | void Material::setDiffuse (float r, float g, float b) |
---|
| 212 | { |
---|
[4584] | 213 | PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
[3195] | 214 | this->diffuse[0] = r; |
---|
| 215 | this->diffuse[1] = g; |
---|
[4584] | 216 | this->diffuse[2] = b; |
---|
[3195] | 217 | this->diffuse[3] = 1.0; |
---|
[2780] | 218 | |
---|
[2776] | 219 | } |
---|
[5308] | 220 | |
---|
[2842] | 221 | /** |
---|
[4836] | 222 | * Sets the Material Diffuse Color. |
---|
| 223 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
[5308] | 224 | */ |
---|
[2776] | 225 | void Material::setDiffuse (char* rgb) |
---|
| 226 | { |
---|
[3140] | 227 | float r,g,b; |
---|
| 228 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 229 | this->setDiffuse (r, g, b); |
---|
[2776] | 230 | } |
---|
| 231 | |
---|
[2842] | 232 | /** |
---|
[4836] | 233 | * Sets the Material Ambient Color. |
---|
| 234 | * @param r Red Color Channel. |
---|
| 235 | * @param g Green Color Channel. |
---|
| 236 | * @param b Blue Color Channel. |
---|
[2842] | 237 | */ |
---|
[2776] | 238 | void Material::setAmbient (float r, float g, float b) |
---|
| 239 | { |
---|
[4584] | 240 | PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
[3195] | 241 | this->ambient[0] = r; |
---|
| 242 | this->ambient[1] = g; |
---|
| 243 | this->ambient[2] = b; |
---|
| 244 | this->ambient[3] = 1.0; |
---|
[2776] | 245 | } |
---|
[5308] | 246 | |
---|
[2842] | 247 | /** |
---|
[4836] | 248 | * Sets the Material Ambient Color. |
---|
| 249 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
[5308] | 250 | */ |
---|
[2776] | 251 | void Material::setAmbient (char* rgb) |
---|
| 252 | { |
---|
[3140] | 253 | float r,g,b; |
---|
| 254 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 255 | this->setAmbient (r, g, b); |
---|
[2776] | 256 | } |
---|
| 257 | |
---|
[2842] | 258 | /** |
---|
[4836] | 259 | * Sets the Material Specular Color. |
---|
| 260 | * @param r Red Color Channel. |
---|
| 261 | * @param g Green Color Channel. |
---|
| 262 | * @param b Blue Color Channel. |
---|
[5308] | 263 | */ |
---|
[2776] | 264 | void Material::setSpecular (float r, float g, float b) |
---|
| 265 | { |
---|
[4584] | 266 | PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b); |
---|
[3195] | 267 | this->specular[0] = r; |
---|
| 268 | this->specular[1] = g; |
---|
| 269 | this->specular[2] = b; |
---|
| 270 | this->specular[3] = 1.0; |
---|
[7785] | 271 | } |
---|
[5308] | 272 | |
---|
[2842] | 273 | /** |
---|
[4836] | 274 | * Sets the Material Specular Color. |
---|
| 275 | * @param rgb The red, green, blue channel in char format (with spaces between them) |
---|
[2842] | 276 | */ |
---|
[2776] | 277 | void Material::setSpecular (char* rgb) |
---|
| 278 | { |
---|
[3140] | 279 | float r,g,b; |
---|
| 280 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 281 | this->setSpecular (r, g, b); |
---|
[2776] | 282 | } |
---|
| 283 | |
---|
[2842] | 284 | /** |
---|
[4836] | 285 | * Sets the Material Shininess. |
---|
| 286 | * @param shini stes the Shininess from float. |
---|
[2842] | 287 | */ |
---|
[2836] | 288 | void Material::setShininess (float shini) |
---|
| 289 | { |
---|
[3195] | 290 | this->shininess = shini; |
---|
[2836] | 291 | } |
---|
[2842] | 292 | /** |
---|
[4836] | 293 | * Sets the Material Shininess. |
---|
| 294 | * @param shini stes the Shininess from char*. |
---|
[2842] | 295 | */ |
---|
[2836] | 296 | void Material::setShininess (char* shini) |
---|
| 297 | { |
---|
[3195] | 298 | this->setShininess (atof(shini)); |
---|
[2836] | 299 | } |
---|
[2776] | 300 | |
---|
[2842] | 301 | /** |
---|
[4836] | 302 | * Sets the Material Transparency. |
---|
| 303 | * @param trans stes the Transparency from int. |
---|
[2842] | 304 | */ |
---|
[2776] | 305 | void Material::setTransparency (float trans) |
---|
| 306 | { |
---|
[4584] | 307 | PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans); |
---|
[3195] | 308 | this->transparency = trans; |
---|
[2776] | 309 | } |
---|
[2842] | 310 | /** |
---|
[4836] | 311 | * Sets the Material Transparency. |
---|
| 312 | * @param trans stes the Transparency from char*. |
---|
[2842] | 313 | */ |
---|
[2776] | 314 | void Material::setTransparency (char* trans) |
---|
| 315 | { |
---|
[3195] | 316 | this->setTransparency (atof(trans)); |
---|
[2776] | 317 | } |
---|
[2778] | 318 | |
---|
[3140] | 319 | /** |
---|
[4836] | 320 | * Adds a Texture Path to the List of already existing Paths |
---|
| 321 | * @param pathName The Path to add. |
---|
[3140] | 322 | */ |
---|
[7221] | 323 | void Material::addTexturePath(const std::string& pathName) |
---|
[3140] | 324 | { |
---|
[3658] | 325 | ResourceManager::getInstance()->addImageDir(pathName); |
---|
[3140] | 326 | } |
---|
| 327 | |
---|
[3070] | 328 | // MAPPING // |
---|
| 329 | |
---|
[7788] | 330 | void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber) |
---|
[7785] | 331 | { |
---|
| 332 | assert(textureNumber < Material::getMaxTextureUnits()); |
---|
| 333 | |
---|
| 334 | if (this->textures.size() <= textureNumber) |
---|
[7788] | 335 | this->textures.resize(textureNumber+1, Texture()); |
---|
[7785] | 336 | |
---|
| 337 | //! @todo check if RESOURCE MANAGER is availiable |
---|
| 338 | this->textures[textureNumber] = texture; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | |
---|
[2842] | 342 | /** |
---|
[7785] | 343 | * @brief Sets the Materials Diffuse Map |
---|
[4836] | 344 | * @param dMap the Name of the Image to Use |
---|
[3070] | 345 | */ |
---|
[7785] | 346 | void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber) |
---|
[3070] | 347 | { |
---|
[7785] | 348 | assert(textureNumber < Material::getMaxTextureUnits()); |
---|
| 349 | |
---|
[7676] | 350 | PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str()); |
---|
[7785] | 351 | if (this->textures.size() <= textureNumber) |
---|
[7788] | 352 | this->textures.resize(textureNumber+1, Texture()); |
---|
[7785] | 353 | |
---|
[4834] | 354 | //! @todo check if RESOURCE MANAGER is availiable |
---|
[7221] | 355 | if (!dMap.empty()) |
---|
[7785] | 356 | { |
---|
[7848] | 357 | Texture* tex = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target)); |
---|
| 358 | if (tex != NULL) |
---|
| 359 | this->textures[textureNumber] = *tex; |
---|
| 360 | else |
---|
| 361 | this->textures[textureNumber] = Texture(); |
---|
[7785] | 362 | } |
---|
[4834] | 363 | else |
---|
[7785] | 364 | { |
---|
[7788] | 365 | this->textures[textureNumber] = Texture(); |
---|
[7785] | 366 | } |
---|
[3070] | 367 | } |
---|
| 368 | |
---|
| 369 | /** |
---|
[7788] | 370 | * @brief Sets the Materials Diffuse Map |
---|
[7785] | 371 | * @param surface pointer to SDL_Surface which shall be used as texture |
---|
| 372 | */ |
---|
| 373 | void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber) |
---|
| 374 | { |
---|
| 375 | assert(textureNumber < Material::getMaxTextureUnits()); |
---|
| 376 | |
---|
| 377 | |
---|
| 378 | if (this->textures.size() <= textureNumber) |
---|
[7788] | 379 | this->textures.resize(textureNumber+1, Texture()); |
---|
[7785] | 380 | |
---|
| 381 | if(surface != NULL) |
---|
| 382 | { |
---|
[7788] | 383 | this->textures[textureNumber] = Texture(surface, GL_TEXTURE_2D); |
---|
[7785] | 384 | } |
---|
| 385 | else |
---|
| 386 | { |
---|
[7788] | 387 | this->textures[textureNumber] = Texture(); |
---|
[7785] | 388 | } |
---|
| 389 | |
---|
| 390 | } |
---|
| 391 | |
---|
[8037] | 392 | /** |
---|
| 393 | * @brief renders viewport buffer (?? or another buffer ;-)) to a texture |
---|
| 394 | * @param textureNumber select the texture unit that will be overwritten |
---|
| 395 | */ |
---|
| 396 | void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height) |
---|
| 397 | { |
---|
| 398 | assert(textureNumber < Material::getMaxTextureUnits()); |
---|
| 399 | assert(textureNumber < this->textures.size()); |
---|
[7785] | 400 | |
---|
[8312] | 401 | // HACK |
---|
| 402 | glActiveTexture(textureNumber); |
---|
[8037] | 403 | glEnable(GL_TEXTURE_2D); |
---|
| 404 | glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture()); |
---|
| 405 | glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height); |
---|
| 406 | |
---|
| 407 | } |
---|
| 408 | |
---|
[7785] | 409 | /** |
---|
[7788] | 410 | * @brief Sets the Materials Ambient Map |
---|
[8037] | 411 | * @todo implement this |
---|
[3070] | 412 | */ |
---|
[7221] | 413 | void Material::setAmbientMap(const std::string& aMap, GLenum target) |
---|
[3070] | 414 | { |
---|
[8316] | 415 | /// FIXME SDL_Surface* ambientMap; |
---|
[3070] | 416 | |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | /** |
---|
[7788] | 420 | * @brief Sets the Materials Specular Map |
---|
[4836] | 421 | * @param sMap the Name of the Image to Use |
---|
| 422 | @todo implement this |
---|
[3070] | 423 | */ |
---|
[7221] | 424 | void Material::setSpecularMap(const std::string& sMap, GLenum target) |
---|
[3070] | 425 | { |
---|
[8316] | 426 | /// FIXME SDL_Surface* specularMap; |
---|
[3070] | 427 | |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | /** |
---|
[7788] | 431 | * @brief Sets the Materials Bumpiness |
---|
[4836] | 432 | * @param bump the Name of the Image to Use |
---|
[7788] | 433 | * @todo implemet this |
---|
[3070] | 434 | */ |
---|
[7221] | 435 | void Material::setBump(const std::string& bump) |
---|
[3070] | 436 | { |
---|
[7785] | 437 | } |
---|
[3070] | 438 | |
---|
[7785] | 439 | |
---|
| 440 | |
---|
[8316] | 441 | unsigned int Material::getMaxTextureUnits() |
---|
[7785] | 442 | { |
---|
| 443 | int maxTexUnits = 0; |
---|
| 444 | glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits); |
---|
| 445 | return maxTexUnits; |
---|
[3070] | 446 | } |
---|