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