From 2b686319ca7a14d7f7f15ff3e18a376bccc44a17 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Tue, 14 Jul 2020 21:24:32 +0200 Subject: [PATCH] [Skybox] Now uses its own shader. --- external/gamekit | 2 +- resources/shaders/fog.f.glsl | 2 +- resources/shaders/game.f.glsl | 3 +++ resources/shaders/skybox.f.glsl | 24 ++++++++++++++++++++++++ resources/shaders/skybox.v.glsl | 22 ++++++++++++++++++++++ source/client/graphics/PlayerBox.cpp | 2 +- source/client/graphics/Skybox.cpp | 12 +++++++++--- source/client/graphics/Skybox.hpp | 6 +++--- source/client/hud/BlockCursor.cpp | 2 +- source/client/states/GameState.cpp | 23 ++++++++--------------- 10 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 resources/shaders/skybox.f.glsl create mode 100644 resources/shaders/skybox.v.glsl diff --git a/external/gamekit b/external/gamekit index 0a2ec1c22..ec8bc52b1 160000 --- a/external/gamekit +++ b/external/gamekit @@ -1 +1 @@ -Subproject commit 0a2ec1c2230031034dcae84225f07cff92a29d00 +Subproject commit ec8bc52b19b15b1ac0112f783380b779e3d49534 diff --git a/resources/shaders/fog.f.glsl b/resources/shaders/fog.f.glsl index 83accec7c..38fbf1152 100644 --- a/resources/shaders/fog.f.glsl +++ b/resources/shaders/fog.f.glsl @@ -9,7 +9,7 @@ vec4 fog(vec4 color, float fogCoord, float fogStart, float fogEnd) { const float pi = 3.1415927; - float sunlight = clamp((1 + sin(2 * pi * u_time) * 4.0), 0.0, 1.0); + float sunlight = clamp(0.5 + sin(2 * pi * u_time) * 2.0, 0.0, 1.0); float red = clamp(sunlight - (1 - u_fogColor.r), 0.0, u_fogColor.r); float green = clamp(sunlight - (1 - u_fogColor.g), 0.0, u_fogColor.g); diff --git a/resources/shaders/game.f.glsl b/resources/shaders/game.f.glsl index ac55c530d..84e0c97d2 100644 --- a/resources/shaders/game.f.glsl +++ b/resources/shaders/game.f.glsl @@ -27,6 +27,9 @@ void main() { float blockFace = floor(v_blockFace + 0.5); float lightCheck = floor(v_lightValue.x + 0.5); + // Discard if the pixel is too far away + if(blockFace > -1. && v_dist > u_renderDistance) discard; + // Get current pixel color and apply multiplier on grayscale textures vec4 color = v_color; if (v_texCoord.x > -0.99 && v_texCoord.y > -0.99) { diff --git a/resources/shaders/skybox.f.glsl b/resources/shaders/skybox.f.glsl new file mode 100644 index 000000000..10096b0fb --- /dev/null +++ b/resources/shaders/skybox.f.glsl @@ -0,0 +1,24 @@ +#version 120 + +varying vec4 v_color; +varying vec4 v_coord3d; +varying vec2 v_texCoord; + +uniform sampler2D u_tex; + +void main() { + vec4 color = v_color; + + if (v_texCoord.x != -1 && v_texCoord.y != -1) { + color = texture2D(u_tex, v_texCoord); + + if (color.r == color.g && color.g == color.b) { + color *= v_color; + } + } + + if (color.a < 0.3) discard; + + gl_FragColor = color; +} + diff --git a/resources/shaders/skybox.v.glsl b/resources/shaders/skybox.v.glsl new file mode 100644 index 000000000..36391927b --- /dev/null +++ b/resources/shaders/skybox.v.glsl @@ -0,0 +1,22 @@ +#version 120 + +attribute vec4 color; +attribute vec4 coord3d; +attribute vec2 texCoord; + +varying vec4 v_color; +varying vec4 v_coord3d; +varying vec2 v_texCoord; + +uniform mat4 u_modelMatrix; +uniform mat4 u_projectionMatrix; +uniform mat4 u_viewMatrix; + +void main() { + v_coord3d = u_modelMatrix * vec4(coord3d.xyz, 1.0); + v_color = color; + v_texCoord = texCoord; + + gl_Position = u_projectionMatrix * u_viewMatrix * v_coord3d; +} + diff --git a/source/client/graphics/PlayerBox.cpp b/source/client/graphics/PlayerBox.cpp index c02ce6f45..a7b34dc91 100644 --- a/source/client/graphics/PlayerBox.cpp +++ b/source/client/graphics/PlayerBox.cpp @@ -272,7 +272,7 @@ void PlayerBox::updateVertexBuffer() { void PlayerBox::draw(gk::RenderTarget &target, gk::RenderStates states) const { // Subtract the camera position - see comment in ClientWorld::draw() - gk::Vector3d cameraPosition = m_camera.getDPosition(); + const gk::Vector3d &cameraPosition = m_camera.getDPosition(); states.transform.translate(m_x - cameraPosition.x, m_y - cameraPosition.y, m_z - cameraPosition.z); states.transform.rotate(m_viewAngleH, gk::Vector3{0, 0, 1}); diff --git a/source/client/graphics/Skybox.cpp b/source/client/graphics/Skybox.cpp index 4ab173b45..9f449dd75 100644 --- a/source/client/graphics/Skybox.cpp +++ b/source/client/graphics/Skybox.cpp @@ -25,9 +25,13 @@ * ===================================================================================== */ #include "Skybox.hpp" -#include "Vertex.hpp" Skybox::Skybox(gk::Camera &camera) : m_camera(camera) { + m_shader.createProgram(); + m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/skybox.v.glsl"); + m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/skybox.f.glsl"); + m_shader.linkProgram(); + m_sun.setColor(gk::Color::Yellow); m_sun.setPosition(150, -10, -10); @@ -36,9 +40,11 @@ Skybox::Skybox(gk::Camera &camera) : m_camera(camera) { } void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const { + states.shader = &m_shader; + // Subtract the camera position - see comment in ClientWorld::draw() - gk::Vector3d cameraPosition = m_camera.getDPosition(); - states.transform.translate(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z); + const gk::Vector3d &cameraPosition = m_camera.getDPosition(); + states.transform.translate(cameraPosition.x, cameraPosition.y, cameraPosition.z); target.draw(m_sun, states); target.draw(m_moon, states); diff --git a/source/client/graphics/Skybox.hpp b/source/client/graphics/Skybox.hpp index 4a1f3ad68..63b61d655 100644 --- a/source/client/graphics/Skybox.hpp +++ b/source/client/graphics/Skybox.hpp @@ -28,6 +28,7 @@ #define SKYBOX_HPP_ #include +#include #include "CelestialObject.hpp" @@ -38,9 +39,8 @@ class Skybox : public gk::Drawable, public gk::Transformable { private: void draw(gk::RenderTarget &target, gk::RenderStates states) const override; - gk::Camera m_camera; - - gk::VertexBuffer m_sunVBO; + gk::Camera &m_camera; + gk::Shader m_shader; CelestialObject m_sun; CelestialObject m_moon; diff --git a/source/client/hud/BlockCursor.cpp b/source/client/hud/BlockCursor.cpp index 53da58a78..cd8c573a8 100644 --- a/source/client/hud/BlockCursor.cpp +++ b/source/client/hud/BlockCursor.cpp @@ -282,7 +282,7 @@ void BlockCursor::draw(gk::RenderTarget &target, gk::RenderStates states) const glCheck(glEnable(GL_DEPTH_TEST)); // Subtract the camera position - see comment in ClientWorld::draw() - gk::Vector3d cameraPosition = m_player.camera().getDPosition(); + const gk::Vector3d &cameraPosition = m_player.camera().getDPosition(); states.transform.translate(m_selectedBlock.x - cameraPosition.x, m_selectedBlock.y - cameraPosition.y, m_selectedBlock.z - cameraPosition.z); glCheck(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); diff --git a/source/client/states/GameState.cpp b/source/client/states/GameState.cpp index 659da0c82..6cbc1f48c 100644 --- a/source/client/states/GameState.cpp +++ b/source/client/states/GameState.cpp @@ -58,6 +58,7 @@ GameState::GameState() m_clientCommandHandler.setupCallbacks(); m_camera.setAspectRatio((float)Config::screenWidth / Config::screenHeight); + m_camera.setFarClippingPlane(10000.0f); m_world.setClient(m_clientCommandHandler); m_world.setCamera(m_player.camera()); @@ -182,23 +183,14 @@ void GameState::update() { } m_client.update(); - - // Update far plane using render distance - static u16 oldRenderDistance = Config::renderDistance; - if (Config::renderDistance != oldRenderDistance) { - m_camera.setFarClippingPlane(Config::renderDistance * CHUNK_MAXSIZE); - oldRenderDistance = Config::renderDistance; - } } void GameState::initShaders() { m_shader.createProgram(); - m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/game.v.glsl"); m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/light.f.glsl"); m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/fog.f.glsl"); m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/game.f.glsl"); - m_shader.linkProgram(); m_fbo.loadShader("screen"); @@ -210,11 +202,6 @@ void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) { void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { float time = std::fmod(gk::GameClock::getInstance().getTicks() * 4.f / 1000.f, 360.f) / 360.f; - - gk::Shader::bind(&m_shader); - m_shader.setUniform("u_time", time); - gk::Shader::bind(nullptr); - if (m_world.sky()) { const float pi = 3.1415927f; float sunlight = std::clamp(0.5f + std::sin(2 * pi * time) * 2.0f, 0.0f, 1.0f); @@ -227,12 +214,18 @@ void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { glClearColor(red, green, blue, 1.0f); } + gk::Shader::bind(&m_shader); + m_shader.setUniform("u_time", time); + gk::Shader::bind(nullptr); + m_fbo.begin(); states.shader = &m_shader; + target.setView(m_camera); - target.draw(m_world, states); + target.draw(m_skybox, states); + target.draw(m_world, states); for (auto &it : m_playerBoxes) if (it.second.dimension() == m_player.dimension())