| 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 | #include "texture.h" | 
|---|
| 20 | #include <stdlib.h> | 
|---|
| 21 | #include <string.h> | 
|---|
| 22 |  | 
|---|
| 23 | using namespace std; | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | /** | 
|---|
| 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 | */ | 
|---|
| 30 | Material::Material() | 
|---|
| 31 | { | 
|---|
| 32 |   this->init(); | 
|---|
| 33 |    | 
|---|
| 34 |   this->setName (""); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | /** | 
|---|
| 38 |    \brief creates a Material. | 
|---|
| 39 |    \param mtlName Name of the Material to be added to the Material List | 
|---|
| 40 | */ | 
|---|
| 41 | Material::Material (char* mtlName) | 
|---|
| 42 | { | 
|---|
| 43 |   this->init(); | 
|---|
| 44 |    | 
|---|
| 45 |   this->setName (mtlName); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /**  | 
|---|
| 49 |     \brief deletes a Material | 
|---|
| 50 | */ | 
|---|
| 51 | Material::~Material() | 
|---|
| 52 | { | 
|---|
| 53 |   PRINTF(2)("delete Material %s.\n", this->name); | 
|---|
| 54 |   if (this->name) | 
|---|
| 55 |     delete []this->name; | 
|---|
| 56 |   if (this->diffuseTexture) | 
|---|
| 57 |     this->diffuseTexture; | 
|---|
| 58 |   if (this->nextMat) | 
|---|
| 59 |     delete this->nextMat; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 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 | */ | 
|---|
| 67 | Material* Material::addMaterial(char* mtlName) | 
|---|
| 68 | { | 
|---|
| 69 |   PRINTF(2)("adding Material %s.\n", mtlName); | 
|---|
| 70 |    Material* tmpMat = this; | 
|---|
| 71 |   while (tmpMat->nextMat != NULL) | 
|---|
| 72 |     { | 
|---|
| 73 |       tmpMat = tmpMat->nextMat; | 
|---|
| 74 |     } | 
|---|
| 75 |   tmpMat->nextMat = new Material(mtlName); | 
|---|
| 76 |   return tmpMat->nextMat; | 
|---|
| 77 |    | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 |    \brief initializes a new Material with its default Values | 
|---|
| 82 | */ | 
|---|
| 83 | void Material::init(void) | 
|---|
| 84 | { | 
|---|
| 85 |   PRINTF(2)("initializing new Material.\n"); | 
|---|
| 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); | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 |   this->diffuseTexture = NULL; | 
|---|
| 97 |   this->ambientTexture = NULL; | 
|---|
| 98 |   this->specularTexture = NULL; | 
|---|
| 99 |  | 
|---|
| 100 |   this->diffuseTextureSet = false; | 
|---|
| 101 |   this->ambientTextureSet = false; | 
|---|
| 102 |   this->specularTextureSet = false; | 
|---|
| 103 |  | 
|---|
| 104 |    | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | /** | 
|---|
| 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 | */ | 
|---|
| 112 | Material* Material::search(char* mtlName) | 
|---|
| 113 | { | 
|---|
| 114 |   PRINTF(2)("Searching for material %s", mtlName); | 
|---|
| 115 |   Material* searcher = this; | 
|---|
| 116 |   while (searcher != NULL) | 
|---|
| 117 |     { | 
|---|
| 118 |       PRINT(2)("."); | 
|---|
| 119 |       if (!strcmp (searcher->getName(), mtlName)) | 
|---|
| 120 |         { | 
|---|
| 121 |           PRINT(2)("found.\n"); | 
|---|
| 122 |           return searcher; | 
|---|
| 123 |         } | 
|---|
| 124 |       searcher = searcher->nextMat; | 
|---|
| 125 |     } | 
|---|
| 126 |   PRINT(2)("not found\n"); | 
|---|
| 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]); | 
|---|
| 137 |   glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse); | 
|---|
| 138 |  | 
|---|
| 139 |   // setting ambient color | 
|---|
| 140 |   glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); | 
|---|
| 141 |  | 
|---|
| 142 |   // setting up Sprecular | 
|---|
| 143 |   glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); | 
|---|
| 144 |  | 
|---|
| 145 |   // setting up Shininess | 
|---|
| 146 |   glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); | 
|---|
| 147 |    | 
|---|
| 148 |   // setting illumination Model  | 
|---|
| 149 |   if (this->illumModel == 1) //! \todo make this work, if no vertex-normals are read. | 
|---|
| 150 |     glShadeModel(GL_FLAT); | 
|---|
| 151 |   else if (this->illumModel >= 2) | 
|---|
| 152 |     glShadeModel(GL_SMOOTH); | 
|---|
| 153 |  | 
|---|
| 154 |   if (this->diffuseTextureSet) | 
|---|
| 155 |     glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); | 
|---|
| 156 |   else | 
|---|
| 157 |     glBindTexture(GL_TEXTURE_2D, 0); | 
|---|
| 158 |    | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|
| 162 | /** | 
|---|
| 163 |    \brief Set the Name of the Material. (Important for searching) | 
|---|
| 164 |    \param mtlName the Name of the Material to be set. | 
|---|
| 165 | */  | 
|---|
| 166 | void Material::setName (char* mtlName) | 
|---|
| 167 | { | 
|---|
| 168 |   PRINTF(3)("setting Material Name to %s.\n", this->name); | 
|---|
| 169 |   this->name = new char [strlen(mtlName)+1]; | 
|---|
| 170 |   strcpy(this->name, mtlName); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | /** | 
|---|
| 174 |    \returns The Name of The Material | 
|---|
| 175 | */ | 
|---|
| 176 | char* Material::getName (void) | 
|---|
| 177 | { | 
|---|
| 178 |   return this->name; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | /** | 
|---|
| 182 |    \brief Sets the Material Illumination Model.  | 
|---|
| 183 |    \brief illu illumination Model in int form | 
|---|
| 184 | */ | 
|---|
| 185 | void Material::setIllum (int illum) | 
|---|
| 186 | { | 
|---|
| 187 |   PRINTF(3)("setting illumModel of Material %s to %i\n", this->name, illum); | 
|---|
| 188 |   this->illumModel = illum; | 
|---|
| 189 |   //  PRINTF(3)("setting illumModel to: %i\n", illumModel); | 
|---|
| 190 | } | 
|---|
| 191 | /** | 
|---|
| 192 |    \brief Sets the Material Illumination Model.  | 
|---|
| 193 |    \brief illu illumination Model in char* form | 
|---|
| 194 | */void Material::setIllum (char* illum) | 
|---|
| 195 | { | 
|---|
| 196 |   this->setIllum (atoi(illum)); | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 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 | */ | 
|---|
| 205 | void Material::setDiffuse (float r, float g, float b) | 
|---|
| 206 | { | 
|---|
| 207 |   PRINTF(3)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); | 
|---|
| 208 |   this->diffuse[0] = r; | 
|---|
| 209 |   this->diffuse[1] = g; | 
|---|
| 210 |   this->diffuse[2] = b;   | 
|---|
| 211 |   this->diffuse[3] = 1.0; | 
|---|
| 212 |  | 
|---|
| 213 | } | 
|---|
| 214 | /** | 
|---|
| 215 |    \brief Sets the Material Diffuse Color. | 
|---|
| 216 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 217 | */ | 
|---|
| 218 | void Material::setDiffuse (char* rgb) | 
|---|
| 219 | { | 
|---|
| 220 |   float r,g,b; | 
|---|
| 221 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| 222 |   this->setDiffuse (r, g, b); | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 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 | */ | 
|---|
| 231 | void Material::setAmbient (float r, float g, float b) | 
|---|
| 232 | { | 
|---|
| 233 |   PRINTF(3)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); | 
|---|
| 234 |   this->ambient[0] = r; | 
|---|
| 235 |   this->ambient[1] = g; | 
|---|
| 236 |   this->ambient[2] = b; | 
|---|
| 237 |   this->ambient[3] = 1.0; | 
|---|
| 238 | } | 
|---|
| 239 | /** | 
|---|
| 240 |    \brief Sets the Material Ambient Color. | 
|---|
| 241 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 242 | */ | 
|---|
| 243 | void Material::setAmbient (char* rgb) | 
|---|
| 244 | { | 
|---|
| 245 |   float r,g,b; | 
|---|
| 246 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| 247 |   this->setAmbient (r, g, b); | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 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 | */ | 
|---|
| 256 | void Material::setSpecular (float r, float g, float b) | 
|---|
| 257 | { | 
|---|
| 258 |   PRINTF(3)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); | 
|---|
| 259 |   this->specular[0] = r; | 
|---|
| 260 |   this->specular[1] = g; | 
|---|
| 261 |   this->specular[2] = b; | 
|---|
| 262 |   this->specular[3] = 1.0; | 
|---|
| 263 |  } | 
|---|
| 264 | /** | 
|---|
| 265 |    \brief Sets the Material Specular Color. | 
|---|
| 266 |    \param rgb The red, green, blue channel in char format (with spaces between them) | 
|---|
| 267 | */ | 
|---|
| 268 | void Material::setSpecular (char* rgb) | 
|---|
| 269 | { | 
|---|
| 270 |   float r,g,b; | 
|---|
| 271 |   sscanf (rgb, "%f %f %f", &r, &g, &b); | 
|---|
| 272 |   this->setSpecular (r, g, b); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | /** | 
|---|
| 276 |    \brief Sets the Material Shininess. | 
|---|
| 277 |    \param shini stes the Shininess from float. | 
|---|
| 278 | */ | 
|---|
| 279 | void Material::setShininess (float shini) | 
|---|
| 280 | { | 
|---|
| 281 |   this->shininess = shini; | 
|---|
| 282 | } | 
|---|
| 283 | /** | 
|---|
| 284 |    \brief Sets the Material Shininess. | 
|---|
| 285 |    \param shini stes the Shininess from char*. | 
|---|
| 286 | */ | 
|---|
| 287 | void Material::setShininess (char* shini) | 
|---|
| 288 | { | 
|---|
| 289 |   this->setShininess (atof(shini)); | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | /** | 
|---|
| 293 |    \brief Sets the Material Transparency. | 
|---|
| 294 |    \param trans stes the Transparency from int. | 
|---|
| 295 | */ | 
|---|
| 296 | void Material::setTransparency (float trans) | 
|---|
| 297 | { | 
|---|
| 298 |   PRINTF(3)("setting Transparency of Material %s to %f.\n", this->name, trans); | 
|---|
| 299 |   this->transparency = trans; | 
|---|
| 300 | } | 
|---|
| 301 | /** | 
|---|
| 302 |    \brief Sets the Material Transparency. | 
|---|
| 303 |    \param trans stes the Transparency from char*. | 
|---|
| 304 | */ | 
|---|
| 305 | void Material::setTransparency (char* trans) | 
|---|
| 306 | { | 
|---|
| 307 |   this->setTransparency (atof(trans)); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 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 | { | 
|---|
| 316 |   PathList::getInstance()->addPath(pathName); | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | // MAPPING // | 
|---|
| 320 |  | 
|---|
| 321 | /** | 
|---|
| 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 | { | 
|---|
| 327 |   PRINTF(3)("setting Diffuse Map %s\n", dMap); | 
|---|
| 328 |   diffuseTexture = new Texture(); | 
|---|
| 329 |   this->diffuseTextureSet = diffuseTexture->loadImage(dMap); | 
|---|
| 330 |  | 
|---|
| 331 | } | 
|---|
| 332 |  | 
|---|
| 333 | /** | 
|---|
| 334 |    \brief Sets the Materials Ambient Map | 
|---|
| 335 |    \param aMap the Name of the Image to Use | 
|---|
| 336 |    \todo implement this | 
|---|
| 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 | 
|---|
| 347 |    \todo implement this | 
|---|
| 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 | 
|---|
| 358 |    \todo implemet this | 
|---|
| 359 | */ | 
|---|
| 360 | void Material::setBump(char* bump) | 
|---|
| 361 | { | 
|---|
| 362 |  | 
|---|
| 363 | } | 
|---|