added fog (need to set the fog params)
authorEleni Maria Stea <estea@igalia.com>
Mon, 21 Aug 2017 18:23:36 +0000 (21:23 +0300)
committerEleni Maria Stea <estea@igalia.com>
Mon, 21 Aug 2017 18:23:36 +0000 (21:23 +0300)
gl_shaders/default.f.glsl
src/opengl/texture-gl.cc
src/opengl/texture-gl.h
src/renderer.cc
src/texture.h
src/vulkan/texture-vk.cc
src/vulkan/texture-vk.h

index 490efab..17d4353 100644 (file)
@@ -11,11 +11,14 @@ varying vec3 normal;
 varying vec3 ldir;
 varying vec2 tex_coord;
 
+const float fog_density = 0.005;
+const vec4 sky_color = vec4(0.35, 0.5, 0.65, 1.0);
+
 out vec4 color;
 
 void main()
 {
-       vec3 p = normalize(pos);
+       vec3 p = normalize(pos); // view space dir
        vec3 n = normalize(normal);
        vec3 l = normalize(ldir);
 
@@ -25,7 +28,14 @@ void main()
        float cdiff = max(dot(l, n), 0.0);
        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);
-       color.xyz = diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec;
-       color.w = 1.0;
+
+       vec4 object_color;
+       object_color.xyz = diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec;
+       object_color.w = 1.0;
+
+       color = mix(sky_color, object_color, fog);
 }
index 157d0bb..80db3ad 100644 (file)
@@ -56,10 +56,15 @@ void TextureGL::update()
        glGenerateMipmap(target);
 }
 
-void TextureGL::bind()
+void TextureGL::bind(int texture_unit)
 {
+       glActiveTexture(GL_TEXTURE0 + texture_unit);
+
        unsigned int target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
        glBindTexture(target, tex);
+
+       //TODO: (not needed for now) if I ever use textures outside the Texture class
+       //glActiveTexture(GL_TEXTURE0);
 }
 
 void TextureGL::unbind()
index aa2b3db..0b495ed 100644 (file)
@@ -13,7 +13,7 @@ public:
        TextureGL();
        virtual ~TextureGL();
 
-       virtual void bind() override;
+       virtual void bind(int texture_unit = 0) override;
        virtual void unbind() override;
 };
 
index 380cf69..e383efa 100644 (file)
@@ -98,7 +98,7 @@ void Renderer::draw_object(Object *object) const
        /* texture */
 
        if(m->dtex)
-               m->dtex->bind();
+               m->dtex->bind(0);
 
        /* setting uniforms for matrices */
 
index 3a850f1..a1b52d8 100644 (file)
@@ -24,7 +24,7 @@ public:
 
        virtual bool is_cubemap() const;
 
-       virtual void bind() = 0;
+       virtual void bind(int texture_unit = 0) = 0;
        virtual void unbind() = 0;
 };
 
index 3c56ebd..1c9ad46 100644 (file)
@@ -12,7 +12,7 @@ void TextureVK::update()
 {
 }
 
-void TextureVK::bind()
+void TextureVK::bind(int texture_unit)
 {
 }
 
index 56da38d..5b93917 100644 (file)
@@ -11,7 +11,7 @@ public:
        TextureVK();
        virtual ~TextureVK();
 
-       virtual void bind() override;
+       virtual void bind(int texture_unit = 0) override;
        virtual void unbind() override;
 };