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