Skip to content

Commit

Permalink
[Skybox] Now uses its own shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 14, 2020
1 parent 50a3c7a commit 2b68631
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 25 deletions.
2 changes: 1 addition & 1 deletion external/gamekit
2 changes: 1 addition & 1 deletion resources/shaders/fog.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions resources/shaders/game.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions resources/shaders/skybox.f.glsl
Original file line number Diff line number Diff line change
@@ -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;
}

22 changes: 22 additions & 0 deletions resources/shaders/skybox.v.glsl
Original file line number Diff line number Diff line change
@@ -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;
}

2 changes: 1 addition & 1 deletion source/client/graphics/PlayerBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down
12 changes: 9 additions & 3 deletions source/client/graphics/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions source/client/graphics/Skybox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define SKYBOX_HPP_

#include <gk/gl/Camera.hpp>
#include <gk/gl/Shader.hpp>

#include "CelestialObject.hpp"

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion source/client/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
23 changes: 8 additions & 15 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -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())
Expand Down

0 comments on commit 2b68631

Please sign in to comment.