added IBR lighting from irradiance maps
authorEleni Maria Stea <estea@igalia.com>
Thu, 24 Aug 2017 00:17:19 +0000 (03:17 +0300)
committerEleni Maria Stea <estea@igalia.com>
Thu, 24 Aug 2017 00:17:19 +0000 (03:17 +0300)
(diffuse only, TODO: add specular)

gl_shaders/default.f.glsl
gl_shaders/default.v.glsl
gl_shaders/morphing.f.glsl
gl_shaders/morphing.v.glsl
src/main.cc
src/morph_renderer.cc
src/renderer.cc
src/renderer.h

index f0f61d8..ad4b77a 100644 (file)
@@ -1,15 +1,17 @@
 #version 450
 
 uniform sampler2D tex;
+uniform samplerCube dstex;
 
 uniform vec4 diffuse;
 uniform vec4 specular;
 uniform float shininess;
 
 varying vec3 pos;
-varying vec3 normal;
-varying vec3 ldir;
+// varying vec3 normal;
+// varying vec3 ldir;
 varying vec2 tex_coord;
+varying vec3 world_normal;
 
 // const float fog_density = 0.005;
 uniform float fog_density;
@@ -19,7 +21,9 @@ out vec4 color;
 
 void main()
 {
-       vec3 p = normalize(pos); // view space dir
+       vec4 itexel = textureCube(dstex, normalize(world_normal));
+
+/*     vec3 p = normalize(pos); // view space dir
        vec3 n = normalize(normal);
        vec3 l = normalize(ldir);
 
@@ -27,14 +31,15 @@ void main()
        vec3 vdir = normalize(-p);
 
        float cdiff = max(dot(l, n), 0.0);
-       float cspec = pow(max(dot(r, vdir), 0.0), shininess);
+       float cspec = pow(max(dot(r, vdir), 0.0), shininess); */
 
        float dist = -pos.z;
        float fog = clamp(exp(-fog_density * dist), 0.0, 1.0);
 
        vec4 texel = texture2D(tex, tex_coord);
 
-       vec3 object_color = diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec;
+       // vec3 object_color = (diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec) * itexel.xyz;
+       vec3 object_color = diffuse.xyz * texel.xyz * itexel.xyz;
 
        color.xyz = mix(sky_color, object_color, fog);
        color.w = 1.0;
index 455759d..76aae8a 100644 (file)
@@ -3,11 +3,13 @@
 
 uniform mat4 mview;
 uniform mat4 mmviewproj;
+uniform mat4 mmod;
 
 varying vec3 pos;
-varying vec3 normal;
-varying vec3 ldir;
+// varying vec3 normal;
+// varying vec3 ldir;
 varying vec2 tex_coord;
+varying vec3 world_normal;
 
 const vec3 lpos = vec3(-10.0, 100.0, 10.0);
 
@@ -21,9 +23,11 @@ void main()
        gl_Position = mmviewproj * vec4(attr_pos, 1.0);
 
        pos = (mview * vec4(attr_pos, 1.0)).xyz;
-       ldir = (mview * vec4(lpos, 1.0)).xyz;
+       // ldir = (mview * vec4(lpos, 1.0)).xyz;
 
-       mat3 normal_matrix = mat3(mview);
-       normal = normal_matrix * attr_normal;
+       // mat3 normal_matrix = mat3(mview);
+       // normal = normal_matrix * attr_normal;
        tex_coord = attr_tex;
+
+       world_normal = (mmod * vec4(attr_normal, 1.0)).xyz;
 }
\ No newline at end of file
index df74c90..ddb0579 100644 (file)
@@ -1,36 +1,39 @@
 #version 450
 
 uniform sampler2D tex;
+uniform samplerCube dstex;
 
 uniform vec4 diffuse;
-uniform vec4 specular;
-uniform float shininess;
+// uniform vec4 specular;
+// uniform float shininess;
 
 uniform float fog_density;
 const vec3 sky_color = vec3(0.35, 0.5, 0.65);
 
 varying vec3 pos;
-varying vec3 normal;
-varying vec3 ldir;
+// varying vec3 normal;
+// varying vec3 ldir;
 varying vec2 tex_coord;
+varying vec3 world_normal;
 
 out vec4 color;
 
 void main()
 {
-       vec3 p = normalize(pos);
-       vec3 n = normalize(normal);
-       vec3 l = normalize(ldir);
+       vec4 itexel = textureCube(dstex, normalize(world_normal));
+       // vec3 p = normalize(pos);
+       // vec3 n = normalize(normal);
+       // vec3 l = normalize(ldir);
 
-       vec3 r = normalize(-reflect(l, n));
-       vec3 vdir = normalize(-p);
+       // vec3 r = normalize(-reflect(l, n));
+       // vec3 vdir = normalize(-p);
 
-       float cdiff = max(dot(l, n), 0.0);
-       float cspec = pow(max(dot(r, vdir), 0.0), shininess);
+       // float cdiff = max(dot(l, n), 0.0);
+       // float cspec = pow(max(dot(r, vdir), 0.0), shininess);
 
        vec4 texel = texture2D(tex, tex_coord);
-       vec3 object_color = diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec;
-
+       // vec3 object_color = diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec;
+       vec3 object_color = diffuse.xyz * texel.xyz * itexel.xyz;
        float dist = -pos.z;
        float fog = clamp(exp(-fog_density * dist), 0.0, 1.0);
 
index 6f72aa7..d84336f 100644 (file)
@@ -2,16 +2,18 @@
 
 uniform mat4 mview;
 uniform mat4 mmviewproj;
+uniform mat4 mmod;
 
 uniform float t;
 const float half_height = 0.855;
 
 varying vec3 pos;
-varying vec3 normal;
-varying vec3 ldir;
+// varying vec3 normal;
+// varying vec3 ldir;
 varying vec2 tex_coord;
+varying vec3 world_normal;
 
-const vec3 lpos = vec3(0.0, 100.0, -10.0);
+// const vec3 lpos = vec3(0.0, 100.0, -10.0);
 
 /* attributes */
 layout(location = 1) in vec3 attr_pos;
@@ -31,10 +33,11 @@ void main()
        gl_Position = mmviewproj * vec4(p, 1.0);
 
        pos = (mview * vec4(p, 1.0)).xyz;
-       ldir = (mview * vec4(lpos, 1.0)).xyz;
+       // ldir = (mview * vec4(lpos, 1.0)).xyz;
 
-       mat3 normal_matrix = mat3(mview);
-       normal = normal_matrix * n;
+       // mat3 normal_matrix = mat3(mview);
+       // normal = normal_matrix * n;
 
        tex_coord = attr_tex;
+       world_normal = (mmod * vec4(attr_normal, 1.0)).xyz;
 }
