X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmaterial.cpp;h=d6e21683d2a7f3b8fb08498b269419d386d7d9d7;hb=6924ea10c4111b11eab51f0e1aa5b4a6438da7d3;hp=b5ec6a5cd913209940acdd68558f1dc072fda8aa;hpb=927a1aa0a3a27e463ec0efc08bd08e7c4e969909;p=libs%2Fgl.git diff --git a/source/material.cpp b/source/material.cpp index b5ec6a5c..d6e21683 100644 --- a/source/material.cpp +++ b/source/material.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include "gl.h" #include "material.h" @@ -19,51 +12,59 @@ Material::Material(): shininess(0) { } +void Material::update_parameter(int mask) const +{ + if(cur_obj!=this) + return; + + if(mask&AMBIENT) + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r); + if(mask&DIFFUSE) + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r); + if(mask&SPECULAR) + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r); + if(mask&EMISSION) + glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &emission.r); + if(mask&SHININESS) + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess); +} + void Material::set_ambient(const Color &a) { - ambient=a; + ambient = a; + update_parameter(AMBIENT); } void Material::set_diffuse(const Color &d) { - diffuse=d; + diffuse = d; + update_parameter(DIFFUSE); } void Material::set_specular(const Color &s) { - specular=s; + specular = s; + update_parameter(SPECULAR); } void Material::set_emission(const Color &e) { - emission=e; + emission = e; + update_parameter(EMISSION); } void Material::set_shininess(float s) { - shininess=s; + shininess = s; + update_parameter(SHININESS); } void Material::bind() const { - if(current!=this) - { - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r); - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r); - glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &emission.r); - glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess); - current=this; - } -} - -void Material::unbind() -{ - current=0; + if(set_current(this)) + update_parameter(-1); } -const Material *Material::current=0; - Material::Loader::Loader(Material &m): DataFile::ObjectLoader(m) @@ -77,22 +78,22 @@ Material::Loader::Loader(Material &m): void Material::Loader::ambient(float r, float g, float b, float a) { - obj.ambient=GL::Color(r, g, b, a); + obj.ambient = GL::Color(r, g, b, a); } void Material::Loader::diffuse(float r, float g, float b, float a) { - obj.diffuse=GL::Color(r, g, b, a); + obj.diffuse = GL::Color(r, g, b, a); } void Material::Loader::specular(float r, float g, float b, float a) { - obj.specular=GL::Color(r, g, b, a); + obj.specular = GL::Color(r, g, b, a); } void Material::Loader::emission(float r, float g, float b, float a) { - obj.emission=GL::Color(r, g, b, a); + obj.emission = GL::Color(r, g, b, a); } } // namespace GL