From f1c3197c3e035f4be0e10ff6a8d0cf0339824177 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Mon, 21 Aug 2017 21:23:36 +0300 Subject: [PATCH] added fog (need to set the fog params) --- gl_shaders/default.f.glsl | 16 +++++++++++++--- src/opengl/texture-gl.cc | 7 ++++++- src/opengl/texture-gl.h | 2 +- src/renderer.cc | 2 +- src/texture.h | 2 +- src/vulkan/texture-vk.cc | 2 +- src/vulkan/texture-vk.h | 2 +- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/gl_shaders/default.f.glsl b/gl_shaders/default.f.glsl index 490efab..17d4353 100644 --- a/gl_shaders/default.f.glsl +++ b/gl_shaders/default.f.glsl @@ -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); } diff --git a/src/opengl/texture-gl.cc b/src/opengl/texture-gl.cc index 157d0bb..80db3ad 100644 --- a/src/opengl/texture-gl.cc +++ b/src/opengl/texture-gl.cc @@ -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() diff --git a/src/opengl/texture-gl.h b/src/opengl/texture-gl.h index aa2b3db..0b495ed 100644 --- a/src/opengl/texture-gl.h +++ b/src/opengl/texture-gl.h @@ -13,7 +13,7 @@ public: TextureGL(); virtual ~TextureGL(); - virtual void bind() override; + virtual void bind(int texture_unit = 0) override; virtual void unbind() override; }; diff --git a/src/renderer.cc b/src/renderer.cc index 380cf69..e383efa 100644 --- a/src/renderer.cc +++ b/src/renderer.cc @@ -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 */ diff --git a/src/texture.h b/src/texture.h index 3a850f1..a1b52d8 100644 --- a/src/texture.h +++ b/src/texture.h @@ -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; }; diff --git a/src/vulkan/texture-vk.cc b/src/vulkan/texture-vk.cc index 3c56ebd..1c9ad46 100644 --- a/src/vulkan/texture-vk.cc +++ b/src/vulkan/texture-vk.cc @@ -12,7 +12,7 @@ void TextureVK::update() { } -void TextureVK::bind() +void TextureVK::bind(int texture_unit) { } diff --git a/src/vulkan/texture-vk.h b/src/vulkan/texture-vk.h index 56da38d..5b93917 100644 --- a/src/vulkan/texture-vk.h +++ b/src/vulkan/texture-vk.h @@ -11,7 +11,7 @@ public: TextureVK(); virtual ~TextureVK(); - virtual void bind() override; + virtual void bind(int texture_unit = 0) override; virtual void unbind() override; }; -- 1.7.10.4