Brenta Engine 1.2
Loading...
Searching...
No Matches
frame_buffer.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/frame_buffer.hpp>
7#include <brenta/gl.hpp>
8#include <brenta/logger.hpp>
9#include <brenta/window.hpp>
10
11#include <stdexcept>
12
13using namespace brenta;
14using namespace brenta::types;
15
16framebuffer::framebuffer(int width, int height, GLenum format)
17{
18 this->format = format;
19
20 glGenFramebuffers(1, &this->id);
21 if (this->id == 0)
22 {
23 ERROR("framebuffer: error generating framebuffer");
24 throw std::runtime_error("framebuffer: error generating framebuffer");
25 }
26 glBindFramebuffer(GL_FRAMEBUFFER, this->id);
27 check_error();
28
29 glGenTextures(1, &this->texture_id);
30 if (this->texture_id == 0)
31 {
32 ERROR("framebuffer: error generating texture");
33 exit(1);
34 }
35 glBindTexture(GL_TEXTURE_2D, this->texture_id);
36 check_error();
37
38 glTexImage2D(GL_TEXTURE_2D, 0, this->format, width, height, 0, GL_RGB,
39 GL_UNSIGNED_BYTE, NULL);
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
42 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
43 this->texture_id, 0);
44
45 glGenRenderbuffers(1, &this->render_buffer_id);
46 glBindRenderbuffer(GL_RENDERBUFFER, this->render_buffer_id);
47 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
48 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
49 GL_RENDERBUFFER, this->render_buffer_id);
50 check_error();
51
52 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
53 {
54 ERROR("framebuffer: not complete");
55 exit(1);
56 }
57
58 glBindFramebuffer(GL_FRAMEBUFFER, 0);
59 glBindTexture(GL_TEXTURE_2D, 0);
60 glBindRenderbuffer(GL_RENDERBUFFER, 0);
61}
62
64{
65 glDeleteFramebuffers(1, &this->id);
66 glDeleteTextures(1, &this->texture_id);
67}
68
69void framebuffer::bind()
70{
71 glBindFramebuffer(GL_FRAMEBUFFER, this->id);
72 check_error();
73}
74
75void framebuffer::unbind()
76{
77 glBindFramebuffer(GL_FRAMEBUFFER, 0);
78 check_error();
79}
80
82{
83 glDeleteFramebuffers(1, &this->id);
84 glDeleteTextures(1, &this->texture_id);
85}
86
87void framebuffer::rescale(int width, int height)
88{
89 glBindFramebuffer(GL_FRAMEBUFFER, this->id);
90 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
91 {
92 ERROR("framebuffer: not complete");
93 return;
94 }
95
96 glBindTexture(GL_TEXTURE_2D, this->texture_id);
97 check_error();
98
99 glTexImage2D(GL_TEXTURE_2D, 0, this->format, width, height, 0, GL_RGBA,
100 GL_UNSIGNED_BYTE, NULL);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
103 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
104 this->texture_id, 0);
105 glBindTexture(GL_TEXTURE_2D, 0);
106 check_error();
107
108 glBindRenderbuffer(GL_RENDERBUFFER, this->render_buffer_id);
109 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
110 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
111 GL_RENDERBUFFER, this->render_buffer_id);
112
113 window::set_width_height(width, height);
114}
115
116void framebuffer::set_format(GLenum format)
117{
118 this->format = format;
119}
~framebuffer()
Destructor Deletes the framebuffer and its texture.
void destroy()
Delete the framebuffer and its texture.
void set_format(GLenum format)
Set the format of the framebuffer.
GLenum format
Color format of the frame buffer Default is GL_RGBA.
void rescale(int width, int height)
Rescale the framebuffer.
framebuffer()
Empty constructor Does nothing.