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