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