post-processing test
This commit is contained in:
@@ -23,7 +23,7 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
|
||||
|
||||
// Setup color attachment (texture)
|
||||
GLuint tex;
|
||||
GLuint format = alpha ? GL_RGBA : GL_RGB;
|
||||
format = alpha ? GL_RGBA : GL_RGB;
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
|
||||
@@ -55,6 +55,16 @@ void Framebuffer::unbind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void Framebuffer::resize(uint width, uint height) {
|
||||
if (this->width == width && this->height == height) {
|
||||
return;
|
||||
}
|
||||
GLuint texid = texture->getId();
|
||||
texture->bind();
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
|
||||
texture->unbind();
|
||||
}
|
||||
|
||||
Texture* Framebuffer::getTexture() const {
|
||||
return texture.get();
|
||||
}
|
||||
|
||||
+26
-13
@@ -8,22 +8,35 @@
|
||||
class Texture;
|
||||
|
||||
class Framebuffer {
|
||||
uint fbo;
|
||||
uint depth;
|
||||
uint width;
|
||||
uint height;
|
||||
std::unique_ptr<Texture> texture;
|
||||
uint fbo;
|
||||
uint depth;
|
||||
uint width;
|
||||
uint height;
|
||||
uint format;
|
||||
std::unique_ptr<Texture> texture;
|
||||
public:
|
||||
Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture);
|
||||
Framebuffer(uint width, uint height, bool alpha=false);
|
||||
~Framebuffer();
|
||||
Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture);
|
||||
Framebuffer(uint width, uint height, bool alpha=false);
|
||||
~Framebuffer();
|
||||
|
||||
void bind();
|
||||
void unbind();
|
||||
/// @brief Use framebuffer
|
||||
void bind();
|
||||
|
||||
Texture* getTexture() const;
|
||||
uint getWidth() const;
|
||||
uint getHeight() const;
|
||||
/// @brief Stop using framebuffer
|
||||
void unbind();
|
||||
|
||||
/// @brief Update framebuffer texture size
|
||||
/// @param width new width
|
||||
/// @param height new height
|
||||
void resize(uint width, uint height);
|
||||
|
||||
/// @brief Get framebuffer color attachment
|
||||
Texture* getTexture() const;
|
||||
|
||||
/// @brief Get framebuffer width
|
||||
uint getWidth() const;
|
||||
/// @brief Get framebuffer height
|
||||
uint getHeight() const;
|
||||
};
|
||||
|
||||
#endif /* SRC_GRAPHICS_FRAMEBUFFER_H_ */
|
||||
|
||||
@@ -31,7 +31,7 @@ GfxContext::~GfxContext() {
|
||||
fbo->unbind();
|
||||
}
|
||||
if (parent->fbo) {
|
||||
fbo->bind();
|
||||
parent->fbo->bind();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "PostProcessing.h"
|
||||
#include "Mesh.h"
|
||||
#include "Shader.h"
|
||||
#include "Texture.h"
|
||||
#include "Framebuffer.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
PostProcessing::PostProcessing() {
|
||||
// Fullscreen quad mesh bulding
|
||||
float vertices[] {
|
||||
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
|
||||
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f
|
||||
};
|
||||
vattr attrs[] {{2}, {0}};
|
||||
quadMesh = std::make_unique<Mesh>(vertices, 6, attrs);
|
||||
}
|
||||
|
||||
PostProcessing::~PostProcessing() {
|
||||
}
|
||||
|
||||
void PostProcessing::use(GfxContext& context) {
|
||||
const auto& vp = context.getViewport();
|
||||
if (fbo) {
|
||||
fbo->resize(vp.getWidth(), vp.getHeight());
|
||||
} else {
|
||||
fbo = std::make_unique<Framebuffer>(vp.getWidth(), vp.getHeight());
|
||||
}
|
||||
context.setFramebuffer(fbo.get());
|
||||
}
|
||||
|
||||
void PostProcessing::render(GfxContext& context, Shader* screenShader) {
|
||||
if (fbo == nullptr) {
|
||||
throw std::runtime_error("'use(...)' was never called");
|
||||
}
|
||||
|
||||
const auto& viewport = context.getViewport();
|
||||
screenShader->use();
|
||||
screenShader->uniform2i("u_screenSize", viewport.size());
|
||||
fbo->getTexture()->bind();
|
||||
quadMesh->draw();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef GRAPHICS_POST_PROCESSING_H_
|
||||
#define GRAPHICS_POST_PROCESSING_H_
|
||||
|
||||
#include "Viewport.h"
|
||||
#include "GfxContext.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Mesh;
|
||||
class Shader;
|
||||
class Framebuffer;
|
||||
|
||||
/// @brief Framebuffer with blitting with shaders.
|
||||
/// @attention Current implementation does not support multiple render passes
|
||||
/// for multiple effects. Will be implemented in v0.21
|
||||
class PostProcessing {
|
||||
/// @brief Main framebuffer (lasy field)
|
||||
std::unique_ptr<Framebuffer> fbo;
|
||||
/// @brief Fullscreen quad mesh as the post-processing canvas
|
||||
std::unique_ptr<Mesh> quadMesh;
|
||||
public:
|
||||
PostProcessing();
|
||||
~PostProcessing();
|
||||
|
||||
/// @brief Prepare and bind framebuffer
|
||||
/// @param context graphics context will be modified
|
||||
void use(GfxContext& context);
|
||||
|
||||
/// @brief Render fullscreen quad using the passed shader
|
||||
/// with framebuffer texture bound
|
||||
/// @param context graphics context
|
||||
/// @param screenShader shader used for fullscreen quad
|
||||
/// @throws std::runtime_error if use(...) wasn't called before
|
||||
void render(GfxContext& context, Shader* screenShader);
|
||||
};
|
||||
|
||||
#endif // GRAPHICS_POST_PROCESSING_H_
|
||||
@@ -52,6 +52,11 @@ void Shader::uniform2f(std::string name, glm::vec2 xy){
|
||||
glUniform2f(transformLoc, xy.x, xy.y);
|
||||
}
|
||||
|
||||
void Shader::uniform2i(std::string name, glm::ivec2 xy){
|
||||
GLuint transformLoc = glGetUniformLocation(id, name.c_str());
|
||||
glUniform2i(transformLoc, xy.x, xy.y);
|
||||
}
|
||||
|
||||
void Shader::uniform3f(std::string name, float x, float y, float z){
|
||||
GLuint transformLoc = glGetUniformLocation(id, name.c_str());
|
||||
glUniform3f(transformLoc, x,y,z);
|
||||
|
||||
@@ -21,6 +21,7 @@ public:
|
||||
void uniform1f(std::string name, float x);
|
||||
void uniform2f(std::string name, float x, float y);
|
||||
void uniform2f(std::string name, glm::vec2 xy);
|
||||
void uniform2i(std::string name, glm::ivec2 xy);
|
||||
void uniform3f(std::string name, float x, float y, float z);
|
||||
void uniform3f(std::string name, glm::vec3 xyz);
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ public:
|
||||
virtual uint getWidth() const;
|
||||
virtual uint getHeight() const;
|
||||
|
||||
glm::vec2 size() const {
|
||||
return glm::vec2(width, height);
|
||||
glm::ivec2 size() const {
|
||||
return glm::ivec2(width, height);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user