]> git.tdb.fi Git - libs/gl.git/blobdiff - source/material.cpp
Fix some incorrect whitespace
[libs/gl.git] / source / material.cpp
index 58cd15cb8e122169803ce5015be54681df948173..d6e21683d2a7f3b8fb08498b269419d386d7d9d7 100644 (file)
@@ -1,10 +1,4 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include "gl.h"
 #include "material.h"
 
 namespace Msp {
@@ -18,38 +12,88 @@ 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(set_current(this))
+               update_parameter(-1);
+}
+
+
+Material::Loader::Loader(Material &m):
+       DataFile::ObjectLoader<Material>(m)
+{
+       add("ambient",   &Loader::ambient);
+       add("diffuse",   &Loader::diffuse);
+       add("specular",  &Loader::specular);
+       add("emission",  &Loader::emission);
+       add("shininess", &Material::shininess);
+}
+
+void Material::Loader::ambient(float r, float g, float b, float 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);
+}
+
+void Material::Loader::specular(float r, float g, float b, float a)
+{
+       obj.specular = GL::Color(r, g, b, a);
 }
 
-void Material::apply() const
+void Material::Loader::emission(float r, float g, float b, float a)
 {
-       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);
+       obj.emission = GL::Color(r, g, b, a);
 }
 
 } // namespace GL