-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameBufferObject.cpp
More file actions
126 lines (105 loc) · 3.21 KB
/
Copy pathFrameBufferObject.cpp
File metadata and controls
126 lines (105 loc) · 3.21 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Common.h"
#include "FrameBufferObject.h"
CFrameBufferObject::CFrameBufferObject()
: m_iWidth(0),
m_iHeight(0),
m_uiFramebuffer(0),
m_uiColourTexture(0),
m_uiDepthTexture(0),
m_uiSampler(0)
{
}
CFrameBufferObject::~CFrameBufferObject()
{
Release();
}
bool CFrameBufferObject::Create(int width, int height)
{
if (m_uiFramebuffer != 0 || width <= 0 || height <= 0) {
return false;
}
glGenFramebuffers(1, &m_uiFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_uiFramebuffer);
glGenTextures(1, &m_uiColourTexture);
glBindTexture(GL_TEXTURE_2D, m_uiColourTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glGenerateMipmap(GL_TEXTURE_2D);
glGenSamplers(1, &m_uiSampler);
SetSamplerObjectParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
SetSamplerObjectParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SetSamplerObjectParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
SetSamplerObjectParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &m_uiDepthTexture);
glBindTexture(GL_TEXTURE_2D, m_uiDepthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_uiColourTexture, 0);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_uiDepthTexture, 0);
static const GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, drawBuffers);
m_iWidth = width;
m_iHeight = height;
bool complete = (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return complete;
}
void CFrameBufferObject::Bind(bool setFullViewport)
{
glBindFramebuffer(GL_FRAMEBUFFER, m_uiFramebuffer);
if (setFullViewport) {
glViewport(0, 0, m_iWidth, m_iHeight);
}
glm::vec4 clearColour = glm::vec4(0.08f, 0.09f, 0.14f, 1.0f);
float one = 1.0f;
glClearBufferfv(GL_COLOR, 0, &clearColour.r);
glClearBufferfv(GL_DEPTH, 0, &one);
}
void CFrameBufferObject::BindTexture(int textureUnit)
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, m_uiColourTexture);
glBindSampler(textureUnit, m_uiSampler);
glGenerateMipmap(GL_TEXTURE_2D);
}
void CFrameBufferObject::BindDepth(int textureUnit)
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, m_uiDepthTexture);
glBindSampler(textureUnit, m_uiSampler);
}
void CFrameBufferObject::Release()
{
if (m_uiFramebuffer) {
glDeleteFramebuffers(1, &m_uiFramebuffer);
m_uiFramebuffer = 0;
}
if (m_uiSampler) {
glDeleteSamplers(1, &m_uiSampler);
m_uiSampler = 0;
}
if (m_uiColourTexture) {
glDeleteTextures(1, &m_uiColourTexture);
m_uiColourTexture = 0;
}
if (m_uiDepthTexture) {
glDeleteTextures(1, &m_uiDepthTexture);
m_uiDepthTexture = 0;
}
m_iWidth = 0;
m_iHeight = 0;
}
void CFrameBufferObject::SetSamplerObjectParameter(GLenum parameter, GLenum value)
{
glSamplerParameteri(m_uiSampler, parameter, value);
}
void CFrameBufferObject::SetSamplerObjectParameterf(GLenum parameter, float value)
{
glSamplerParameterf(m_uiSampler, parameter, value);
}
int CFrameBufferObject::GetWidth()
{
return m_iWidth;
}
int CFrameBufferObject::GetHeight()
{
return m_iHeight;
}