From: Mikko Rasa Date: Sun, 15 Dec 2013 23:16:31 +0000 (+0200) Subject: Create ProgramData for materials and lights X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=a6acc6fc98f8571eaaa66f726c1ff4d60abe4f58;p=libs%2Fgl.git Create ProgramData for materials and lights --- diff --git a/source/light.cpp b/source/light.cpp index d4b45aaf..11f4826c 100644 --- a/source/light.cpp +++ b/source/light.cpp @@ -1,7 +1,10 @@ #include +#include #include "light.h" #include "lightunit.h" +#include "matrix.h" #include "misc.h" +#include "programdata.h" using namespace std; @@ -97,6 +100,14 @@ void Light::set_attenuation(float c, float l, float q) update_parameter(ATTENUATION); } +void Light::update_shader_data(ProgramData &shdata, const Matrix &view_matrix, unsigned i) const +{ + string base = format("light_sources[%d]", i); + shdata.uniform(base+".position", view_matrix*position); + shdata.uniform(base+".diffuse", diffuse); + shdata.uniform(base+".specular", specular); +} + void Light::bind_to(unsigned i) const { LightUnit &unit = LightUnit::get_unit(i); diff --git a/source/light.h b/source/light.h index 1c6a5227..77542a54 100644 --- a/source/light.h +++ b/source/light.h @@ -8,6 +8,9 @@ namespace Msp { namespace GL { +class Matrix; +class ProgramData; + class Light { private: @@ -53,6 +56,7 @@ public: void set_attenuation(float, float, float); const float *get_attenuation() const { return attenuation; } + void update_shader_data(ProgramData &, const Matrix &, unsigned) const; void bind() const { return bind_to(0); } void bind_to(unsigned) const; diff --git a/source/lighting.cpp b/source/lighting.cpp index 517530a2..515733a6 100644 --- a/source/lighting.cpp +++ b/source/lighting.cpp @@ -41,6 +41,14 @@ void Lighting::detach(unsigned i) Light::unbind_from(i); } +void Lighting::update_shader_data(const Matrix &view_matrix) +{ + shdata.uniform("ambient_color", ambient); + for(unsigned i=0; iupdate_shader_data(shdata, view_matrix, i); +} + void Lighting::bind() const { if(!set_current(this)) diff --git a/source/lighting.h b/source/lighting.h index 794fb75a..7c7dc454 100644 --- a/source/lighting.h +++ b/source/lighting.h @@ -5,6 +5,7 @@ #include "bindable.h" #include "color.h" #include "gl.h" +#include "programdata.h" namespace Msp { namespace GL { @@ -19,6 +20,7 @@ class Lighting: public Bindable private: Color ambient; std::vector lights; + ProgramData shdata; public: Lighting(); @@ -28,6 +30,9 @@ public: void attach(unsigned, const Light &); void detach(unsigned); + + void update_shader_data(const Matrix &); + const ProgramData &get_shader_data() const { return shdata; } void bind() const; static void unbind(); diff --git a/source/material.cpp b/source/material.cpp index d6e21683..7d90dd5a 100644 --- a/source/material.cpp +++ b/source/material.cpp @@ -4,13 +4,14 @@ namespace Msp { namespace GL { -Material::Material(): - ambient(0.2), - diffuse(0.8), - specular(0), - emission(0), - shininess(0) -{ } +Material::Material() +{ + set_ambient(0.2); + set_diffuse(0.8); + set_specular(0); + set_emission(0); + set_shininess(0); +} void Material::update_parameter(int mask) const { @@ -32,30 +33,35 @@ void Material::update_parameter(int mask) const void Material::set_ambient(const Color &a) { ambient = a; + shdata.uniform("material.ambient", ambient); update_parameter(AMBIENT); } void Material::set_diffuse(const Color &d) { diffuse = d; + shdata.uniform("material.diffuse", diffuse); update_parameter(DIFFUSE); } void Material::set_specular(const Color &s) { specular = s; + shdata.uniform("material.specular", specular); update_parameter(SPECULAR); } void Material::set_emission(const Color &e) { emission = e; + shdata.uniform("material.emission", emission); update_parameter(EMISSION); } void Material::set_shininess(float s) { shininess = s; + shdata.uniform("material.shininess", shininess); update_parameter(SHININESS); } @@ -73,27 +79,32 @@ Material::Loader::Loader(Material &m): add("diffuse", &Loader::diffuse); add("specular", &Loader::specular); add("emission", &Loader::emission); - add("shininess", &Material::shininess); + add("shininess", &Loader::shininess); } void Material::Loader::ambient(float r, float g, float b, float a) { - obj.ambient = GL::Color(r, g, b, a); + obj.set_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.set_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.set_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.set_emission(GL::Color(r, g, b, a)); +} + +void Material::Loader::shininess(float s) +{ + obj.set_shininess(s); } } // namespace GL diff --git a/source/material.h b/source/material.h index f2446368..5a71aa34 100644 --- a/source/material.h +++ b/source/material.h @@ -4,6 +4,7 @@ #include #include "bindable.h" #include "color.h" +#include "programdata.h" namespace Msp { namespace GL { @@ -25,6 +26,7 @@ public: void diffuse(float, float, float, float); void specular(float, float, float, float); void emission(float, float, float, float); + void shininess(float); }; private: @@ -42,6 +44,7 @@ private: Color specular; Color emission; float shininess; + ProgramData shdata; public: Material(); @@ -60,6 +63,7 @@ public: const Color &get_specular() const { return specular; } const Color &get_emission() const { return emission; } float get_shininess() const { return shininess; } + const ProgramData &get_shader_data() const { return shdata; } void bind() const; };