]> git.tdb.fi Git - libs/gl.git/blobdiff - source/material.cpp
Style update: add spaces around assignment operators
[libs/gl.git] / source / material.cpp
index 25c21ff21988b3aed6a00419cb4dcc9ec2450a9f..eee1ebc6152057d8799db1a118fa3dcda2bfcd2c 100644 (file)
@@ -1,10 +1,11 @@
 /* $Id$
 
 This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2008  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include "gl.h"
 #include "material.h"
 
 namespace Msp {
@@ -20,36 +21,78 @@ Material::Material():
 
 void Material::set_ambient(const Color &a)
 {
-       ambient=a;
+       ambient = a;
 }
 
 void Material::set_diffuse(const Color &d)
 {
-       diffuse=d;
+       diffuse = d;
 }
 
 void Material::set_specular(const Color &s)
 {
-       specular=s;
+       specular = s;
 }
 
 void Material::set_emission(const Color &e)
 {
-       emission=e;
+       emission = e;
 }
 
 void Material::set_shininess(float s)
 {
-       shininess=s;
+       shininess = s;
 }
 
-void Material::apply()
+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(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;
+}
+
+const Material *Material::current = 0;
+
+
+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::Loader::emission(float r, float g, float b, float a)
+{
+       obj.emission = GL::Color(r, g, b, a);
 }
 
 } // namespace GL