]> git.tdb.fi Git - libs/gl.git/blobdiff - source/material.cpp
Better state tracking for bound objects
[libs/gl.git] / source / material.cpp
index b5ec6a5cd913209940acdd68558f1dc072fda8aa..d6e21683d2a7f3b8fb08498b269419d386d7d9d7 100644 (file)
@@ -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<Material>(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