-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoF.cpp
More file actions
193 lines (151 loc) · 5.32 KB
/
DoF.cpp
File metadata and controls
193 lines (151 loc) · 5.32 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "DoF.h"
using namespace std;
using namespace glm;
GLuint frameBuffer;
GLuint colormap;
GLuint depthmap;
GLuint dofVAO, dofVBO;
// Quad vertices
GLfloat quadVertices[] = {
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
};
// need to be called after the framebuffer has been created
void createColorDepthMap() {
glGenTextures(1, &depthmap);
glGenTextures(1, &colormap);
// DEPTH
glBindTexture(GL_TEXTURE_2D, depthmap);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, Window::width, Window::height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthmap, 0
);
// COLOR
glBindTexture(GL_TEXTURE_2D, colormap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, Window::width, Window::height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colormap, 0
);
}
void createFrameBuffer() {
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
}
DoF::DoF(GLuint program) {
aperture = 2.0f;
focus = 1.0f;
max_blur = 1.0f;
// create the frame buffer and depth buffer color buffer
glGenVertexArrays(1, &dofVAO);
glGenBuffers(1, &dofVBO);
glBindVertexArray(dofVAO);
glBindBuffer(GL_ARRAY_BUFFER, dofVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);
GLint posAttrib = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
GLint texAttrib = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// create them
createFrameBuffer();
createColorDepthMap();
// check if it is completed
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (err == GL_FRAMEBUFFER_COMPLETE) {
cout << "frame buffer is completed" << endl;
}
}
DoF::~DoF() {}
void DOF_Postprocessing(GLuint program, float aperture, float focus, float maxBlur) {
glUseProgram(program);
glBindVertexArray(dofVAO);
glUniform1f(glGetUniformLocation(program, "maxBlur"), maxBlur);
glUniform1f(glGetUniformLocation(program, "aperture"), aperture);
glUniform1f(glGetUniformLocation(program, "focus"), focus);
glUniform1f(glGetUniformLocation(program, "aspect"), Window::width / (float)Window::height);
//cout << "the sizes are " << Window::width << " and " << Window::height << " and " << Window::width / (float)Window::height << endl;
// bindColormap
glUniform1i(glGetUniformLocation(program, "texture"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colormap);
// bind ddepthmap
glUniform1i(glGetUniformLocation(program, "tDepth"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthmap);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
void DoF::bindFrameBuffer() {
// use the customized framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glBindTexture(GL_TEXTURE_2D, depthmap);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, Window::width, Window::height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthmap, 0
);
// COLOR
glBindTexture(GL_TEXTURE_2D, colormap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, Window::width, Window::height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colormap, 0
);
}
void DoF::dof_post_processing(GLuint program) {
// unbind the framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// DOF post processing
DOF_Postprocessing(program, aperture, focus, max_blur);
}
// controls
void DoF::increase_focus() {
// cap to 1
float temp = focus + 0.001f;
focus = temp;
cout << "focus is " << focus << endl;
}
void DoF::decrease_focus() {
// cap to 1
float temp = focus - 0.001f;
focus = temp;
cout << "focus is " << focus << endl;
}
void DoF::reset_focus() {
focus = 1.0f;
}
void DoF::increase_aperture() {
aperture += 0.1f;
cout << "aperture is " << aperture << endl;
}
void DoF::decrease_aperture() {
aperture -= 0.1f;
cout << "aperture is " << aperture << endl;
}