59 lines
2.2 KiB
C++
59 lines
2.2 KiB
C++
#include "framebuffer.hpp"
|
|
#include <glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
void FrameBuffer::init() {
|
|
// target_fb config
|
|
glGenFramebuffers(1, &target_fb);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, target_fb);
|
|
|
|
// target_fb configuration
|
|
// -------------------------
|
|
glGenFramebuffers(1, &target_fb);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, target_fb);
|
|
|
|
// create a color attachment texture
|
|
glGenTextures(1, &texture_buffer);
|
|
glBindTexture(GL_TEXTURE_2D, texture_buffer);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_buffer, 0);
|
|
|
|
// create a renderbuffer object for depth and stencil attachment (we won't be sampling these)
|
|
glGenRenderbuffers(1, &target_rbo);
|
|
glBindRenderbuffer(GL_RENDERBUFFER, target_rbo);
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, SCR_WIDTH, SCR_HEIGHT); // use a single renderbuffer object for both a depth AND stencil buffer.
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, target_rbo); // now actually attach it
|
|
// now that we actually created the target_fb and added all attachments we want to check if it is actually complete now
|
|
|
|
dbc::check(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Frame buffer not complete.");
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
}
|
|
|
|
void FrameBuffer::begin() {
|
|
// target_fb stuff here
|
|
glBindFramebuffer(GL_FRAMEBUFFER, target_fb);
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
}
|
|
|
|
void FrameBuffer::commit() {
|
|
// disable the frame buffer
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
}
|
|
|
|
void FrameBuffer::draw(Screen& screen) {
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
// render the screen quad
|
|
screen.shader.use();
|
|
glBindVertexArray(screen.target_VAO);
|
|
glBindTexture(GL_TEXTURE_2D, texture_buffer);
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
}
|