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