sculpto
gl_frame_buffer.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file gl_frame_buffer.cpp
3 * \brief OpenGL frame buffer class implementation class.
4 *
5 * \author Sabitov Kirill
6 * \date 06 July 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10#include "gl_frame_buffer.h"
11#include "gl_texture.h"
12
14{
15 this->Props = Props;
16 this->Invalidate();
17 SCL_CORE_INFO("OpenGL Frame Buffer with id {} props changed.", Id);
18}
19
21{
22 return Props;
23}
24
26{
27 return ColorAttachments[Index];
28}
29
31{
32 return DepthAttachment;
33}
34
35void scl::gl_frame_buffer::Invalidate()
36{
37 if (Id != 0) this->Free();
38
39 if (Props.IsSwapChainTarget) {
40 Id = 0;
41 ClearConfig = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
42 SCL_CORE_INFO("OpenGL Frame Buffer swap chain target invalidated.");
43 return;
44 }
45
46 // Create and bind frame buffer
47 glGenFramebuffers(1, &Id);
48 glBindFramebuffer(GL_FRAMEBUFFER, Id);
49
50 // Props validation
51 SCL_CORE_ASSERT(Props.ColorAttachmentsCount > 0 || Props.DepthAttachmentsCount > 0, "At least one of attachments count must be > 0.");
52 SCL_CORE_ASSERT(Props.DepthAttachmentsCount <= 8, "OpenGL support max 8 collor attachments per frame buffer.");
53 SCL_CORE_ASSERT(Props.DepthAttachmentsCount <= 1, "OpenGL support max 1 depth attachment per frame buffer.");
54
55 // Frame buffer color and depth attachments creation
56 std::vector<GLenum> active_color_attachments {};
57 image viewport(Props.Width, Props.Height, 4, false);
58 ColorAttachments.resize(Props.ColorAttachmentsCount);
59 for (int i = 0; i < Props.ColorAttachmentsCount; i++) {
60 texture_type color_attachment_texture_type = Props.IsHDR ? texture_type::COLOR_FLOATING_POINT : texture_type::COLOR;
61 ColorAttachments[i] = texture_2d::Create(viewport, color_attachment_texture_type);
62 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, ColorAttachments[i]->GetHandle(), 0);
63 active_color_attachments.push_back(GL_COLOR_ATTACHMENT0 + i);
64 }
65 if (Props.ColorAttachmentsCount == 0) glDrawBuffer(GL_NONE), glReadBuffer(GL_NONE);
66 else glDrawBuffers(Props.ColorAttachmentsCount, active_color_attachments.data());
67
68 if (Props.DepthAttachmentsCount == 1) {
69 DepthAttachment = texture_2d::Create(viewport, texture_type::DEPTH);
70 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DepthAttachment->GetHandle(), 0);
71 }
72
73 // Set frame buffer clear config depending on attachments count
74 ClearConfig = ((Props.ColorAttachmentsCount > 0) * GL_COLOR_BUFFER_BIT) |
75 ((Props.DepthAttachmentsCount > 0) * GL_DEPTH_BUFFER_BIT);
76
77 // Check frame buffer status, unbinding
78 SCL_CORE_ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Frame buffer creation error!");
79 glBindFramebuffer(GL_FRAMEBUFFER, 0);
80
81 SCL_CORE_INFO("OpenGL Frame Buffer with id {}, width {}, height {}, color attachments {}, depth attachments invalidated.",
82 Id, Props.Width, Props.Height, Props.ColorAttachmentsCount, Props.DepthAttachmentsCount);
83}
84
86 Props(Props)
87{
88 this->Invalidate();
89 SCL_CORE_SUCCES("OpenGL Frame Buffer with id {} created.", Id);
90}
91
93{
94 this->Free();
95}
96
98{
99 glBindFramebuffer(GL_FRAMEBUFFER, Id);
100 glViewport(0, 0, Props.Width, Props.Height);
101}
102
104{
105 glBindFramebuffer(GL_FRAMEBUFFER, 0);
106}
107
108void scl::gl_frame_buffer::Resize(int Width, int Height)
109{
110 Props.Width = Width;
111 Props.Height = Height;
112 this->Invalidate();
113}
114
116{
117 if (Id != 0) glDeleteFramebuffers(1, &Id);
118
119 for (auto &color_attachment : ColorAttachments) color_attachment.reset();
120 ColorAttachments.clear();
121
122 if (DepthAttachment) DepthAttachment.reset();
123
124 SCL_CORE_INFO("OpenGL Frame Buffer with id {} freed.", Id);
125 Id = 0;
126}
127
129{
130 this->Bind();
131 glClear(ClearConfig);
132 this->Unbind();
133}
#define SCL_CORE_ASSERT(expr,...)
Definition: assert.h:69
const shared< texture_2d > & GetColorAttachment(int Index) const override
void SetFrameBufferProps(const frame_buffer_props &Props)
void Bind() const override
gl_frame_buffer(const frame_buffer_props &Props)
void Resize(int Width, int Height) override
const frame_buffer_props & GetFrameBufferProps() const
void Unbind() const override
const shared< texture_2d > & GetDepthAttachment(int Index) const override
static shared< texture_2d > Create(const image &Image, texture_type Type=texture_type::COLOR)
Definition: texture.cpp:14
OpenGL frame buffer class definition class.
OpenGL texture class definition module.
#define SCL_CORE_SUCCES(...)
Definition: log.h:42
#define SCL_CORE_INFO(...)
Definition: log.h:41
std::shared_ptr< T > shared
Definition: smart_ptr.h:15
texture_type
Definition: texture.h:18
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.