From f0df5815c08101d1a07e271e9a4b9f51281d95f5 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Mon, 29 Jun 2020 23:39:02 +0200 Subject: [PATCH] [GameState] Framebuffer is now resized when window size changes. [Framebuffer] Small fixes. --- resources/shaders/screen.f.glsl | 60 +++++++++++++++++++++++++- source/client/graphics/Framebuffer.cpp | 25 +++++++++-- source/client/graphics/Framebuffer.hpp | 9 +++- source/client/states/GameState.cpp | 4 +- 4 files changed, 89 insertions(+), 9 deletions(-) diff --git a/resources/shaders/screen.f.glsl b/resources/shaders/screen.f.glsl index b5bc3460..a3d06200 100644 --- a/resources/shaders/screen.f.glsl +++ b/resources/shaders/screen.f.glsl @@ -6,7 +6,63 @@ varying vec2 v_coord2d; uniform sampler2D screenTexture; void main() { - gl_FragColor = texture2D(screenTexture, v_texCoord); - /* gl_FragColor = vec4(vec3(1.0 - texture2D(screenTexture, v_texCoord)), 1.0); */ + vec4 color = texture2D(screenTexture, v_texCoord); + + // Grayscale + /* float average = (color.r + color.g + color.b) / 3.0; // Basic */ + /* float average = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; // More realistic */ + /* color = vec4(average, average, average, 1.0); */ + + // Inverted colors + /* color = vec4(vec3(1.0 - texture2D(screenTexture, v_texCoord)), 1.0); */ + + gl_FragColor = color; } +// const float offset = 1.0 / 300.0; +// +// void main() { +// vec2 offsets[9] = vec2[]( +// vec2(-offset, offset), // top-left +// vec2( 0.0f, offset), // top-center +// vec2( offset, offset), // top-right +// vec2(-offset, 0.0f), // center-left +// vec2( 0.0f, 0.0f), // center-center +// vec2( offset, 0.0f), // center-right +// vec2(-offset, -offset), // bottom-left +// vec2( 0.0f, -offset), // bottom-center +// vec2( offset, -offset) // bottom-right +// ); +// +// // Sharpen +// /* float kernel[9] = float[]( */ +// /* -1, -1, -1, */ +// /* -1, 9, -1, */ +// /* -1, -1, -1 */ +// /* ); */ +// +// // Blur +// /* float kernel[9] = float[]( */ +// /* 1.0 / 16, 2.0 / 16, 1.0 / 16, */ +// /* 2.0 / 16, 4.0 / 16, 2.0 / 16, */ +// /* 1.0 / 16, 2.0 / 16, 1.0 / 16 */ +// /* ); */ +// +// // Edge-detection +// /* float kernel[9] = float[]( */ +// /* 1, 1, 1, */ +// /* 1, -8, 1, */ +// /* 1, 1, 1 */ +// /* ); */ +// +// vec3 sampleTex[9]; +// for(int i = 0; i < 9; i++) { +// sampleTex[i] = vec3(texture2D(screenTexture, v_texCoord.st + offsets[i])); +// } +// +// vec3 col = vec3(0.0); +// for(int i = 0; i < 9; i++) +// col += sampleTex[i] * kernel[i]; +// +// gl_FragColor = vec4(col, 1.0); +// } diff --git a/source/client/graphics/Framebuffer.cpp b/source/client/graphics/Framebuffer.cpp index 80a38073..f39dd9fd 100644 --- a/source/client/graphics/Framebuffer.cpp +++ b/source/client/graphics/Framebuffer.cpp @@ -30,6 +30,17 @@ #include "Framebuffer.hpp" Framebuffer::Framebuffer(u16 width, u16 height) { + init(width, height); +} + +Framebuffer::~Framebuffer() { + clear(); +} + +void Framebuffer::init(u16 width, u16 height) { + if (m_id) + clear(); + glCheck(glGenFramebuffers(1, &m_id)); bind(this); @@ -55,15 +66,21 @@ Framebuffer::Framebuffer(u16 width, u16 height) { bind(nullptr); } -Framebuffer::~Framebuffer() { - if (m_id) +void Framebuffer::clear() { + if (m_id) { glCheck(glDeleteFramebuffers(1, &m_id)); + m_id = 0; + } - if (m_texid) + if (m_texid) { glCheck(glDeleteTextures(1, &m_texid)); + m_texid = 0; + } - if (m_rbo) + if (m_rbo) { glCheck(glDeleteRenderbuffers(1, &m_rbo)); + m_rbo = 0; + } } void Framebuffer::bind(const Framebuffer *framebuffer) { diff --git a/source/client/graphics/Framebuffer.hpp b/source/client/graphics/Framebuffer.hpp index 2fe0fe13..8d5fda61 100644 --- a/source/client/graphics/Framebuffer.hpp +++ b/source/client/graphics/Framebuffer.hpp @@ -29,12 +29,19 @@ #include #include +#include -class Framebuffer { +class Framebuffer : public gk::NonCopyable { public: Framebuffer(u16 width, u16 height); + Framebuffer(Framebuffer &&) = default; ~Framebuffer(); + Framebuffer &operator=(Framebuffer &&) = default; + + void init(u16 width, u16 height); + void clear(); + GLuint texid() const { return m_texid; } static void bind(const Framebuffer *framebuffer); diff --git a/source/client/states/GameState.cpp b/source/client/states/GameState.cpp index 896e33fc..92f534cf 100644 --- a/source/client/states/GameState.cpp +++ b/source/client/states/GameState.cpp @@ -139,6 +139,8 @@ void GameState::onEvent(const sf::Event &event) { m_camera.setAspectRatio((float)Config::screenWidth / Config::screenHeight); m_hud.setup(); + + m_fbo.init(Config::screenWidth, Config::screenHeight); } } @@ -213,7 +215,6 @@ void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { // gk::Shader::bind(nullptr); Framebuffer::bind(&m_fbo); - // glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); @@ -226,7 +227,6 @@ void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { target.draw(it.second, states); Framebuffer::bind(nullptr); - // glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_DEPTH_TEST);