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