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