75 lines
2.1 KiB
C++
75 lines
2.1 KiB
C++
#include "posteffectmodule.h"
|
|
#include "framebuffer.h"
|
|
#include "glassert.h"
|
|
#include "texture.h"
|
|
#include "shader.h"
|
|
|
|
const GLfloat PostEffectModule::vertices[] = {
|
|
0.0f, 1.0f, 0.0f,
|
|
1.0f, 1.0f, 0.0f,
|
|
1.0f, 0.0f, 0.0f,
|
|
1.0f, 0.0f, 0.0f,
|
|
0.0f, 0.0f, 0.0f,
|
|
0.0f, 1.0f, 0.0f
|
|
};
|
|
|
|
PostEffectModule::PostEffectModule(int width, int height)
|
|
{
|
|
inputFBO = new FrameBuffer();
|
|
Texture* tex;
|
|
// Color buffer
|
|
tex = new Texture(GL_RGBA, GL_RGBA, width, height);
|
|
tex->setFiltering(GL_NEAREST);
|
|
inputFBO->addTexture(tex, GL_COLOR_ATTACHMENT1);
|
|
|
|
// Depth buffer
|
|
tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, width, height, GL_FLOAT);
|
|
tex->setFiltering(GL_NEAREST);
|
|
inputFBO->addTexture(tex, GL_DEPTH_ATTACHMENT);
|
|
|
|
inputFBO->initColorAttachments();
|
|
|
|
outputFBO = FrameBuffer::screen;
|
|
|
|
// set up vao
|
|
glAssert(glGenVertexArrays(1, &vao));
|
|
glAssert(glBindVertexArray(vao));
|
|
glAssert(glGenBuffers(1, &vbo));
|
|
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo));
|
|
glAssert(glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GLfloat), vertices, GL_STATIC_DRAW));
|
|
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, NULL));
|
|
glAssert(glEnableVertexAttribArray(0));
|
|
}
|
|
|
|
PostEffectModule::~PostEffectModule()
|
|
{
|
|
glAssert(glDeleteVertexArrays(1, &vao));
|
|
glAssert(glDeleteBuffers(1, &vbo));
|
|
delete(inputFBO);
|
|
}
|
|
|
|
void PostEffectModule::renderGL(Camera* myCamera, Scene* scene)
|
|
{
|
|
if(shader != NULL)
|
|
{
|
|
shader->bind();
|
|
shader->bindInteger(colorLocation, COLOR_BUFFER);
|
|
shader->bindInteger(depthLocation, DEPTH_BUFFER);
|
|
|
|
outputFBO->bindFBO();
|
|
|
|
inputFBO->getTexture(COLOR_BUFFER)->bind(COLOR_BUFFER);
|
|
inputFBO->getTexture(DEPTH_BUFFER)->bind(DEPTH_BUFFER);
|
|
|
|
glAssert(glBindVertexArray(vao));
|
|
glAssert(glDrawArrays(GL_TRIANGLES, 0, 36));
|
|
}
|
|
}
|
|
|
|
void PostEffectModule::setShader(Shader* myShader)
|
|
{
|
|
shader = myShader;
|
|
colorLocation = shader->getLocation("colorSampler");
|
|
depthLocation = shader->getLocation("depthSampler");
|
|
}
|