126 lines
3.6 KiB
C++
126 lines
3.6 KiB
C++
#include "Multipass.h"
|
|
|
|
Multipass::Multipass() : _quad_shader("textureViewer.vert", "textureViewer.frag")
|
|
{
|
|
|
|
_draw_buffers_size = 0;
|
|
glGenFramebuffers(1, &_fboId);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
|
|
_createQuad();
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
}
|
|
|
|
void Multipass::setDrawBuffers()
|
|
{
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
|
|
glDrawBuffers(_draw_buffers_size, _draw_buffers);
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
throw std::runtime_error("Error during framebuffer initialisation");
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
}
|
|
|
|
void Multipass::bindToFrameBuffer(GLenum type, GLenum texture, std::string texName)
|
|
{
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, type, texture, _pass_textures[texName], 0);
|
|
if (type != GL_DEPTH_ATTACHMENT) {
|
|
_draw_buffers[_draw_buffers_size] = type;
|
|
_draw_buffers_size++;
|
|
}
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
}
|
|
|
|
Multipass::~Multipass()
|
|
{
|
|
glDeleteFramebuffers(1, &_fboId);
|
|
}
|
|
|
|
void Multipass::addTexture(const std::string & tex_name, GLuint filter, GLuint type, GLuint type_2,
|
|
bool depth, SceneContext &scnctx)
|
|
{
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
|
|
glGenTextures(1, &_pass_textures[tex_name]);;
|
|
glBindTexture(GL_TEXTURE_2D, _pass_textures[tex_name]);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, type, scnctx.width, scnctx.height, 0, type_2, GL_FLOAT, 0);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
if (depth)
|
|
{
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
|
|
}
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
}
|
|
|
|
void Multipass::enableFrameBufferTexture(const std::string tex_name)
|
|
{
|
|
_current_tex = tex_name;
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
|
|
}
|
|
|
|
void Multipass::drawResultToScreen(SceneContext & scnctx)
|
|
{
|
|
//EXAMPLE CODE
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
glViewport(0, 0, scnctx.width, scnctx.height);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
_quad_shader.enable();
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, _pass_textures[_current_tex]);
|
|
_quad_shader.addUniform("tex", 0);
|
|
glBindVertexArray(_quad_vao);
|
|
glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glBindVertexArray(0);
|
|
_quad_shader.disable();
|
|
}
|
|
|
|
void Multipass::_createQuad()
|
|
{
|
|
float pos[] = {
|
|
-1.0, 1.0,
|
|
1.0, 1.0,
|
|
-1.0, -1.0,
|
|
1.0, -1.0
|
|
};
|
|
|
|
float uv[] = {
|
|
0.0, 1.0,
|
|
1.0, 1.0,
|
|
0.0, 0.0,
|
|
1.0, 0.0
|
|
};
|
|
|
|
//Generate VAO
|
|
glGenVertexArrays(1, &_quad_vao);
|
|
//Bind VAO
|
|
glBindVertexArray(_quad_vao);
|
|
|
|
//Generate VBO
|
|
glGenBuffers(1, &_quad_vbo);
|
|
//Bind VBO
|
|
glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
|
|
//Alocate buffer
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(pos) + sizeof(uv), NULL, GL_STATIC_DRAW);
|
|
//Fill VBO
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(pos), pos);
|
|
glBufferSubData(GL_ARRAY_BUFFER, 8 * sizeof(float), sizeof(uv), uv);
|
|
|
|
//Fill attributes and uniforms
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, (sizeof(float) * 2), (void*)0);
|
|
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, (sizeof(float) * 2), (GLvoid*)(sizeof(float) * 8));
|
|
|
|
//unbind buffer
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
|
}
|