]> git.tdb.fi Git - libs/gl.git/blobdiff - source/material.cpp
Inherit Loaders from the ObjectLoader classes
[libs/gl.git] / source / material.cpp
index f165c744fcdf06277c362977954b800c06e6e6df..b5ec6a5cd913209940acdd68558f1dc072fda8aa 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 {
@@ -43,18 +44,29 @@ void Material::set_shininess(float s)
        shininess=s;
 }
 
-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(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):
-       mat(m)
+       DataFile::ObjectLoader<Material>(m)
 {
        add("ambient",   &Loader::ambient);
        add("diffuse",   &Loader::diffuse);
@@ -65,22 +77,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