[GameState] Framebuffer is now resized when window size changes. [Framebuffer] Small fixes.

This commit is contained in:
Quentin Bazin 2020-06-29 23:39:02 +02:00
parent 608b53730e
commit f0df5815c0
4 changed files with 89 additions and 9 deletions

View File

@ -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);
// }

View File

@ -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) {

View File

@ -29,12 +29,19 @@
#include <gk/core/IntTypes.hpp>
#include <gk/gl/OpenGL.hpp>
#include <gk/utils/NonCopyable.hpp>
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);

View File

@ -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);