]> git.localhorst.tv Git - blank.git/commitdiff
added distance fog
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 18 Mar 2015 10:26:38 +0000 (11:26 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 18 Mar 2015 10:26:38 +0000 (11:26 +0100)
src/chunk.cpp
src/hud.cpp
src/shader.cpp
src/shader.hpp
src/world.cpp

index d298fb2640cd737f586aa1ad09ec09ac0ef00b8a..81c8698db803dbcc3a1e99a4ebe1a9226bf15976 100644 (file)
@@ -462,8 +462,8 @@ ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
 , loaded()
 , to_generate()
 , to_free()
-, load_dist(4)
-, unload_dist(5) {
+, load_dist(6)
+, unload_dist(8) {
 
 }
 
index 266b80ae9dcbdc4f7aa29984817152501813c621..0747cffbca3424364e5a91c43bfe38c3eaa91af5 100644 (file)
@@ -62,6 +62,8 @@ void HUD::Display(const Block &b) {
 void HUD::Render(DirectionalLighting &program) {
        if (block_visible) {
                program.SetLightDirection({ 1.0f, 3.0f, 5.0f });
+               // disable distance fog
+               program.SetFogDensity(0.0f);
                GLContext::ClearDepthBuffer();
                program.SetMVP(block_transform, view, projection);
                block.Draw();
index 43b3910096789bb4aaece8133abd8ac2b42e75a4..eb09b10bcf2e0e1933e980e41610829452e9bc3a 100644 (file)
@@ -143,9 +143,11 @@ DirectionalLighting::DirectionalLighting()
 , light_color(0.9f, 0.9f, 0.9f)
 , vp(1.0f)
 , m_handle(0)
+, mv_handle(0)
 , mvp_handle(0)
 , light_direction_handle(0)
-, light_color_handle(0) {
+, light_color_handle(0)
+, fog_density_handle(0) {
        program.LoadShader(
                GL_VERTEX_SHADER,
                "#version 330 core\n"
@@ -153,11 +155,14 @@ DirectionalLighting::DirectionalLighting()
                "layout(location = 1) in vec3 vtx_color;\n"
                "layout(location = 2) in vec3 vtx_normal;\n"
                "uniform mat4 M;\n"
+               "uniform mat4 MV;\n"
                "uniform mat4 MVP;\n"
                "out vec3 frag_color;\n"
+               "out vec3 vtx_viewspace;\n"
                "out vec3 normal;\n"
                "void main() {\n"
                        "gl_Position = MVP * vec4(vtx_position, 1);\n"
+                       "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
                        "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
                        "frag_color = vtx_color;\n"
                "}\n"
@@ -166,16 +171,23 @@ DirectionalLighting::DirectionalLighting()
                GL_FRAGMENT_SHADER,
                "#version 330 core\n"
                "in vec3 frag_color;\n"
+               "in vec3 vtx_viewspace;\n"
                "in vec3 normal;\n"
                "uniform vec3 light_direction;\n"
                "uniform vec3 light_color;\n"
+               "uniform float fog_density;\n"
                "out vec3 color;\n"
                "void main() {\n"
                        "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
+                       // this should be the same as the clear color, otherwise looks really weird
+                       "vec3 fog_color = vec3(0, 0, 0);\n"
+                       "float e = 2.718281828;\n"
                        "vec3 n = normalize(normal);\n"
                        "vec3 l = normalize(light_direction);\n"
                        "float cos_theta = clamp(dot(n, l), 0, 1);\n"
-                       "color = ambient + frag_color * light_color * cos_theta;\n"
+                       "vec3 reflect_color = ambient + frag_color * light_color * cos_theta;\n"
+                       "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
+                       "color = mix(fog_color, reflect_color, value);\n"
                "}\n"
        );
        program.Link();
@@ -185,9 +197,11 @@ DirectionalLighting::DirectionalLighting()
        }
 
        m_handle = program.UniformLocation("M");
+       mv_handle = program.UniformLocation("MV");
        mvp_handle = program.UniformLocation("MVP");
        light_direction_handle = program.UniformLocation("light_direction");
        light_color_handle = program.UniformLocation("light_color");
+       fog_density_handle = program.UniformLocation("fog_density");
 }
 
 
@@ -201,8 +215,10 @@ void DirectionalLighting::Activate() {
 }
 
 void DirectionalLighting::SetM(const glm::mat4 &m) {
+       glm::mat4 mv(view * m);
        glm::mat4 mvp(vp * m);
        glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
+       glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
        glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
 }
 
@@ -211,6 +227,11 @@ void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) {
        glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
 }
 
+void DirectionalLighting::SetFogDensity(float f) {
+       fog_density = f;
+       glUniform1f(fog_density_handle, fog_density);
+}
+
 void DirectionalLighting::SetProjection(const glm::mat4 &p) {
        projection = p;
        vp = p * view;
index ea5b84474223fab1da9bb8fd18ad1c7e859643c9..f42d19b396b1d8187302eea77c6fe9b4a1401dc2 100644 (file)
@@ -69,6 +69,8 @@ public:
 
        void SetLightDirection(const glm::vec3 &);
 
+       void SetFogDensity(float);
+
        void SetM(const glm::mat4 &m);
        void SetProjection(const glm::mat4 &p);
        void SetView(const glm::mat4 &v);
@@ -85,14 +87,18 @@ private:
        glm::vec3 light_direction;
        glm::vec3 light_color;
 
+       float fog_density;
+
        glm::mat4 projection;
        glm::mat4 view;
        glm::mat4 vp;
 
        GLuint m_handle;
+       GLuint mv_handle;
        GLuint mvp_handle;
        GLuint light_direction_handle;
        GLuint light_color_handle;
+       GLuint fog_density_handle;
 
 };
 
index 7ab7fe486ec3af8a3d202580e35d47644f79bc22..f0d96f8d4e931f87077f47607d77d003322f2150 100644 (file)
@@ -198,6 +198,11 @@ void World::Update(int dt) {
 
 void World::Render(DirectionalLighting &program) {
        program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
+       // fade out reaches 1/e (0.3679) at 1/fog_density,
+       // gets less than 0.01 at e/(2 * fog_density)
+       // I chose 0.011 because it yields 91 and 124 for those, so
+       // slightly less than 6 and 8 chunks
+       program.SetFogDensity(0.011f);
        program.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
 
        for (Chunk &chunk : chunks.Loaded()) {