Brenta Engine 1.1
Loading...
Searching...
No Matches
frame_buffer.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "frame_buffer.hpp"
28
29#include "engine_logger.hpp"
30#include "gl_helper.hpp"
31#include "screen.hpp"
32
33using namespace brenta;
34using namespace brenta::types;
35
36framebuffer::framebuffer(int width, int height, GLenum format)
37{
38 this->format = format;
39
40 glGenFramebuffers(1, &this->id);
41 if (this->id == 0)
42 {
43 ERROR("Error creating framebuffer!");
44 exit(1);
45 }
46 glBindFramebuffer(GL_FRAMEBUFFER, this->id);
47 check_error();
48
49 glGenTextures(1, &this->texture_id);
50 if (this->texture_id == 0)
51 {
52 ERROR("Error creating texture!");
53 exit(1);
54 }
55 glBindTexture(GL_TEXTURE_2D, this->texture_id);
56 check_error();
57
58 glTexImage2D(GL_TEXTURE_2D, 0, this->format, width, height, 0, GL_RGB,
59 GL_UNSIGNED_BYTE, NULL);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
62 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
63 this->texture_id, 0);
64
65 glGenRenderbuffers(1, &this->render_buffer_id);
66 glBindRenderbuffer(GL_RENDERBUFFER, this->render_buffer_id);
67 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
68 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
69 GL_RENDERBUFFER, this->render_buffer_id);
70 check_error();
71
72 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
73 {
74 ERROR("Framebuffer is not complete!");
75 exit(1);
76 }
77
78 glBindFramebuffer(GL_FRAMEBUFFER, 0);
79 glBindTexture(GL_TEXTURE_2D, 0);
80 glBindRenderbuffer(GL_RENDERBUFFER, 0);
81}
82
84{
85 glDeleteFramebuffers(1, &this->id);
86 glDeleteTextures(1, &this->texture_id);
87}
88
90{
91 glBindFramebuffer(GL_FRAMEBUFFER, this->id);
92 check_error();
93}
94
96{
97 glBindFramebuffer(GL_FRAMEBUFFER, 0);
98 check_error();
99}
100
102{
103 glDeleteFramebuffers(1, &this->id);
104 glDeleteTextures(1, &this->texture_id);
105}
106
107void framebuffer::rescale(int width, int height)
108{
109 glBindFramebuffer(GL_FRAMEBUFFER, this->id);
110 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
111 {
112 ERROR("Framebuffer is not complete!");
113 return;
114 }
115
116 glBindTexture(GL_TEXTURE_2D, this->texture_id);
117 check_error();
118
119 glTexImage2D(GL_TEXTURE_2D, 0, this->format, width, height, 0, GL_RGBA,
120 GL_UNSIGNED_BYTE, NULL);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
123 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
124 this->texture_id, 0);
125 glBindTexture(GL_TEXTURE_2D, 0);
126 check_error();
127
128 glBindRenderbuffer(GL_RENDERBUFFER, this->render_buffer_id);
129 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
130 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
131 GL_RENDERBUFFER, this->render_buffer_id);
132
133 screen::WIDTH = width;
134 screen::HEIGHT = height;
135}
136
137void framebuffer::set_format(GLenum format)
138{
139 this->format = format;
140}
static int WIDTH
Width of the window.
Definition screen.hpp:53
static int HEIGHT
Height of the window.
Definition screen.hpp:57
~framebuffer()
Destructor Deletes the framebuffer and its texture.
GLuint texture_id
Itexture ID.
void destroy()
Delete the framebuffer and its texture.
void unbind()
Unbind the framebuffer.
GLuint render_buffer_id
Render Buffer ID.
void set_format(GLenum format)
Set the format of the framebuffer.
GLenum format
Format of the framebuffer Default is GL_RGBA.
void rescale(int width, int height)
Rescale the framebuffer.
void bind()
Bind the framebuffer.
framebuffer()
Empty constructor Does nothing.