forked from raphaelmenges/MolecularDynamicsVisualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFramebuffer.cpp
More file actions
148 lines (124 loc) · 4.2 KB
/
Framebuffer.cpp
File metadata and controls
148 lines (124 loc) · 4.2 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//============================================================================
// Distributed under the MIT License. Author: Raphael Menges
//============================================================================
#include "Framebuffer.h"
Framebuffer::Framebuffer(int width, int height, bool superSampling)
{
// Generate framebuffer and renderbuffer for depth and stencil tests
glGenFramebuffers(1, &mFramebuffer);
glGenRenderbuffers(1, &mDepthStencil);
// Bind framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
// Create render buffer for depth and stencil and save width and height
resize(width, height, superSampling);
// Unbind framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Framebuffer::~Framebuffer()
{
glDeleteFramebuffers(1, &mFramebuffer);
glDeleteRenderbuffers(1, &mDepthStencil);
for(const auto& rPair : colorAttachments)
{
glDeleteTextures(1, &rPair.first);
}
}
void Framebuffer::bind() const
{
glViewport(0, 0, mWidth, mHeight);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
}
void Framebuffer::unbind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
bool Framebuffer::resize(int width, int height)
{
// Apply super sampling if indicated
if(mSuperSampling)
{
width *= mSuperSamplingMultiplier;
height *= mSuperSamplingMultiplier;
}
// Only continue when changed
if(width != mWidth || height != mHeight)
{
// Save width and height
mWidth = width;
mHeight = height;
// Create renderbuffer
glBindRenderbuffer(GL_RENDERBUFFER, mDepthStencil);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, mWidth, mHeight);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// (Re)Bind depth and stencil
glFramebufferRenderbuffer(
GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mDepthStencil);
// Do it for all color attachments
for(const auto& rPair : colorAttachments)
{
glBindTexture(GL_TEXTURE_2D, rPair.first);
glTexImage2D(
GL_TEXTURE_2D,
0,
rPair.second == ColorFormat::RGB ? GL_RGB8 : GL_RGBA8,
mWidth,
mHeight,
0,
rPair.second == ColorFormat::RGB ? GL_RGB : GL_RGBA,
GL_UNSIGNED_BYTE,
NULL);
glBindTexture(GL_TEXTURE_2D, 0);
}
return true;
}
else
{
return false;
}
}
bool Framebuffer::resize(int width, int height, bool superSampling)
{
mSuperSampling = superSampling;
return resize(width, height);
}
void Framebuffer::addAttachment(ColorFormat colorFormat)
{
// Generate new texture
GLuint texture = 0;
glGenTextures(1, &texture);
// Bind texture and set it up
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
// Reserve space for texture
glTexImage2D(
GL_TEXTURE_2D,
0,
colorFormat == ColorFormat::RGB ? GL_RGB8 : GL_RGBA8,
mWidth,
mHeight,
0,
colorFormat == ColorFormat::RGB ? GL_RGB : GL_RGBA,
GL_UNSIGNED_BYTE,
NULL);
// Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
// Find out which color attachment should be used
int count = colorAttachments.size();
GLenum attachmentNumber = GL_COLOR_ATTACHMENT0;
attachmentNumber += count;
// Bind texture to framebuffer
glFramebufferTexture2D(
GL_FRAMEBUFFER, attachmentNumber, GL_TEXTURE_2D, texture, 0);
// Tell framebuffer how many attachments to fill
std::vector<GLenum> attachmentIdentifiers;
for(int i = 0; i <= count; i++)
{
attachmentIdentifiers.push_back(GL_COLOR_ATTACHMENT0 + i);
}
glDrawBuffers(attachmentIdentifiers.size(), attachmentIdentifiers.data());
// Remember that attachment
colorAttachments.push_back(std::make_pair(texture, colorFormat));
}