]> git.tdb.fi Git - libs/gl.git/commitdiff
Support hemispherical skylight in ProgramBuilder
authorMikko Rasa <tdb@tdb.fi>
Sun, 13 Jul 2014 13:10:59 +0000 (16:10 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 13 Jul 2014 13:10:59 +0000 (16:10 +0300)
OpenGL's fixed-function pipeline does not have any similar feature, so
this can't be supported without shaders.

source/lighting.cpp
source/lighting.h
source/programbuilder.cpp
source/programbuilder.h

index f5fbca6ac7ac954c5a2c63cd75590557e4245660..2ba1edc5560e00889ecd31fa01592f121a68a993 100644 (file)
@@ -2,6 +2,7 @@
 #include "light.h"
 #include "lighting.h"
 #include "lightunit.h"
+#include "matrix.h"
 #include "misc.h"
 
 using namespace std;
@@ -10,7 +11,9 @@ namespace Msp {
 namespace GL {
 
 Lighting::Lighting():
-       ambient(0.2)
+       ambient(0.2),
+       sky_direction(0, 0, 1),
+       horizon_angle(Geometry::Angle<float>::zero())
 { }
 
 void Lighting::set_ambient(const Color &a)
@@ -18,6 +21,21 @@ void Lighting::set_ambient(const Color &a)
        ambient = a;
 }
 
+void Lighting::set_sky_color(const Color &s)
+{
+       sky_color = s;
+}
+
+void Lighting::set_sky_direction(const Vector3 &d)
+{
+       sky_direction = d;
+}
+
+void Lighting::set_horizon_angle(const Geometry::Angle<float> &a)
+{
+       horizon_angle = a;
+}
+
 void Lighting::attach(unsigned i, const Light &l)
 {
        if(i>=LightUnit::get_n_units())
@@ -44,6 +62,10 @@ void Lighting::detach(unsigned i)
 void Lighting::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
 {
        shdata.uniform("ambient_color", ambient);
+       shdata.uniform("sky_color", sky_color);
+       shdata.uniform("eye_sky_dir", Vector3(view_matrix*Vector4(sky_direction, 0.0f)));
+       shdata.uniform("horizon_limit", horizon_angle.radians());
+
        for(unsigned i=0; i<lights.size(); ++i)
                if(lights[i])
                        lights[i]->update_shader_data(shdata, view_matrix, i);
index 593d8039fe5c8365269b757f963173b14289cd51..063bbf2b4eb5a5f81bb75a6931ba5833d603b377 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_LIGHTING_H_
 
 #include <vector>
+#include <msp/geometry/angle.h>
 #include "bindable.h"
 #include "color.h"
 #include "gl.h"
@@ -19,6 +20,9 @@ class Lighting: public Bindable<Lighting>
 {
 private:
        Color ambient;
+       Color sky_color;
+       Vector3 sky_direction;
+       Geometry::Angle<float> horizon_angle;
        std::vector<const Light *> lights;
 
 public:
@@ -27,6 +31,10 @@ public:
        void set_ambient(const Color &);
        const Color &get_ambient() const { return ambient; }
 
+       void set_sky_color(const Color &);
+       void set_sky_direction(const Vector3 &);
+       void set_horizon_angle(const Geometry::Angle<float> &);
+
        void attach(unsigned, const Light &);
        void detach(unsigned);
 
index 8e4d13855314b383033175be8a2e6a7b47971735..91575d0dfe436b9e1aa794496303db8fa8d71f75 100644 (file)
@@ -71,12 +71,17 @@ const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
 
        { FRAGMENT, "rgb_surface_ambient", "vec3", "rgb_illumination_ambient*material.ambient.rgb", "!t" },
        { FRAGMENT, "rgb_surface_ambient", "vec3", "rgb_illumination_ambient*material.ambient.rgb*tex_sample.rgb", "t" },
-       { FRAGMENT, "rgb_illumination_ambient", "vec3", "ambient_color.rgb", 0 },
+       { FRAGMENT, "rgb_illumination_ambient", "vec3", "ambient_color.rgb", "!y" },
+       { FRAGMENT, "rgb_illumination_ambient", "vec3", "ambient_color.rgb+l_skylight*sky_color.rgb", "y" },
+       { FRAGMENT, "l_skylight", "float", "dot(n_zzz_normal, zzz_sky_dir)*0.5+0.5", 0 },
 
        { FRAGMENT, "rgb_surface_specular", "vec3", "rgb_illumination_specular*material.specular.rgb", 0 },
-       { FRAGMENT, "rgb_illumination_specular", "vec3", "rgb_light_specular", "!s" },
-       { FRAGMENT, "rgb_illumination_specular", "vec3", "rgb_light_specular*l_shadow", "s" },
+       { FRAGMENT, "rgb_illumination_specular", "vec3", "rgb_illumination_specular_direct", "!y|e" },
+       { FRAGMENT, "rgb_illumination_specular", "vec3", "rgb_illumination_specular_direct+l_sky_specular*sky_color.rgb", "y!e" },
+       { FRAGMENT, "rgb_illumination_specular_direct", "vec3", "rgb_light_specular", "!s" },
+       { FRAGMENT, "rgb_illumination_specular_direct", "vec3", "rgb_light_specular*l_shadow", "s" },
        { FRAGMENT, "rgb_light_specular[i]", "vec3", "l_specular[i]*light_sources[i].specular.rgb", 0 },
+       { FRAGMENT, "l_sky_specular", "float", "pow((1.0-pow(clamp(dot(zzz_reflect_dir, zzz_sky_dir)-horizon_limit, -1.0, 0.0), 2.0)), material.shininess/2.0)", 0 },
        { FRAGMENT, "l_specular[i]", "float", "pow(max(dot(n_zzz_half_vec[i], n_zzz_normal), 0.0), material.shininess)", 0 },
 
        { FRAGMENT, "rgb_surface_diffuse", "vec3", "rgb_illumination_diffuse*material.diffuse.rgb", "!t" },
@@ -100,6 +105,7 @@ const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
        { VERTEX, "gl_Position", "vec4", "projection_matrix*eye_vertex", 0 },
        { VERTEX, "shd_vertex", "vec3", "vec3(dot(eye_vertex, gl_EyePlaneS[shadow_unit]), dot(eye_vertex, gl_EyePlaneT[shadow_unit]), dot(eye_vertex, gl_EyePlaneR[shadow_unit]))", "g" },
        { VERTEX, "shd_vertex", "vec3", "(shd_eye_matrix*eye_vertex).xyz", "!g" },
+       { VERTEX, "tbn_sky_dir", "vec3", "eye_sky_dir*eye_tbn_matrix", "n" },
        { VERTEX, "tbn_light_dir[i]", "vec3", "eye_light_dir[i]*eye_tbn_matrix", 0 },
        { VERTEX, "eye_light_dir[i]", "vec3", "normalize(eye_light_position[i].xyz-eye_vertex.xyz*eye_light_position[i].w)", 0 },
        { VERTEX, "eye_light_position[i]", "vec4", "light_sources[i].position", 0 },
@@ -133,6 +139,9 @@ const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
        { UNIFORM, "shd_eye_matrix", "mat4", 0, 0 },
        { UNIFORM, "light_sources", "struct { vec4 position; vec4 diffuse; vec4 specular; }[MAX_LIGHTS]", "gl_LightSource[i]", 0 },
        { UNIFORM, "ambient_color", "vec4", 0, 0 },
+       { UNIFORM, "sky_color", "vec4", 0, 0 },
+       { UNIFORM, "eye_sky_dir", "vec3", 0, 0 },
+       { UNIFORM, "horizon_limit", "float", 0, 0 },
        { UNIFORM, "material", "struct { vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }", "gl_FrontMaterial", 0 },
 
        // Terminator entry
@@ -616,6 +625,8 @@ string ProgramBuilder::StandardFeatures::create_flags() const
        if(lighting)
        {
                flags += 'l';
+               if(skylight)
+                       flags += 'y';
                if(specular)
                        flags += 'p';
                if(normalmap)
@@ -932,6 +943,7 @@ ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
        add("normalmap", &StandardFeatures::normalmap);
        add("reflection", &StandardFeatures::reflection);
        add("shadow",    &StandardFeatures::shadow);
+       add("skylight",  &StandardFeatures::skylight);
        add("specular",  &StandardFeatures::specular);
        add("texture",   &StandardFeatures::texture);
 }
index 678ee2ca142d9b31e264d2abad9d33ef9fbb31d2..0883f6a74fd07a6479dc1b01fa62424882455042 100644 (file)
@@ -39,6 +39,7 @@ public:
                bool material;
                bool lighting;
                unsigned max_lights;
+               bool skylight;
                bool specular;
                bool normalmap;
                bool shadow;