From: Mikko Rasa Date: Sun, 13 Jul 2014 13:10:59 +0000 (+0300) Subject: Support hemispherical skylight in ProgramBuilder X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=70663ae97d5b70a6b468e07dd7e73475a175dc9b Support hemispherical skylight in ProgramBuilder OpenGL's fixed-function pipeline does not have any similar feature, so this can't be supported without shaders. --- diff --git a/source/lighting.cpp b/source/lighting.cpp index f5fbca6a..2ba1edc5 100644 --- a/source/lighting.cpp +++ b/source/lighting.cpp @@ -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::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 &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; iupdate_shader_data(shdata, view_matrix, i); diff --git a/source/lighting.h b/source/lighting.h index 593d8039..063bbf2b 100644 --- a/source/lighting.h +++ b/source/lighting.h @@ -2,6 +2,7 @@ #define MSP_GL_LIGHTING_H_ #include +#include #include "bindable.h" #include "color.h" #include "gl.h" @@ -19,6 +20,9 @@ class Lighting: public Bindable { private: Color ambient; + Color sky_color; + Vector3 sky_direction; + Geometry::Angle horizon_angle; std::vector 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 &); + void attach(unsigned, const Light &); void detach(unsigned); diff --git a/source/programbuilder.cpp b/source/programbuilder.cpp index 8e4d1385..91575d0d 100644 --- a/source/programbuilder.cpp +++ b/source/programbuilder.cpp @@ -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); } diff --git a/source/programbuilder.h b/source/programbuilder.h index 678ee2ca..0883f6a7 100644 --- a/source/programbuilder.h +++ b/source/programbuilder.h @@ -39,6 +39,7 @@ public: bool material; bool lighting; unsigned max_lights; + bool skylight; bool specular; bool normalmap; bool shadow;