Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

glt/material.cpp

Go to the documentation of this file.
00001 #include "material.h"
00002 
00015 #include <glt/error.h>
00016 
00017 #include <cassert>
00018 #include <cstring>
00019 using namespace std;
00020 
00021 GltMaterial::GltMaterial(const GLenum face,const bool getIt)
00022 : _face(face)
00023 {
00024     assert(_face==GL_FRONT_AND_BACK || _face==GL_FRONT || _face==GL_BACK);
00025 
00026     // TODO - OpenGL Defaults
00027     
00028     _shininess = 0.0;
00029     memset(_colorIndex,0,sizeof(_colorIndex));
00030 
00031     if (getIt)
00032         get();
00033 }
00034 
00035 GltMaterial::GltMaterial
00036 (
00037     const GltColor &ambient,
00038     const GltColor &diffuse,
00039     const GltColor &specular,
00040     const GltColor &emission,
00041     const GLfloat   shininess,
00042     const GLenum face
00043 )
00044 : 
00045     _face(face),
00046     _ambient(ambient),
00047     _diffuse(diffuse),
00048     _specular(specular),
00049     _emission(emission),
00050     _shininess(shininess)
00051 {
00052     assert(_face==GL_FRONT_AND_BACK || _face==GL_FRONT || _face==GL_BACK);
00053 
00054     memset(_colorIndex,0,sizeof(_colorIndex));
00055 }
00056 
00057 GltMaterial::GltMaterial(const std::string &name,const GLenum face)
00058 : _face(face)
00059 {
00060     assert(_face==GL_FRONT_AND_BACK || _face==GL_FRONT || _face==GL_BACK);
00061 
00062     // Do a binary search through the list of names
00063 
00064     int i=0;
00065     int j=_matSize;
00066 
00067     for (;;)
00068     {
00069         int k = ((j-i)>>1)+i;
00070         const int res = strcmp(name.c_str(),_matName[k]);
00071 
00072         if (res<0)
00073             j = k;
00074         else
00075             if (res>0)
00076                 i = k+1;
00077             else
00078             {
00079                 operator=(*_matValue[k]);
00080                 return;
00081             }
00082 
00083         if (i==k && j==k || i==_matSize)
00084         {
00085             assert(!"Material not found");
00086             operator=(GltMaterial());
00087             return;
00088         }
00089     }
00090 }
00091 
00092 GltMaterial::~GltMaterial()
00093 {
00094 }
00095 
00096 void 
00097 GltMaterial::get()
00098 {
00099     // Can't query front and back at same time - beware
00100     const GLenum face = (_face==GL_FRONT_AND_BACK ? GL_FRONT : _face);
00101 
00102     GLERROR
00103 
00104     _ambient. glGetMaterial(face,GL_AMBIENT);
00105     _diffuse. glGetMaterial(face,GL_DIFFUSE);
00106     _specular.glGetMaterial(face,GL_SPECULAR);
00107     _emission.glGetMaterial(face,GL_EMISSION);
00108 
00109     glGetMaterialfv(face, GL_SHININESS, &_shininess);
00110 
00111     GLERROR
00112 }
00113 
00114 void 
00115 GltMaterial::set() const
00116 {
00117     GLERROR
00118 
00119     _ambient. glMaterial(_face,GL_AMBIENT);
00120     _diffuse. glMaterial(_face,GL_DIFFUSE);
00121     _specular.glMaterial(_face,GL_SPECULAR);
00122     _emission.glMaterial(_face,GL_EMISSION);
00123 
00124     glMaterialfv(_face, GL_SHININESS, &_shininess);
00125 
00126     GLERROR
00127 }
00128 
00129 GLenum   &GltMaterial::face()       { return _face;       }
00130 GltColor &GltMaterial::ambient()    { return _ambient;    }
00131 GltColor &GltMaterial::diffuse()    { return _diffuse;    }
00132 GltColor &GltMaterial::specular()   { return _specular;   }
00133 GltColor &GltMaterial::emission()   { return _emission;   }
00134 GLfloat  &GltMaterial::shininess()  { return _shininess;  }
00135 GLint    *GltMaterial::colorIndex() { return _colorIndex; }
00136 
00137 const GLenum   &GltMaterial::face()       const { return _face;       }
00138 const GltColor &GltMaterial::ambient()    const { return _ambient;    }
00139 const GltColor &GltMaterial::diffuse()    const { return _diffuse;    }
00140 const GltColor &GltMaterial::specular()   const { return _specular;   }
00141 const GltColor &GltMaterial::emission()   const { return _emission;   }
00142 const GLfloat  &GltMaterial::shininess()  const { return _shininess;  }
00143 const GLint    *GltMaterial::colorIndex() const { return _colorIndex; }
00144 
00145 //
00146 // Adapted from 
00147 //  http://www.cs.utk.edu/~kuck/materials_ogl.htm
00148 //  http://devernay.free.fr/cours/opengl/materials.html
00149 //  http://www.sgi.com/software/opengl/advanced98/notes/node119.html
00150 //  
00151 
00153 
00154 const GltMaterial blackPlasticMaterial
00155 (
00156     GltColor(0.00, 0.00, 0.00), // Ambient
00157     GltColor(0.01, 0.01, 0.01), // Diffuse
00158     GltColor(0.50, 0.50, 0.50), // Specular
00159     GltColor(0.00, 0.00, 0.00), // Emission
00160     32.0f                       // Shininess
00161 );
00162 
00164 
00165 const GltMaterial blackRubberMaterial
00166 (
00167     GltColor(0.02, 0.02, 0.02), // Ambient
00168     GltColor(0.01, 0.01, 0.01), // Diffuse
00169     GltColor(0.40, 0.40, 0.40), // Specular
00170     GltColor(0.00, 0.00, 0.00), // Emission
00171     10.0f                       // Shininess
00172 );
00173 
00175 
00176 const GltMaterial brassMaterial
00177 (
00178     GltColor(0.329412, 0.223529, 0.027451), // Ambient
00179     GltColor(0.780392, 0.568627, 0.113725), // Diffuse
00180     GltColor(0.992157, 0.941176, 0.807843), // Specular
00181     GltColor(0.0,0.0,0.0),                  // Emission
00182     27.8974f                                // Shininess
00183 );
00184 
00186 
00187 const GltMaterial bronzeMaterial
00188 (
00189     GltColor(0.2125  , 0.1275  , 0.054   ), // Ambient
00190     GltColor(0.714   , 0.4284  , 0.18144 ), // Diffuse
00191     GltColor(0.393548, 0.271906, 0.166721), // Specular
00192     GltColor(0.0,0.0,0.0),                  // Emission
00193     25.6f                                   // Shininess
00194 );
00195 
00197 
00198 const GltMaterial chromeMaterial
00199 (
00200     GltColor(0.25,     0.25,     0.25    ), // Ambient
00201     GltColor(0.4,      0.4,      0.4     ), // Diffuse
00202     GltColor(0.774597, 0.774597, 0.774597), // Specular
00203     GltColor(0.0,0.0,0.0),                  // Emission
00204     76.8f                                   // Shininess
00205 );
00206 
00208 
00209 const GltMaterial copperMaterial
00210 (
00211     GltColor(0.19125,  0.0735,   0.0225  ), // Ambient
00212     GltColor(0.7038,   0.27048,  0.0828  ), // Diffuse
00213     GltColor(0.256777, 0.137622, 0.086014), // Specular
00214     GltColor(0.00,0.00,0.00),               // Emission
00215     12.8f                                   // Shininess
00216 );
00217 
00219 
00220 const GltMaterial emeraldMaterial
00221 (
00222     GltColor(0.0215,  0.1745,   0.0215 ),   // Ambient
00223     GltColor(0.07568, 0.61424,  0.07568),   // Diffuse
00224     GltColor(0.633,   0.727811, 0.633  ),   // Specular
00225     GltColor(0.00,0.00,0.00),               // Emission
00226     76.8f                                   // Shininess
00227 );
00228 
00230 
00231 const GltMaterial goldMaterial
00232 (
00233     GltColor(0.24725,  0.1995,   0.0745),   // Ambient
00234     GltColor(0.75164,  0.60648,  0.22648),  // Diffuse
00235     GltColor(0.628281, 0.555802, 0.366065), // Specular
00236     GltColor(0.00,0.00,0.00),               // Emission
00237     51.2f                                   // Shininess
00238 );
00239 
00241 
00242 const GltMaterial jadeMaterial
00243 (
00244     GltColor(0.135,    0.2225,   0.1575  ), // Ambient
00245     GltColor(0.135,    0.2225,   0.1575  ), // Diffuse
00246     GltColor(0.316228, 0.316228, 0.316228), // Specular
00247     GltColor(0.00,0.00,0.00),               // Emission
00248     12.8f                                   // Shininess
00249 );
00250 
00252 
00253 const GltMaterial obsidianMaterial
00254 (
00255     GltColor(0.05375,  0.05,     0.06625 ), // Ambient
00256     GltColor(0.18275,  0.17,     0.22525 ), // Diffuse
00257     GltColor(0.332741, 0.328634, 0.346435), // Specular
00258     GltColor(0.00,0.00,0.00),               // Emission
00259     38.4f                                   // Shininess
00260 );
00261 
00263 
00264 const GltMaterial pearlMaterial
00265 (
00266     GltColor(0.25,     0.20725,  0.20725 ), // Ambient
00267     GltColor(1.0,      0.829,    0.829   ), // Diffuse
00268     GltColor(0.296648, 0.296648, 0.296648), // Specular
00269     GltColor(0.00,0.00,0.00),               // Emission
00270     11.264f                                 // Shininess
00271 );
00272 
00274 
00275 const GltMaterial pewterMaterial
00276 (
00277     GltColor(0.105882, 0.058824, 0.113725), // Ambient
00278     GltColor(0.427451, 0.470588, 0.541176), // Diffuse
00279     GltColor(0.333333, 0.333333, 0.521569), // Specular
00280     GltColor(0.00,0.00,0.00),               // Emission
00281     9.84615f                                // Shininess
00282 );
00283 
00285 
00286 const GltMaterial polishedBronzeMaterial
00287 (
00288     GltColor(0.25    , 0.148   , 0.06475 ), // Ambient
00289     GltColor(0.4     , 0.2368  , 0.1036  ), // Diffuse
00290     GltColor(0.774597, 0.458561, 0.200621), // Specular
00291     GltColor(0.0,0.0,0.0),                  // Emission
00292     76.8f                                   // Shininess
00293 );
00294 
00296 
00297 const GltMaterial polishedCopperMaterial
00298 (
00299     GltColor(0.2295,   0.08825,  0.0275   ),    // Ambient
00300     GltColor(0.5508,   0.2118,   0.066    ),    // Diffuse
00301     GltColor(0.580594, 0.223257, 0.0695701),    // Specular
00302     GltColor(0.0,0.0,0.0),                      // Emission
00303     51.2f                                       // Shininess
00304 );
00305 
00307 
00308 const GltMaterial polishedGoldMaterial
00309 (
00310     GltColor(0.24725,  0.2245,   0.0645  ), // Ambient
00311     GltColor(0.34615,  0.3143,   0.0903  ), // Diffuse
00312     GltColor(0.797357, 0.723991, 0.208006), // Specular
00313     GltColor(0.0,0.0,0.0),                  // Emission
00314     83.2f                                   // Shininess
00315 );
00316 
00318 
00319 const GltMaterial polishedSilverMaterial
00320 (
00321     GltColor(0.23125, 0.23125, 0.23125),    // Ambient
00322     GltColor(0.2775,  0.2775,  0.2775),     // Diffuse
00323     GltColor(0.773911, 0.773911, 0.773911), // Specular
00324     GltColor(0.0,0.0,0.0),                  // Emission
00325     89.6f                                   // Shininess
00326 );
00327 
00329 
00330 const GltMaterial rubyMaterial
00331 (
00332     GltColor(0.1745,   0.01175,  0.01175),  // Ambient
00333     GltColor(0.61424,  0.04136,  0.04136),  // Diffuse
00334     GltColor(0.727811, 0.626959, 0.626959), // Specular
00335     GltColor(0.00,0.00,0.00),               // Emission
00336     76.8f                                   // Shininess
00337 );
00338 
00340 
00341 const GltMaterial silverMaterial
00342 (
00343     GltColor(0.19225,  0.19225,  0.19225 ), // Ambient
00344     GltColor(0.50754,  0.50754,  0.50754 ), // Diffuse
00345     GltColor(0.508273, 0.508273, 0.508273), // Specular
00346     GltColor(0.00,0.00,0.00),               // Emission
00347     51.2f                                   // Shininess
00348 );
00349 
00351 
00352 const GltMaterial turquoiseMaterial
00353 (
00354     GltColor(0.1,      0.18725, 0.1745  ),  // Ambient
00355     GltColor(0.396,    0.74151, 0.69102 ),  // Diffuse
00356     GltColor(0.297254, 0.30829, 0.306678),  // Specular
00357     GltColor(0.00,0.00,0.00),               // Emission
00358     12.8f                                   // Shininess
00359 );
00360 
00362 
00363 const GltMaterial whitePlasticMaterial
00364 (
00365     GltColor(0.00, 0.00, 0.00), // Ambient
00366     GltColor(0.55, 0.55, 0.55), // Diffuse
00367     GltColor(0.70, 0.70, 0.70), // Specular
00368     GltColor(0.00, 0.00, 0.00), // Emission
00369     32.0f                       // Shininess
00370 );
00371 
00372 //
00373 
00374 const int   GltMaterial::_matSize = 20;
00375 
00376 const char *GltMaterial::_matName[20] =
00377 {
00378     "blackPlastic",
00379     "blackRubber",
00380     "brass",
00381     "bronze",
00382     "chrome",
00383     "copper",
00384     "emerald",
00385     "gold",
00386     "jade",
00387     "obsidian",
00388     "pearl",
00389     "pewter",
00390     "polishedBronze",
00391     "polishedCopper",
00392     "polishedGold",
00393     "polishedSilver",
00394     "ruby",
00395     "silver",
00396     "turquoise",
00397     "whitePlastic"
00398 };
00399 
00400 const GltMaterial *GltMaterial::_matValue[20] =
00401 {
00402     &::blackPlasticMaterial,
00403     &::blackRubberMaterial,
00404     &::brassMaterial,
00405     &::bronzeMaterial,
00406     &::chromeMaterial,
00407     &::copperMaterial,
00408     &::emeraldMaterial,
00409     &::goldMaterial,
00410     &::jadeMaterial,
00411     &::obsidianMaterial,
00412     &::pearlMaterial,
00413     &::pewterMaterial,
00414     &::polishedBronzeMaterial,
00415     &::polishedCopperMaterial,
00416     &::polishedGoldMaterial,
00417     &::polishedSilverMaterial,
00418     &::rubyMaterial,
00419     &::silverMaterial,
00420     &::turquoiseMaterial,
00421     &::whitePlasticMaterial
00422 };

Generated on Tue Nov 5 11:11:03 2002 for GLT by doxygen1.2.18