[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 |
---|
| 90 | if (this->transparency == 1.0) |
---|
| 91 | { |
---|
| 92 | glDisable(GL_BLEND); |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | glEnable(GL_BLEND); |
---|
| 97 | glColor4f(1.0f, 1.0f, 1.0f, this->transparency); |
---|
| 98 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 99 | } |
---|
| 100 | |
---|
[3140] | 101 | // setting illumination Model |
---|
[3195] | 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 | |
---|
[3195] | 107 | if (this->diffuseTextureSet) |
---|
[3536] | 108 | { |
---|
| 109 | glEnable(GL_TEXTURE_2D); |
---|
| 110 | glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); |
---|
| 111 | } |
---|
[3140] | 112 | else |
---|
[3536] | 113 | { |
---|
| 114 | glDisable(GL_TEXTURE_2D); |
---|
| 115 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 116 | } |
---|
[3140] | 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
[2842] | 120 | \brief Set the Name of the Material. (Important for searching) |
---|
| 121 | \param mtlName the Name of the Material to be set. |
---|
| 122 | */ |
---|
[3894] | 123 | void Material::setName (const char* mtlName) |
---|
[2776] | 124 | { |
---|
[3894] | 125 | if (this->name) |
---|
| 126 | delete this->name; |
---|
| 127 | if (mtlName) |
---|
| 128 | { |
---|
| 129 | this->name = new char [strlen(mtlName)+1]; |
---|
| 130 | strcpy(this->name, mtlName); |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | this->name = new char[2]; |
---|
| 135 | strcpy(this->name, ""); |
---|
| 136 | } |
---|
[3206] | 137 | } |
---|
[3140] | 138 | |
---|
[2842] | 139 | /** |
---|
| 140 | \returns The Name of The Material |
---|
| 141 | */ |
---|
[2778] | 142 | char* Material::getName (void) |
---|
| 143 | { |
---|
[3195] | 144 | return this->name; |
---|
[2778] | 145 | } |
---|
[2776] | 146 | |
---|
[2842] | 147 | /** |
---|
| 148 | \brief Sets the Material Illumination Model. |
---|
| 149 | \brief illu illumination Model in int form |
---|
| 150 | */ |
---|
[2776] | 151 | void Material::setIllum (int illum) |
---|
| 152 | { |
---|
[3548] | 153 | PRINTF(4)("setting illumModel of Material %s to %i\n", this->name, illum); |
---|
[3195] | 154 | this->illumModel = illum; |
---|
[2776] | 155 | } |
---|
[2842] | 156 | /** |
---|
| 157 | \brief Sets the Material Illumination Model. |
---|
| 158 | \brief illu illumination Model in char* form |
---|
| 159 | */void Material::setIllum (char* illum) |
---|
[2776] | 160 | { |
---|
[3195] | 161 | this->setIllum (atoi(illum)); |
---|
[2776] | 162 | } |
---|
| 163 | |
---|
[2842] | 164 | /** |
---|
| 165 | \brief Sets the Material Diffuse Color. |
---|
| 166 | \param r Red Color Channel. |
---|
| 167 | \param g Green Color Channel. |
---|
| 168 | \param b Blue Color Channel. |
---|
| 169 | */ |
---|
[2776] | 170 | void Material::setDiffuse (float r, float g, float b) |
---|
| 171 | { |
---|
[3548] | 172 | PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
[3195] | 173 | this->diffuse[0] = r; |
---|
| 174 | this->diffuse[1] = g; |
---|
| 175 | this->diffuse[2] = b; |
---|
| 176 | this->diffuse[3] = 1.0; |
---|
[2780] | 177 | |
---|
[2776] | 178 | } |
---|
[2842] | 179 | /** |
---|
| 180 | \brief Sets the Material Diffuse Color. |
---|
| 181 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 182 | */ |
---|
[2776] | 183 | void Material::setDiffuse (char* rgb) |
---|
| 184 | { |
---|
[3140] | 185 | float r,g,b; |
---|
| 186 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 187 | this->setDiffuse (r, g, b); |
---|
[2776] | 188 | } |
---|
| 189 | |
---|
[2842] | 190 | /** |
---|
| 191 | \brief Sets the Material Ambient Color. |
---|
| 192 | \param r Red Color Channel. |
---|
| 193 | \param g Green Color Channel. |
---|
| 194 | \param b Blue Color Channel. |
---|
| 195 | */ |
---|
[2776] | 196 | void Material::setAmbient (float r, float g, float b) |
---|
| 197 | { |
---|
[3548] | 198 | PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
[3195] | 199 | this->ambient[0] = r; |
---|
| 200 | this->ambient[1] = g; |
---|
| 201 | this->ambient[2] = b; |
---|
| 202 | this->ambient[3] = 1.0; |
---|
[2776] | 203 | } |
---|
[2842] | 204 | /** |
---|
| 205 | \brief Sets the Material Ambient Color. |
---|
| 206 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 207 | */ |
---|
[2776] | 208 | void Material::setAmbient (char* rgb) |
---|
| 209 | { |
---|
[3140] | 210 | float r,g,b; |
---|
| 211 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 212 | this->setAmbient (r, g, b); |
---|
[2776] | 213 | } |
---|
| 214 | |
---|
[2842] | 215 | /** |
---|
| 216 | \brief Sets the Material Specular Color. |
---|
| 217 | \param r Red Color Channel. |
---|
| 218 | \param g Green Color Channel. |
---|
| 219 | \param b Blue Color Channel. |
---|
| 220 | */ |
---|
[2776] | 221 | void Material::setSpecular (float r, float g, float b) |
---|
| 222 | { |
---|
[3548] | 223 | PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
[3195] | 224 | this->specular[0] = r; |
---|
| 225 | this->specular[1] = g; |
---|
| 226 | this->specular[2] = b; |
---|
| 227 | this->specular[3] = 1.0; |
---|
[2804] | 228 | } |
---|
[2842] | 229 | /** |
---|
| 230 | \brief Sets the Material Specular Color. |
---|
| 231 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 232 | */ |
---|
[2776] | 233 | void Material::setSpecular (char* rgb) |
---|
| 234 | { |
---|
[3140] | 235 | float r,g,b; |
---|
| 236 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 237 | this->setSpecular (r, g, b); |
---|
[2776] | 238 | } |
---|
| 239 | |
---|
[2842] | 240 | /** |
---|
| 241 | \brief Sets the Material Shininess. |
---|
| 242 | \param shini stes the Shininess from float. |
---|
| 243 | */ |
---|
[2836] | 244 | void Material::setShininess (float shini) |
---|
| 245 | { |
---|
[3195] | 246 | this->shininess = shini; |
---|
[2836] | 247 | } |
---|
[2842] | 248 | /** |
---|
| 249 | \brief Sets the Material Shininess. |
---|
| 250 | \param shini stes the Shininess from char*. |
---|
| 251 | */ |
---|
[2836] | 252 | void Material::setShininess (char* shini) |
---|
| 253 | { |
---|
[3195] | 254 | this->setShininess (atof(shini)); |
---|
[2836] | 255 | } |
---|
[2776] | 256 | |
---|
[2842] | 257 | /** |
---|
| 258 | \brief Sets the Material Transparency. |
---|
| 259 | \param trans stes the Transparency from int. |
---|
| 260 | */ |
---|
[2776] | 261 | void Material::setTransparency (float trans) |
---|
| 262 | { |
---|
[3548] | 263 | PRINTF(4)("setting Transparency of Material %s to %f.\n", this->name, trans); |
---|
[3195] | 264 | this->transparency = trans; |
---|
[2776] | 265 | } |
---|
[2842] | 266 | /** |
---|
| 267 | \brief Sets the Material Transparency. |
---|
| 268 | \param trans stes the Transparency from char*. |
---|
| 269 | */ |
---|
[2776] | 270 | void Material::setTransparency (char* trans) |
---|
| 271 | { |
---|
[3195] | 272 | this->setTransparency (atof(trans)); |
---|
[2776] | 273 | } |
---|
[2778] | 274 | |
---|
[3140] | 275 | /** |
---|
| 276 | \brief Adds a Texture Path to the List of already existing Paths |
---|
| 277 | \param pathName The Path to add. |
---|
| 278 | */ |
---|
| 279 | void Material::addTexturePath(char* pathName) |
---|
| 280 | { |
---|
[3658] | 281 | ResourceManager::getInstance()->addImageDir(pathName); |
---|
[3140] | 282 | } |
---|
| 283 | |
---|
[3070] | 284 | // MAPPING // |
---|
| 285 | |
---|
[2842] | 286 | /** |
---|
[3070] | 287 | \brief Sets the Materials Diffuse Map |
---|
| 288 | \param dMap the Name of the Image to Use |
---|
| 289 | */ |
---|
[3803] | 290 | void Material::setDiffuseMap(const char* dMap) |
---|
[3070] | 291 | { |
---|
[3548] | 292 | PRINTF(4)("setting Diffuse Map %s\n", dMap); |
---|
[3658] | 293 | // diffuseTexture = new Texture(); |
---|
| 294 | // this->diffuseTextureSet = diffuseTexture->loadImage(dMap); |
---|
[3070] | 295 | |
---|
[3655] | 296 | //! \todo check if RESOURCE MANAGER is availiable |
---|
[3658] | 297 | //! \todo Textures from .mtl-file need special care. |
---|
[3672] | 298 | this->diffuseTextureSet = this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE); |
---|
[3070] | 299 | } |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | \brief Sets the Materials Ambient Map |
---|
| 303 | \param aMap the Name of the Image to Use |
---|
[3195] | 304 | \todo implement this |
---|
[3070] | 305 | */ |
---|
[3803] | 306 | void Material::setAmbientMap(const char* aMap) |
---|
[3070] | 307 | { |
---|
| 308 | SDL_Surface* ambientMap; |
---|
| 309 | |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | \brief Sets the Materials Specular Map |
---|
| 314 | \param sMap the Name of the Image to Use |
---|
[3195] | 315 | \todo implement this |
---|
[3070] | 316 | */ |
---|
[3803] | 317 | void Material::setSpecularMap(const char* sMap) |
---|
[3070] | 318 | { |
---|
| 319 | SDL_Surface* specularMap; |
---|
| 320 | |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | /** |
---|
| 324 | \brief Sets the Materials Bumpiness |
---|
| 325 | \param bump the Name of the Image to Use |
---|
[3195] | 326 | \todo implemet this |
---|
[3070] | 327 | */ |
---|
[3803] | 328 | void Material::setBump(const char* bump) |
---|
[3070] | 329 | { |
---|
| 330 | |
---|
| 331 | } |
---|