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