-
Notifications
You must be signed in to change notification settings - Fork 0
/
glRenderer.cpp
372 lines (291 loc) · 12.3 KB
/
glRenderer.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// Created by Eric Fang on 2/7/17.
//
#include "glRenderer.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define cudaCheck(x) \
{ \
cudaError_t err = x; \
if (err != cudaSuccess) { \
printf("Cuda error: %d in %s at %s:%d\n", err, #x, __FILE__, __LINE__); \
assert(0); \
} \
}
const GLfloat glRenderer::skyBoxVertices[108] = {
// Positions
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f};
const GLfloat glRenderer::quadVertices[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,
};
GLuint glRenderer::loadTexture(GLchar *path) {
// Generate texture ID and load texture data
GLuint textureID;
glGenTextures(1, &textureID);
int width, height, n;
unsigned char *image = stbi_load(path, &width, &height, &n, 0);
// Assign texture to ID
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
// Parameters
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_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
delete image;
return textureID;
}
GLuint glRenderer::loadCubeMap(std::vector<std::string> faces) {
GLuint textureID;
glGenTextures(1, &textureID);
int width, height, n;
unsigned char *image;
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
for (GLuint i = 0; i < faces.size(); i++) {
image = stbi_load(faces[i].c_str(), &width, &height, &n, 0);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, image);
delete image;
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return textureID;
}
int glRenderer::init() {
setupParticles();
setupSkyBox();
setupQuad();
textureOne = setupTexture();
textureTwo = setupTexture();
firstFBO = setupFBO(textureOne);
secondFBO = setupFBO(textureTwo);
return 0;
}
void glRenderer::onDraw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 projection = glm::perspective(
camera.Zoom, static_cast<float>(width) / static_cast<float>(height), 0.1f,
1000.f);
updateBuffer();
depthPass(projection, firstFBO, textureOne);
blurPass(projection, secondFBO, textureOne, textureTwo, 0);
blurPass(projection, firstFBO, textureTwo, textureOne, 1);
shadingPass(projection, textureOne);
drawSkyBox(projection);
}
void glRenderer::setupSkyBox() {
glGenVertexArrays(1, &skyBoxVAO);
glGenBuffers(1, &skyBoxVBO);
glBindVertexArray(skyBoxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyBoxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyBoxVertices), &skyBoxVertices,
GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
std::vector<std::string> faces =
{"skybox/right.jpg", "skybox/left.jpg", "skybox/top.jpg", "skybox/bottom.jpg", "skybox/back.jpg",
"skybox/front.jpg"};
cubeMapTexture = loadCubeMap(faces);
}
void glRenderer::drawSkyBox(glm::mat4 projection) {
// Draw skybox as last
glDepthFunc(GL_LEQUAL); // Change depth function so depth test passes when
// values are equal to depth buffer's content
skyBoxShader.Use();
glm::mat4 view =
glm::mat4(glm::mat3(camera.GetViewMatrix())); // Remove any translation
// component of the view
// matrix
glUniformMatrix4fv(glGetUniformLocation(skyBoxShader.Program, "view"), 1,
GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(skyBoxShader.Program, "projection"),
1, GL_FALSE, glm::value_ptr(projection));
// skybox cube
glBindVertexArray(skyBoxVAO);
glActiveTexture(GL_TEXTURE0);
// glUniform1i(glGetUniformLocation(shader.Program, "skybox"), 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS); // Set depth function back to default
}
void glRenderer::blurPass(glm::mat4 projection, GLuint FBO, GLuint textureIn,
GLuint textureOut, int direction) {
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, textureOut, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
blurShader.Use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureIn);
glUniform1i(glGetUniformLocation(blurShader.Program, "renderedTexture"), 0);
glUniformMatrix4fv(glGetUniformLocation(blurShader.Program, "projection"), 1,
GL_FALSE, glm::value_ptr(projection));
glUniform1i(glGetUniformLocation(blurShader.Program, "direction"), direction);
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
void glRenderer::setupQuad() {
glGenBuffers(1, &quadVBO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices),
static_cast<const GLvoid *>(quadVertices), GL_STATIC_DRAW);
glGenVertexArrays(1, &quadVAO);
glBindVertexArray(quadVAO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
GLuint glRenderer::setupFBO(GLuint texture) {
GLuint FBO;
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, texture, 0);
// No color output in the bound framebuffer, only depth.
glDrawBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cout << "framebuffer not ok!" << std::endl;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return FBO;
}
GLuint glRenderer::setupTexture() {
GLint dims[4] = {0};
glGetIntegerv(GL_VIEWPORT, static_cast<GLint *>(dims));
GLint fbWidth = dims[2];
GLint fbHeight = dims[3];
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, fbWidth, fbHeight, 0,
GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
// GL_COMPARE_R_TO_TEXTURE);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
void glRenderer::setupParticles() {
glGenBuffers(1, &particleVBO);
glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
#ifdef DEVICE_RENDER
glBufferData(GL_ARRAY_BUFFER, simulation.getParticleNum() * sizeof(float) * 3,
0, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
cudaGraphicsGLRegisterBuffer(resources, particleVBO,
cudaGraphicsRegisterFlagsNone);
#else
glBufferData(GL_ARRAY_BUFFER, simulation.getParticleNum() * sizeof(float) * 3,
simulation.getParticlePos(), GL_STREAM_DRAW);
glGenVertexArrays(1, &particleVAO);
glBindVertexArray(particleVAO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
#endif /* DEVICE_RENDER */
}
void glRenderer::updateBuffer() {
// Update the VBO
#ifdef DEVICE_RENDER
void* particlePosPtr;
cudaCheck(cudaGraphicsMapResources(1, resources));
size_t size;
cudaGraphicsResourceGetMappedPointer(&particlePosPtr, &size, resources[0]);
float* pos = simulation.getParticlePos();
cudaCheck(cudaMemcpy(particlePosPtr, pos,
simulation.getParticleNum() * sizeof(float) * 3,
cudaMemcpyDeviceToDevice));
cudaGraphicsUnmapResources(1, resources);
#else
glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0,
simulation.getParticleNum() * sizeof(float) * 3,
simulation.getParticlePos());
#endif /* DEVICE_RENDER */
}
void glRenderer::depthPass(glm::mat4 projection, GLuint FBO,
GLuint textureOut) {
depthShader.Use();
// Create camera transformation
glm::mat4 model;
glm::mat4 view = camera.GetViewMatrix();
GLfloat pointRadius = 2.0f;
GLfloat pointScale = 1000.0f;
// Pass the matrices to the shader
glUniformMatrix4fv(glGetUniformLocation(depthShader.Program, "model"), 1,
GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(glGetUniformLocation(depthShader.Program, "view"), 1,
GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(depthShader.Program, "projection"), 1,
GL_FALSE, glm::value_ptr(projection));
glUniform1f(glGetUniformLocation(depthShader.Program, "pointRadius"),
pointRadius);
glUniform1f(glGetUniformLocation(depthShader.Program, "pointScale"),
pointScale);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, textureOut, 0);
glBindVertexArray(particleVAO);
glDrawArrays(GL_POINTS, 0, simulation.getParticleNum());
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void glRenderer::shadingPass(glm::mat4 projection, GLuint textureIn) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shadingShader.Use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureIn);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMapTexture);
// glm::mat4 view = camera.GetViewMatrix();
glUniform1i(glGetUniformLocation(shadingShader.Program, "renderedTexture"),
0);
glUniform1i(glGetUniformLocation(shadingShader.Program, "skybox"), 1);
// glUniformMatrix4fv(glGetUniformLocation(depthShader.Program, "view"), 1,
// GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shadingShader.Program, "projection"),
1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
glRenderer::~glRenderer() {
#ifdef DEVICE_RENDER
cudaDeviceSynchronize();
cudaGraphicsUnregisterResource(*resources);
#endif /* DEVICE_RENDER */
glDeleteVertexArrays(1, &particleVAO);
glDeleteVertexArrays(1, &skyBoxVAO);
glDeleteBuffers(1, &particleVBO);
glDeleteBuffers(1, &skyBoxVBO);
}