diff --git a/include/world/World.hpp b/include/world/World.hpp index 2e359ccc..a7d7be97 100644 --- a/include/world/World.hpp +++ b/include/world/World.hpp @@ -27,7 +27,7 @@ class World { World(); ~World(); - void draw(Shader &shader, const glm::mat4 &pv); + void draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix); Chunk *getChunk(s32 x, s32 z); diff --git a/shaders/game.f.glsl b/shaders/game.f.glsl index 29a8c12b..c57b4b53 100644 --- a/shaders/game.f.glsl +++ b/shaders/game.f.glsl @@ -43,7 +43,7 @@ void main() { float dist = length(v_viewSpace); if(dist > u_renderDistance) discard; - gl_FragColor = fog(color, fogColor, dist, u_renderDistance - 4, u_renderDistance); + gl_FragColor = fog(color, fogColor, dist, u_renderDistance - 6, u_renderDistance); } vec4 fog(vec4 color, vec4 fogColor, float fogCoord, float fogStart, float fogEnd) { diff --git a/shaders/game.v.glsl b/shaders/game.v.glsl index 9821bca0..be403fe1 100644 --- a/shaders/game.v.glsl +++ b/shaders/game.v.glsl @@ -18,9 +18,9 @@ uniform mat4 u_viewMatrix; void main() { v_coord3d = u_modelMatrix * vec4(coord3d, 1.0); - v_viewSpace = u_viewMatrix * u_modelMatrix * vec4(coord3d, 1.0); + v_viewSpace = u_viewMatrix * v_coord3d; - gl_Position = u_projectionMatrix * u_viewMatrix * v_coord3d; + gl_Position = u_projectionMatrix * v_viewSpace; v_color = vec4(color, 1.0); v_normal = vec4(normal, 1.0); diff --git a/source/states/GameState.cpp b/source/states/GameState.cpp index bbab2d55..953f21f8 100644 --- a/source/states/GameState.cpp +++ b/source/states/GameState.cpp @@ -48,7 +48,7 @@ void GameState::draw() { //m_skybox.draw(m_shader); - m_world.draw(m_shader, m_projectionMatrix * m_viewMatrix); + m_world.draw(m_shader, m_projectionMatrix, m_viewMatrix); Shader::bind(nullptr); } diff --git a/source/world/World.cpp b/source/world/World.cpp index 752f5300..eac4eeea 100644 --- a/source/world/World.cpp +++ b/source/world/World.cpp @@ -43,7 +43,7 @@ World::World() { World::~World() { } -void World::draw(Shader &shader, const glm::mat4 &pv) { +void World::draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix) { float ud = 1000.0; s32 ux = 0; s32 uz = 0; @@ -51,16 +51,22 @@ void World::draw(Shader &shader, const glm::mat4 &pv) { shader.setUniform("u_renderDistance", renderDistance * Chunk::width); for(auto &it : m_chunks) { - glm::mat4 model = glm::translate(glm::mat4(1.0f), - glm::vec3(it->x() * Chunk::width, - it->y() * Chunk::height, - it->z() * Chunk::depth)); - glm::mat4 mvp = pv * model; + glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), + glm::vec3(it->x() * Chunk::width, + it->y() * Chunk::height, + it->z() * Chunk::depth)); + + // Is the chunk closer enough? + glm::vec4 center = viewMatrix * modelMatrix * glm::vec4(Chunk::width / 2, + Chunk::height / 2, + Chunk::depth / 2, 1); + + if(glm::length(center) > (renderDistance + 1) * Chunk::width) { + continue; + } // Is this chunk on the screen? - glm::vec4 center = mvp * glm::vec4(Chunk::width / 2, - Chunk::height / 2, - Chunk::depth / 2, 1); + center = projectionMatrix * center; float d = glm::length(center); center.x /= center.w; @@ -87,7 +93,7 @@ void World::draw(Shader &shader, const glm::mat4 &pv) { continue; } - shader.setUniform("u_modelMatrix", model); + shader.setUniform("u_modelMatrix", modelMatrix); it->draw(shader); }