From: Mikko Rasa Date: Wed, 24 Sep 2008 16:10:43 +0000 (+0000) Subject: Object model for Material and TexEnv X-Git-Tag: 1.0~5 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=50e504e2171295d5922ddf87b358e0024db3ce40;p=libs%2Fgl.git Object model for Material and TexEnv --- diff --git a/source/material.cpp b/source/material.cpp index 0b888ef5..310834f3 100644 --- a/source/material.cpp +++ b/source/material.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -44,15 +44,26 @@ 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) diff --git a/source/material.h b/source/material.h index b0c9cd0d..1da27199 100644 --- a/source/material.h +++ b/source/material.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -56,7 +56,11 @@ public: const Color &get_specular() const { return specular; } const Color &get_emission() const { return emission; } float get_shininess() const { return shininess; } - void apply() const; + void bind() const; + + static void unbind(); +private: + static const Material *current; }; } // namespace GL diff --git a/source/object.cpp b/source/object.cpp index 78f44557..db286611 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -78,7 +78,7 @@ void Object::setup_render(const ObjectPass &pass) const main_texture->bind(); if(material) - material->apply(); + material->bind(); } void Object::finish_render(const ObjectPass &pass) const @@ -94,6 +94,9 @@ void Object::finish_render(const ObjectPass &pass) const } else if(main_texture) Texture::unbind(); + + if(material) + Material::unbind(); } void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const diff --git a/source/texenv.cpp b/source/texenv.cpp new file mode 100644 index 00000000..6e5cca77 --- /dev/null +++ b/source/texenv.cpp @@ -0,0 +1,43 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include "texenv.h" +#include "texunit.h" + +namespace Msp { +namespace GL { + +TexEnv::TexEnv(): + mode(MODULATE) +{ } + +void TexEnv::set_mode(TexEnvMode m) +{ + mode=m; +} + +void TexEnv::set_color(const Color &c) +{ + color=c; +} + +void TexEnv::bind() +{ + if(TexUnit::current().set_texenv(this)) + { + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode); + glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &color.r); + } +} + +void TexEnv::unbind() +{ + TexUnit::current().set_texenv(0); +} + +} // namespace GL +} // namespace Msp diff --git a/source/texenv.h b/source/texenv.h index 6d0eba9f..b5fee448 100644 --- a/source/texenv.h +++ b/source/texenv.h @@ -1,13 +1,16 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #ifndef MSP_GL_TEXENV_H_ #define MSP_GL_TEXENV_H_ +#include "color.h" +#include "gl.h" + namespace Msp { namespace GL { @@ -23,9 +26,19 @@ enum TexEnvMode class TexEnv { +private: + TexEnvMode mode; + Color color; + public: - void mode(TexEnvMode); - void color(float, float, float, float); + TexEnv(); + void set_mode(TexEnvMode); + void set_color(const Color &); + TexEnvMode get_mode() const { return mode; } + const Color &get_color() const { return color; } + void bind(); + + static void unbind(); }; } // namespace GL diff --git a/source/texunit.cpp b/source/texunit.cpp index 7aced5cc..622599a2 100644 --- a/source/texunit.cpp +++ b/source/texunit.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -27,6 +27,13 @@ bool TexUnit::set_texture(const Texture *tex) return result; } +bool TexUnit::set_texenv(const TexEnv *env) +{ + bool result=(texenv!=env); + texenv=env; + return result; +} + TexUnit &TexUnit::activate(unsigned n) { if(units.size()<=n) diff --git a/source/texunit.h b/source/texunit.h index 0bdb2159..1b616505 100644 --- a/source/texunit.h +++ b/source/texunit.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -13,6 +13,7 @@ Distributed under the LGPL namespace Msp { namespace GL { +class TexEnv; class Texture; class TexUnit @@ -20,14 +21,15 @@ class TexUnit public: TexUnit(); bool set_texture(const Texture *); - const Texture *get_texture() { return texture; } - //TexEnv &get_env() { return env; } + const Texture *get_texture() const { return texture; } + bool set_texenv(const TexEnv *); + const TexEnv *get_texenv() const { return texenv; } static TexUnit &activate(unsigned); static TexUnit ¤t(); private: const Texture *texture; - //TexEnv env; + const TexEnv *texenv; static std::vector units; static TexUnit *cur_unit;