Skip to content

Commit

Permalink
[Framebuffer] Rendering code splitted in EffectRenderer. [Config] Add…
Browse files Browse the repository at this point in the history
…ed option for bloom effect.
  • Loading branch information
Unarelith committed Jun 30, 2020
1 parent 7be6c28 commit dbd860a
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 139 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ openminer_server
*.dat
*.zip
saves
test_atlas.png
test_atlas*.png
config/client.lua
config/keys.lua
config/server.lua
Expand Down
1 change: 1 addition & 0 deletions config/client.example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ isAmbientOcclusionEnabled = false
isWireframeModeEnabled = false
isFullscreenModeEnabled = false
isVerticalSyncEnabled = true
isBloomEffectEnabled = false
cameraFOV = 70.0
screenWidth = 1600
screenHeight = 1050
Expand Down
29 changes: 29 additions & 0 deletions resources/shaders/blur.f.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#version 130

varying vec2 v_texCoord;

uniform sampler2D sampleTexture;

uniform bool horizontal;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);

void main() {
vec2 tex_offset = 1.0 / textureSize(sampleTexture, 0);
vec3 result = texture2D(sampleTexture, v_texCoord).rgb * weight[0];

if(horizontal) {
for(int i = 1; i < 5; ++i) {
result += texture(sampleTexture, v_texCoord + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(sampleTexture, v_texCoord - vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
}
}
else {
for(int i = 1; i < 5; ++i) {
result += texture(sampleTexture, v_texCoord + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(sampleTexture, v_texCoord - vec2(0.0, tex_offset.y * i)).rgb * weight[i];
}
}

gl_FragColor = vec4(result, 1.0);
}

13 changes: 13 additions & 0 deletions resources/shaders/blur.v.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 120

attribute vec2 coord2d;
attribute vec2 texCoord;

varying vec2 v_texCoord;

void main() {
v_texCoord = texCoord;

gl_Position = vec4(coord2d, 0, 1);
}

12 changes: 8 additions & 4 deletions resources/shaders/game.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ varying float v_dist;
uniform int u_renderDistance;

uniform sampler2D u_tex;
/* uniform sampler2D u_bloomTex; */
uniform sampler2D u_bloomTex;

uniform int u_isBloomEnabled;

// Get light color
vec4 light(vec4 color, vec3 lightColor, vec4 lightPosition, float ambientIntensity, float diffuseIntensity);
Expand All @@ -21,9 +23,6 @@ vec4 light(vec4 color, vec3 lightColor, vec4 lightPosition, float ambientIntensi
vec4 fog(vec4 color, float fogCoord, float fogStart, float fogEnd);

void main() {
/* gl_FragData[1] = texture2D(u_bloomTex, v_texCoord); */
gl_FragData[1] = vec4(0, 0, 0, 0);

// Needed to prevent bad interpolation on some systems
// Refer to #23 for more informations
float blockFace = floor(v_blockFace + 0.5);
Expand Down Expand Up @@ -82,5 +81,10 @@ void main() {
color = fog(color, v_dist, u_renderDistance - 32, u_renderDistance);

gl_FragData[0] = color;

if (u_isBloomEnabled == 1)
gl_FragData[1] = texture2D(u_bloomTex, v_texCoord);
else
gl_FragData[1] = vec4(0, 0, 0, 0);
}

4 changes: 2 additions & 2 deletions resources/shaders/screen.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void main() {
color.rgb = mix(color.rgb, u_fogColor.rgb, clamp(pow(depth, u_fogDepth), 0.0, 1.0));
}

if (gl_FragCoord.x < 800)
color += bloom;
/* if (gl_FragCoord.x < 800) */
color.rgb += bloom.rgb;

// Grayscale
/* float average = (color.r + color.g + color.b) / 3.0; // Basic */
Expand Down
3 changes: 3 additions & 0 deletions source/client/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ bool Config::isAmbientOcclusionEnabled = false;
bool Config::isWireframeModeEnabled = false;
bool Config::isFullscreenModeEnabled = false;
bool Config::isVerticalSyncEnabled = true;
bool Config::isBloomEffectEnabled = false;
float Config::cameraFOV = 70.0f;
u16 Config::screenWidth = 1600;
u16 Config::screenHeight = 1050;
Expand Down Expand Up @@ -90,6 +91,7 @@ void Config::loadConfigFromFile(const char *filename) {
isWireframeModeEnabled = lua["isWireframeModeEnabled"].get_or(isWireframeModeEnabled);
isFullscreenModeEnabled = lua["isFullscreenModeEnabled"].get_or(isFullscreenModeEnabled);
isVerticalSyncEnabled = lua["isVerticalSyncEnabled"].get_or(isVerticalSyncEnabled);
isBloomEffectEnabled = lua["isBloomEffectEnabled"].get_or(isBloomEffectEnabled);
cameraFOV = lua["cameraFOV"].get_or(cameraFOV);
screenWidth = lua["screenWidth"].get_or(screenWidth);
screenHeight = lua["screenHeight"].get_or(screenHeight);
Expand Down Expand Up @@ -126,6 +128,7 @@ void Config::saveConfigToFile(const char *filename) {
file << "isWireframeModeEnabled = " << (isWireframeModeEnabled ? "true" : "false") << std::endl;
file << "isFullscreenModeEnabled = " << (isFullscreenModeEnabled ? "true" : "false") << std::endl;
file << "isVerticalSyncEnabled = " << (isVerticalSyncEnabled ? "true" : "false") << std::endl;
file << "isBloomEffectEnabled = " << (isBloomEffectEnabled ? "true" : "false") << std::endl;
file << "cameraFOV = " << cameraFOV << std::endl;
file << "screenWidth = " << screenWidth << std::endl;
file << "screenHeight = " << screenHeight << std::endl;
Expand Down
1 change: 1 addition & 0 deletions source/client/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace Config {
extern bool isWireframeModeEnabled;
extern bool isFullscreenModeEnabled;
extern bool isVerticalSyncEnabled;
extern bool isBloomEffectEnabled;
extern float cameraFOV;
extern u16 screenWidth;
extern u16 screenHeight;
Expand Down
155 changes: 155 additions & 0 deletions source/client/graphics/EffectRenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include "Config.hpp"
#include "EffectRenderer.hpp"

void EffectRenderer::init(u16 width, u16 height) {
m_fbo.init(width, height);

float quad[24] = {
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,

-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};

gk::VertexBuffer::bind(&m_vbo);
m_vbo.setData(sizeof(quad), &quad, GL_STATIC_DRAW);
gk::VertexBuffer::bind(nullptr);

m_pingpongBuffer[0].init(width, height);
m_pingpongBuffer[1].init(width, height);
}

void EffectRenderer::loadShaders() {
m_shader.createProgram();
m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/screen.v.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/screen.f.glsl");
m_shader.linkProgram();

gk::Shader::bind(&m_shader);
m_shader.setUniform("colorTexture", 0);
m_shader.setUniform("bloomTexture", 1);
m_shader.setUniform("depthTexture", 2);
gk::Shader::bind(nullptr);

m_blurShader.createProgram();
m_blurShader.addShader(GL_VERTEX_SHADER, "resources/shaders/blur.v.glsl");
m_blurShader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/blur.f.glsl");
m_blurShader.linkProgram();

gk::Shader::bind(&m_blurShader);
m_blurShader.setUniform("sampleTexture", 0);
gk::Shader::bind(nullptr);
}

void EffectRenderer::begin() const {
Framebuffer::bind(&m_fbo);

static GLenum buffers[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, buffers);

glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
}

void EffectRenderer::end() const {
Framebuffer::bind(nullptr);
glClear(GL_COLOR_BUFFER_BIT);
}

void EffectRenderer::render() const {
glDisable(GL_DEPTH_TEST);

// Bloom effect
if (Config::isBloomEffectEnabled) {
bool horizontal = true;
bool firstIteration = true;
int amount = 10;
gk::Shader::bind(&m_blurShader);
for (int i = 0 ; i < amount ; ++i) {
Framebuffer::bind(&m_pingpongBuffer[horizontal]);
m_blurShader.setUniform("horizontal", (int)horizontal);

if (firstIteration)
m_fbo.bindBloomTexture(0);
else
m_pingpongBuffer[!horizontal].bindColorTexture(0);

renderQuad();

horizontal = !horizontal;
if (firstIteration)
firstIteration = false;
}

Framebuffer::bind(nullptr);

m_pingpongBuffer[!horizontal].bindColorTexture(1);
}
else
m_fbo.bindBloomTexture(1);

m_fbo.bindColorTexture(0);
m_fbo.bindDepthTexture(2);

gk::Shader::bind(&m_shader);
m_shader.setUniform("u_effectType", Config::currentScreenEffect);
m_shader.setUniform("u_fogDepth", Config::fogDepth);
m_shader.setUniform("u_fogColor", Config::fogColor);

renderQuad();

gk::Shader::bind(nullptr);

glActiveTexture(GL_TEXTURE0);

glEnable(GL_DEPTH_TEST);
}

void EffectRenderer::renderQuad() const {
gk::VertexBuffer::bind(&m_vbo);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

m_vbo.setAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(0 * sizeof(float)));
m_vbo.setAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));

glDrawArrays(GL_TRIANGLES, 0, 6);

glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);

gk::VertexBuffer::bind(nullptr);
}

61 changes: 61 additions & 0 deletions source/client/graphics/EffectRenderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef EFFECTRENDERER_HPP_
#define EFFECTRENDERER_HPP_

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

#include "Framebuffer.hpp"

class EffectRenderer {
public:
EffectRenderer(u16 width, u16 height) { init(width, height); }

void init(u16 width, u16 height);

void loadShaders();

void begin() const;
void end() const;

void render() const;

private:
void renderQuad() const;

Framebuffer m_fbo{Framebuffer::All};

Framebuffer m_pingpongBuffer[2]{Framebuffer::Color, Framebuffer::Color};

gk::Shader m_shader;
gk::Shader m_blurShader;

gk::VertexBuffer m_vbo;
};

#endif // EFFECTRENDERER_HPP_
Loading

0 comments on commit dbd860a

Please sign in to comment.