-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameBuffer.cpp
More file actions
112 lines (89 loc) · 3.3 KB
/
FrameBuffer.cpp
File metadata and controls
112 lines (89 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "FrameBuffer.h"
#include "logger.h"
namespace Engine
{
FrameBuffer::FrameBuffer(int width, int height, int frameTypeFlag, int atachment, GLint colorInternalFormat) {
m_width = width;
m_height = height;
m_frameType = frameTypeFlag;
//framebuffer
glGenFramebuffers(1, &m_FramebufferID);
glBindFramebuffer(GL_FRAMEBUFFER, m_FramebufferID);
if (!glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) {
error_OnLoad = true;
log_error("ERROR::FRAMEBUFFER::FAILED_TO_CREATE_FRAMEBUFFER");
unbind();
return;
}
//Color texture
if (frameTypeFlag & FrameType_Color) {
glGenTextures(1, &m_ColorID);
glBindTexture(GL_TEXTURE_2D, m_ColorID);
// Give an empty image to OpenGL ( the last "0" )
glTexImage2D(GL_TEXTURE_2D, 0, colorInternalFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//set texture as color atachemnt
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + atachment, GL_TEXTURE_2D, m_ColorID, 0);
}
//Depth texture
if (frameTypeFlag & FrameType_Depth) {
glGenTextures(1, &m_DepthID);
glBindTexture(GL_TEXTURE_2D, m_DepthID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_DepthID, 0);
}
// Set the list of draw buffers.
GLenum DrawBuffers[2];
switch (frameTypeFlag)
{
case FrameType_Color:
DrawBuffers[0] = GL_COLOR_ATTACHMENT0 + atachment;
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
break;
case FrameType_Depth:
DrawBuffers[0] = GL_DEPTH_ATTACHMENT;
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
break;
case FrameType_Color | FrameType_Depth:
DrawBuffers[0] = GL_COLOR_ATTACHMENT0 + atachment;
DrawBuffers[1] = GL_DEPTH_ATTACHMENT;
glDrawBuffers(2, DrawBuffers); // "2" is the size of DrawBuffers
break;
default:
error_OnLoad = true;
log_error("ERROR::FRAMEBUFFER::INVALID_FRAME_TYPE");
unbind();
return;
break;
}
error_OnLoad = false;
unbind();
}
void FrameBuffer::bind(bool changeViewport)
{
if (error_OnLoad) {
log_error("ERROR::FRAMEBUFFER::FAILED_TO_BIND_FRAMEBUFFER");
return;
}
glBindFramebuffer(GL_FRAMEBUFFER, m_FramebufferID);
if(changeViewport)
glViewport(0, 0, m_width, m_height);
}
void FrameBuffer::bindColorTexture(int textureUnit) const
{
glActiveTexture(GL_TEXTURE0 + textureUnit); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, m_ColorID);
}
void FrameBuffer::bindDepthTexture(int textureUnit) const
{
glActiveTexture(GL_TEXTURE0 + textureUnit); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, m_DepthID);
}
}