|
|
|
|
#include "framebuffer.hpp"
|
|
|
|
|
#include <glad/gl.h>
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
#include "config.hpp"
|
|
|
|
|
|
|
|
|
|
void FrameBuffer::init() {
|
|
|
|
|
glGenFramebuffers(1, &target_fb);
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, target_fb);
|
|
|
|
|
glGenTextures(1, &texture_color_buffer);
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_color_buffer);
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WINDOW_WIDTH, WINDOW_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_color_buffer, 0);
|
|
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
invariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FrameBuffer::begin() {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, target_fb);
|
|
|
|
|
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FrameBuffer::commit() {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FrameBuffer::draw(Screen& screen) {
|
|
|
|
|
screen.shader.use();
|
|
|
|
|
glBindVertexArray(screen.target_VAO);
|
|
|
|
|
glActiveTexture(GL_TEXTURE0 + screen.texture_unit);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_color_buffer);
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FrameBuffer::invariant() {
|
|
|
|
|
dbc::check(target_fb != UINT_MAX, "target_fb not initialized");
|
|
|
|
|
dbc::check(texture_color_buffer != UINT_MAX, "texture_color_buffer not initialized");
|
|
|
|
|
}
|