-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframebuffer.cpp
104 lines (87 loc) · 2.63 KB
/
framebuffer.cpp
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
#include <iostream>
#include "framebuffer.h"
Framebuffer::Framebuffer(int width_, int height_, bool depth_)
{
id = 0;
depth = depth_;
width = width_;
height = height_;
renderTexture = new Texture2D();
}
Framebuffer::~Framebuffer()
{
glDeleteRenderbuffers(1, &depthId);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &id);
delete renderTexture;
}
void Framebuffer::Resize(int width_, int height_)
{
width = width_;
height = height_;
// TODO : only if depth
glBindRenderbuffer(GL_RENDERBUFFER_EXT, depthId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
renderTexture->Resize(width, height);
}
void Framebuffer::ScaleRenderTarget(float ratio)
{
renderTexture->Resize(width * ratio, height * ratio);
}
void Framebuffer::Start(float downSamplingRatio)
{
assert(downSamplingRatio <= 1.0);
// Down scale render target.
ScaleRenderTarget(downSamplingRatio);
glViewport(0, 0, width * downSamplingRatio, height * downSamplingRatio);
glBindFramebuffer(GL_FRAMEBUFFER, id);
Clear();
}
void Framebuffer::End()
{
glViewport(0, 0, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::Bind(Shader *shader, glm::vec2 resolution, string textureName) const
{
// TODO : move this to renderer
shader->Use();
renderTexture->Bind();
shader->SetTexture(textureName, renderTexture);
shader->SetVector2f("resolution", resolution);
}
void Framebuffer::Clear() const
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Framebuffer::AttachTexture(Texture2D *renderTexture_, string textureName_)
{
renderTexture = renderTexture_;
textureName = textureName_;
}
int Framebuffer::Width() const
{
return width;
}
int Framebuffer::Height() const
{
return height;
}
Texture2D *Framebuffer::GetRenderTexture() const
{
return renderTexture;
}
void Framebuffer::Init()
{
glGenFramebuffers(1, &id);
glBindFramebuffer(GL_FRAMEBUFFER, id);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTexture->ID, 0);
// TODO : make a test if we want depth testing to take place to this fbo
glGenRenderbuffers(1, &depthId);
glBindRenderbuffer(GL_RENDERBUFFER_EXT, depthId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthId);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}