\ No newline at end of file
index cbe6ed6..491dd9c 100644 (file)
@@ -69,6 +69,7 @@ static MorphRenderer *cow_rend;
 static Terrain terrain;
 static TerrainParams p;
 static Texture *skybox_tex;
+static Texture *irradiance_tex;
 static Texture *terrain_tex;
 static Material terrain_mat;
 static Renderer *terrain_rend;
@@ -170,9 +171,12 @@ static bool init(Gfx_API api)
 
        skybox_tex = gfx_create_texture();
        skybox_tex->load("data/cubemap/cubemap.hdr");
-       //irr_tex->load("data/cubemap/irradiance.hdr");
        terrain_rend->set_sky_tex(skybox_tex);
 
+       irradiance_tex = gfx_create_texture();
+       irradiance_tex->load("data/cubemap/irradiance.hdr");
+       terrain_rend->set_diffuse_sky_tex(irradiance_tex);
+
        if(!terrain_rend->create()) {
                fprintf(stderr, "terrain fail\n");
                return false;
@@ -230,8 +234,11 @@ static void cleanup()
        delete cow_scene;
        delete cow_rend;
 
+       delete skybox_tex;
+       delete irradiance_tex;
        delete terrain_tex;
        delete terrain_rend;
+
        gfx_cleanup();
 }
 
index ce6f1b0..7535171 100644 (file)
@@ -1,8 +1,10 @@
 #include "global.h"
 #include "morph_renderer.h"
+#include "object.h"
 #include "scene.h"
 #include "shader.h"
 #include "shader_manager.h"
+#include "texture.h"
 
 MorphRenderer::MorphRenderer()
 {
@@ -39,6 +41,11 @@ bool MorphRenderer::create()
 
        mmviewproj_loc = sprog->get_uniform_location("mmviewproj");
        mview_loc = sprog->get_uniform_location("mview");
+       mmod_loc = sprog->get_uniform_location("mmod");
+
+       dstex_loc = sprog->get_uniform_location("dstex");
+       if(dstex_loc != -1)
+               sprog->set_uniformi(dstex_loc, 1);
 
        return true;
 }
@@ -51,12 +58,20 @@ void MorphRenderer::draw() const
        if(!sprog->link())
                return;
 
+       if(dskytex) {
+               dskytex->bind(1);
+       }
+
        sprog->use();
 
        for(size_t i=0; i<scene->objects.size(); i++) {
                float t = (sin(time_sec + 7.3 * noise(i * M_PI)) + 1) * 0.5;
                if (t_loc != -1)
                        sprog->set_uniformf(t_loc, t);
+
+               if(mmod_loc != -1)
+                       sprog->set_uniform_matrix(mmod_loc, scene->objects[i]->transform.upper3x3());
+
                draw_object(scene->objects[i]);
        }
 }
\ No newline at end of file
index b4decc4..b20ef19 100644 (file)
@@ -60,6 +60,18 @@ bool Renderer::create()
 
        mmviewproj_loc = sprog->get_uniform_location("mmviewproj");
        mview_loc = sprog->get_uniform_location("mview");
+       mmod_loc = sprog->get_uniform_location("mmod");
+
+       /* skybox, irradiance map uniforms */
+
+       stex_loc = sprog->get_uniform_location("stex");
+       dstex_loc = sprog->get_uniform_location("dstex");
+
+       if(stex_loc != -1)
+               sprog->set_uniformi(stex_loc, 0);
+
+       if(dstex_loc != -1)
+               sprog->set_uniformi(dstex_loc, 1);
 
        return true;
 }
@@ -71,6 +83,10 @@ void Renderer::draw() const
 
        if(skytex) {
                draw_skybox();
+
+               if(dskytex) {
+                       dskytex->bind(1);
+               }
        }
 
        if(!sprog->link())
@@ -116,6 +132,10 @@ void Renderer::draw_object(Object *object) const
        if(mview_loc != -1)
                sprog->set_uniform_matrix(mview_loc, mview);
 
+       Mat4 mmod = object->transform.upper3x3();
+       if(mmod_loc != -1)
+               sprog->set_uniform_matrix(mmod_loc, mmod);
+
        object->mesh->draw();
 
        // debug
index e484767..23471f5 100644 (file)
@@ -14,7 +14,10 @@ protected:
        int shin_loc;
        int mmviewproj_loc;
        int mview_loc;
+       int mmod_loc;
        int fog_loc;
+       int dstex_loc;
+       int stex_loc;
 
        ShaderProgram *sprog;