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