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