[World] Render distance: OK.

This commit is contained in:
Quentin Bazin 2014-12-29 03:14:09 +01:00
parent 72918f2cdc
commit 6ce01710d9
5 changed files with 21 additions and 15 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}