-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Framebuffer] Rendering code splitted in EffectRenderer. [Config] Add…
…ed option for bloom effect.
- Loading branch information
Showing
16 changed files
with
367 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
Oops, something went wrong.