X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmaterial.cpp;h=d6e21683d2a7f3b8fb08498b269419d386d7d9d7;hb=27fe0982fc18d1f5f9efe942e18c42f508268f34;hp=0b888ef519404e164ac32be278f3593fb85edc0e;hpb=a4ec5410595ddf37bfbc0e85ad87d31a9cbf94f1;p=libs%2Fgl.git diff --git a/source/material.cpp b/source/material.cpp index 0b888ef5..d6e21683 100644 --- a/source/material.cpp +++ b/source/material.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include "gl.h" #include "material.h" @@ -19,43 +12,62 @@ 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::apply() const +void Material::bind() const { - 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); + if(set_current(this)) + update_parameter(-1); } Material::Loader::Loader(Material &m): - mat(m) + DataFile::ObjectLoader(m) { add("ambient", &Loader::ambient); add("diffuse", &Loader::diffuse); @@ -66,22 +78,22 @@ Material::Loader::Loader(Material &m): void Material::Loader::ambient(float r, float g, float b, float a) { - mat.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) { - mat.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) { - mat.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) { - mat.emission=GL::Color(r, g, b, a); + obj.emission = GL::Color(r, g, b, a); } } // namespace GL