Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
OpenGLFrameBuffer.cpp
1/*
2Copyright (C) 2019,2021,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
17#include "OpenGLFrameBuffer.hpp"
18
19namespace AeonGames
20{
21 OpenGLFrameBuffer::OpenGLFrameBuffer() = default;
22 OpenGLFrameBuffer::~OpenGLFrameBuffer()
23 {
24 Finalize();
25 }
26
28 {
29 // Frame Buffer
30 glGenFramebuffers ( 1, &mFBO );
31 OPENGL_CHECK_ERROR_THROW;
32 glBindFramebuffer ( GL_FRAMEBUFFER, mFBO );
33 OPENGL_CHECK_ERROR_THROW;
34
35 // Color Buffer
36 glGenTextures ( 1, &mColorBuffer );
37 OPENGL_CHECK_ERROR_THROW;
38 glBindTexture ( GL_TEXTURE_2D, mColorBuffer );
39 OPENGL_CHECK_ERROR_THROW;
40 glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr );
41 OPENGL_CHECK_ERROR_THROW;
42 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
43 OPENGL_CHECK_ERROR_THROW;
44 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
45 OPENGL_CHECK_ERROR_THROW;
46 glBindTexture ( GL_TEXTURE_2D, 0 );
47 OPENGL_CHECK_ERROR_THROW;
48
49 glFramebufferTexture2D ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColorBuffer, 0 );
50 OPENGL_CHECK_ERROR_THROW;
51 // Render Buffer
52 glGenRenderbuffers ( 1, &mRBO );
53 OPENGL_CHECK_ERROR_THROW;
54 glBindRenderbuffer ( GL_RENDERBUFFER, mRBO );
55 OPENGL_CHECK_ERROR_THROW;
56 glRenderbufferStorage ( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600 );
57 OPENGL_CHECK_ERROR_THROW;
58 glBindRenderbuffer ( GL_RENDERBUFFER, 0 );
59 OPENGL_CHECK_ERROR_THROW;
60
61 glFramebufferRenderbuffer ( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mRBO );
62 OPENGL_CHECK_ERROR_THROW;
63 if ( glCheckFramebufferStatus ( GL_FRAMEBUFFER ) != GL_FRAMEBUFFER_COMPLETE )
64 {
65 std::cout << LogLevel::Error << "Incomplete Framebuffer." << std::endl;
66 throw std::runtime_error ( "Incomplete Framebuffer." );
67 }
68 OPENGL_CHECK_ERROR_THROW;
69
70 glBindFramebuffer ( GL_FRAMEBUFFER, 0 );
71 OPENGL_CHECK_ERROR_THROW;
72 }
73
75 {
76 if ( glIsRenderbuffer ( mRBO ) )
77 {
78 OPENGL_CHECK_ERROR_NO_THROW;
79 glDeleteRenderbuffers ( 1, &mRBO );
80 OPENGL_CHECK_ERROR_NO_THROW;
81 mRBO = 0;
82 }
83 if ( glIsTexture ( mColorBuffer ) )
84 {
85 OPENGL_CHECK_ERROR_NO_THROW;
86 glDeleteTextures ( 1, &mColorBuffer );
87 OPENGL_CHECK_ERROR_NO_THROW;
88 mColorBuffer = 0;
89 }
90 if ( glIsFramebuffer ( mFBO ) )
91 {
92 OPENGL_CHECK_ERROR_NO_THROW;
93 glDeleteFramebuffers ( 1, &mFBO );
94 OPENGL_CHECK_ERROR_NO_THROW;
95 mFBO = 0;
96 }
97 OPENGL_CHECK_ERROR_NO_THROW;
98 }
99
100 void OpenGLFrameBuffer::Resize ( uint32_t aWidth, uint32_t aHeight )
101 {
102 glBindTexture ( GL_TEXTURE_2D, mColorBuffer );
103 glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, aWidth, aHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr );
104 glBindRenderbuffer ( GL_RENDERBUFFER, mRBO );
105 glRenderbufferStorage ( GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, aWidth, aHeight );
106 glBindRenderbuffer ( GL_RENDERBUFFER, 0 );
107 }
109 {
110 glBindFramebuffer ( GL_FRAMEBUFFER, mFBO );
111 OPENGL_CHECK_ERROR_NO_THROW;
112 }
114 {
115 glBindFramebuffer ( GL_FRAMEBUFFER, 0 );
116 OPENGL_CHECK_ERROR_NO_THROW;
117 }
119 {
120 return mFBO;
121 }
122
123 OpenGLFrameBuffer::OpenGLFrameBuffer ( OpenGLFrameBuffer&& aOpenGLFrameBuffer )
124 {
125 std::swap ( mFBO, aOpenGLFrameBuffer.mFBO );
126 std::swap ( mColorBuffer, aOpenGLFrameBuffer.mColorBuffer );
127 std::swap ( mRBO, aOpenGLFrameBuffer.mRBO );
128 }
129}
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
void Finalize()
Release framebuffer resources.
void Unbind()
Unbind the framebuffer, restoring the default render target.
GLuint GetFBO() const
Get the OpenGL framebuffer object identifier.
void Resize(uint32_t aWidth, uint32_t aHeight)
Resize the framebuffer attachments.
void Initialize()
Create and initialize framebuffer resources.
void Bind()
Bind the framebuffer as the active render target.
<- This is here just for the literals
Definition AABB.hpp:31
@ Error
Error conditions.
Definition LogLevel.hpp:33