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