-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFramebuffer.cpp
More file actions
123 lines (103 loc) · 3.08 KB
/
Framebuffer.cpp
File metadata and controls
123 lines (103 loc) · 3.08 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
#include "Framebuffer.hpp"
#include "Config.hpp"
#include "DrawBase.hpp"
#include "RenderTarget.hpp"
float Framebuffer::rectData[18] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f
};
Framebuffer::Framebuffer(int width, int height, bool hdr, ShaderBase::Ptr shader) : RenderTarget(), BufferGeo()
{
_width = width;
_height = height;
_shader = shader;
glGenTextures(1, &_texture);
glBindTexture(GL_TEXTURE_2D, _texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (!hdr)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, nullptr);
}
glGenRenderbuffers(1, &_rbo);
glBindRenderbuffer(GL_RENDERBUFFER, _rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glGenFramebuffers(1, &_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _rbo);
glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);
_hdr = hdr;
_clearColor = glm::vec4(1.0, 0.0, 1.0, 1.0);
// Configure the geometry (basically a rect)
configure(rectData, 6, sizeof(rectData), shader);
}
Framebuffer::~Framebuffer()
{
glDeleteFramebuffers(1, &_fbo);
glDeleteBuffers(1, &_rbo);
glDeleteTextures(1, &_texture);
}
void Framebuffer::clear(glm::vec4 clearColor)
{
_clearColor = clearColor;
}
void Framebuffer::drawFrame()
{
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
stacks().viewportStack.push({ 0, 0, _width, _height });
stacks().framebufferStack.push(_fbo);
glViewport(0, 0, _width, _height);
glClearColor(_clearColor.x, _clearColor.y, _clearColor.z, _clearColor.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (const DrawBase::Ptr &draw : _drawList)
{
draw->draw();
}
stacks().viewportStack.pop();
stacks().framebufferStack.pop();
const Viewport &oldStack = stacks().viewportStack.top();
glViewport(oldStack.x, oldStack.y, oldStack.w, oldStack.h);
if (!stacks().framebufferStack.empty())
{
glBindFramebuffer(GL_FRAMEBUFFER, stacks().framebufferStack.top());
}
else
{
glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);
}
}
void Framebuffer::addDrawable(DrawBase::Ptr draw)
{
_drawList.push_back(draw);
}
void Framebuffer::clearDrawables()
{
_drawList.clear();
}
void Framebuffer::draw()
{
BufferGeo::draw();
}
GLuint Framebuffer::getTexture() const
{
return _texture;
}
int Framebuffer::getWidth() const
{
return _width;
}
int Framebuffer::getHeight() const
{
return _height;
}