diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-02-15 15:23:19 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-02-15 15:23:19 -0500 |
commit | 783b308990e7c4ef0837a102a138778f73e4d2b7 (patch) | |
tree | 77931fcf919f8c8e7996430994c452376aae293f /src | |
parent | 297c093d398e7d6e5fcc5dc6ba1056ede25abf6f (diff) | |
download | therapy-783b308990e7c4ef0837a102a138778f73e4d2b7.tar.gz therapy-783b308990e7c4ef0837a102a138778f73e4d2b7.tar.bz2 therapy-783b308990e7c4ef0837a102a138778f73e4d2b7.zip |
Added bloom!
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 44 | ||||
-rw-r--r-- | src/renderer.cpp | 73 |
2 files changed, 72 insertions, 45 deletions
diff --git a/src/main.cpp b/src/main.cpp index 7c1a21f..e1e1aa2 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -5,26 +5,6 @@ | |||
5 | 5 | ||
6 | using namespace::std; | 6 | using namespace::std; |
7 | 7 | ||
8 | #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__WINDOWS__) || defined(__TOS_WIN__) | ||
9 | |||
10 | #include <windows.h> | ||
11 | |||
12 | inline void delay( unsigned long ms ) | ||
13 | { | ||
14 | Sleep( ms ); | ||
15 | } | ||
16 | |||
17 | #else /* presume POSIX */ | ||
18 | |||
19 | #include <unistd.h> | ||
20 | |||
21 | inline void delay( unsigned long ms ) | ||
22 | { | ||
23 | usleep( ms * 1000 ); | ||
24 | } | ||
25 | |||
26 | #endif | ||
27 | |||
28 | const int FRAMES_PER_SECOND = 60; | 8 | const int FRAMES_PER_SECOND = 60; |
29 | bool holding_left = false; | 9 | bool holding_left = false; |
30 | bool holding_right = false; | 10 | bool holding_right = false; |
@@ -81,21 +61,8 @@ int main() | |||
81 | 61 | ||
82 | Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); | 62 | Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); |
83 | 63 | ||
84 | double lastTime = glfwGetTime(); | ||
85 | int nbFrames = 0; | ||
86 | |||
87 | while (!quit) | 64 | while (!quit) |
88 | { | 65 | { |
89 | /* | ||
90 | double currentTime = glfwGetTime(); | ||
91 | nbFrames++; | ||
92 | if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago | ||
93 | // printf and reset timer | ||
94 | printf("%f ms/frame\n", 1000.0/double(nbFrames)); | ||
95 | nbFrames = 0; | ||
96 | lastTime += 1.0; | ||
97 | }*/ | ||
98 | |||
99 | if (holding_left && player->x_vel >= 0) | 66 | if (holding_left && player->x_vel >= 0) |
100 | { | 67 | { |
101 | player->x_vel = -2; | 68 | player->x_vel = -2; |
@@ -125,20 +92,11 @@ int main() | |||
125 | Rectangle dst_rect(player->x, player->y, player->w, player->h); | 92 | Rectangle dst_rect(player->x, player->y, player->w, player->h); |
126 | 93 | ||
127 | //blitTexture(tiles, buffer, &src_rect, &dst_rect); | 94 | //blitTexture(tiles, buffer, &src_rect, &dst_rect); |
128 | fillTexture(buffer, &dst_rect, 85, 85, 255); | 95 | fillTexture(buffer, &dst_rect, 255, 255, 255); |
129 | //fillTexture(buffer, NULL, 85, 85, 0); | ||
130 | 96 | ||
131 | renderScreen(buffer); | 97 | renderScreen(buffer); |
132 | 98 | ||
133 | //fuckThePolice(buffer); | ||
134 | |||
135 | glfwPollEvents(); | 99 | glfwPollEvents(); |
136 | |||
137 | // Regulate frame rate | ||
138 | /*if ((clock() - frame_start) < CLOCKS_PER_SEC / FRAMES_PER_SECOND) | ||
139 | { | ||
140 | //delay(((CLOCKS_PER_SEC / FRAMES_PER_SECOND) - clock() + frame_start) * CLOCKS_PER_SEC / 1000); | ||
141 | }*/ | ||
142 | } | 100 | } |
143 | 101 | ||
144 | delete map; | 102 | delete map; |
diff --git a/src/renderer.cpp b/src/renderer.cpp index ca356f3..3011e8f 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -14,6 +14,8 @@ static GLuint ntscShader; // The NTSC shader | |||
14 | static GLuint finalShader; // The passthrough shader | 14 | static GLuint finalShader; // The passthrough shader |
15 | static GLuint blitShader; // The blitting shader | 15 | static GLuint blitShader; // The blitting shader |
16 | static GLuint fillShader; // The fill shader | 16 | static GLuint fillShader; // The fill shader |
17 | static GLuint bloom1Shader; | ||
18 | static GLuint bloom2Shader; | ||
17 | 19 | ||
18 | // The buffers for the NTSC rendering process | 20 | // The buffers for the NTSC rendering process |
19 | static GLuint renderedTex1; | 21 | static GLuint renderedTex1; |
@@ -22,6 +24,8 @@ static GLuint renderedTexBufs[2]; | |||
22 | static int curBuf; | 24 | static int curBuf; |
23 | static GLuint artifactsTex; | 25 | static GLuint artifactsTex; |
24 | static GLuint scanlinesTex; | 26 | static GLuint scanlinesTex; |
27 | static GLuint preBloomTex; | ||
28 | static GLuint bloomPassTex; | ||
25 | 29 | ||
26 | GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) | 30 | GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) |
27 | { | 31 | { |
@@ -246,6 +250,23 @@ GLFWwindow* initRenderer() | |||
246 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 250 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
247 | renderedTexBufs[1] = renderedTex2; | 251 | renderedTexBufs[1] = renderedTex2; |
248 | 252 | ||
253 | // Set up bloom rendering buffers | ||
254 | glGenTextures(1, &preBloomTex); | ||
255 | glBindTexture(GL_TEXTURE_2D, preBloomTex); | ||
256 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 768, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); | ||
257 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
258 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
259 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
260 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
261 | |||
262 | glGenTextures(1, &bloomPassTex); | ||
263 | glBindTexture(GL_TEXTURE_2D, bloomPassTex); | ||
264 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 48, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); | ||
265 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
266 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
267 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
268 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
269 | |||
249 | curBuf = 0; | 270 | curBuf = 0; |
250 | 271 | ||
251 | artifactsTex = loadBMP_custom("../res/artifacts.bmp"); | 272 | artifactsTex = loadBMP_custom("../res/artifacts.bmp"); |
@@ -256,6 +277,8 @@ GLFWwindow* initRenderer() | |||
256 | finalShader = LoadShaders("../shaders/final.vertex", "../shaders/final.fragment"); | 277 | finalShader = LoadShaders("../shaders/final.vertex", "../shaders/final.fragment"); |
257 | blitShader = LoadShaders("../shaders/blit.vertex", "../shaders/blit.fragment"); | 278 | blitShader = LoadShaders("../shaders/blit.vertex", "../shaders/blit.fragment"); |
258 | fillShader = LoadShaders("../shaders/fill.vertex", "../shaders/fill.fragment"); | 279 | fillShader = LoadShaders("../shaders/fill.vertex", "../shaders/fill.fragment"); |
280 | bloom1Shader = LoadShaders("../shaders/bloom1.vertex", "../shaders/bloom1.fragment"); | ||
281 | bloom2Shader = LoadShaders("../shaders/bloom2.vertex", "../shaders/bloom2.fragment"); | ||
259 | 282 | ||
260 | rendererInitialized = true; | 283 | rendererInitialized = true; |
261 | 284 | ||
@@ -275,12 +298,16 @@ void destroyRenderer() | |||
275 | glDeleteProgram(finalShader); | 298 | glDeleteProgram(finalShader); |
276 | glDeleteProgram(blitShader); | 299 | glDeleteProgram(blitShader); |
277 | glDeleteProgram(fillShader); | 300 | glDeleteProgram(fillShader); |
301 | glDeleteProgram(bloom1Shader); | ||
302 | glDeleteProgram(bloom2Shader); | ||
278 | 303 | ||
279 | // Delete the NTSC rendering buffers | 304 | // Delete the NTSC rendering buffers |
280 | glDeleteTextures(1, &renderedTex1); | 305 | glDeleteTextures(1, &renderedTex1); |
281 | glDeleteTextures(1, &renderedTex2); | 306 | glDeleteTextures(1, &renderedTex2); |
282 | glDeleteTextures(1, &artifactsTex); | 307 | glDeleteTextures(1, &artifactsTex); |
283 | glDeleteTextures(1, &scanlinesTex); | 308 | glDeleteTextures(1, &scanlinesTex); |
309 | glDeleteTextures(1, &preBloomTex); | ||
310 | glDeleteTextures(1, &bloomPassTex); | ||
284 | 311 | ||
285 | // Delete the framebuffer | 312 | // Delete the framebuffer |
286 | glDeleteFramebuffers(1, &FramebufferName); | 313 | glDeleteFramebuffers(1, &FramebufferName); |
@@ -578,8 +605,9 @@ void renderScreen(Texture* tex) | |||
578 | glBindBuffer(GL_ARRAY_BUFFER, g_norms); | 605 | glBindBuffer(GL_ARRAY_BUFFER, g_norms); |
579 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_norms_data), g_norms_data, GL_STATIC_DRAW); | 606 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_norms_data), g_norms_data, GL_STATIC_DRAW); |
580 | 607 | ||
581 | // We're going to output to the window now | 608 | // We're going to render the screen now |
582 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 609 | //glBindFramebuffer(GL_FRAMEBUFFER, 0); |
610 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, preBloomTex, 0); | ||
583 | glViewport(0,0,1024,768); | 611 | glViewport(0,0,1024,768); |
584 | glClear(GL_COLOR_BUFFER_BIT); | 612 | glClear(GL_COLOR_BUFFER_BIT); |
585 | glUseProgram(finalShader); | 613 | glUseProgram(finalShader); |
@@ -606,6 +634,7 @@ void renderScreen(Texture* tex) | |||
606 | mat4 v_matrix = lookAt(vec3(0,0,1), vec3(0,0,0), vec3(0,1,0)); | 634 | mat4 v_matrix = lookAt(vec3(0,0,1), vec3(0,0,0), vec3(0,1,0)); |
607 | mat4 m_matrix = mat4(1.0f); | 635 | mat4 m_matrix = mat4(1.0f); |
608 | mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; | 636 | mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; |
637 | //mat4 mv_matrix = v_matrix * m_matrix; | ||
609 | 638 | ||
610 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); | 639 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); |
611 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); | 640 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); |
@@ -622,6 +651,46 @@ void renderScreen(Texture* tex) | |||
622 | glDisableVertexAttribArray(1); | 651 | glDisableVertexAttribArray(1); |
623 | glDisableVertexAttribArray(0); | 652 | glDisableVertexAttribArray(0); |
624 | 653 | ||
654 | // Do the first pass of bloom (downsampling and tapping) | ||
655 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, bloomPassTex, 0); | ||
656 | glViewport(0, 0, 64, 48); | ||
657 | glClear(GL_COLOR_BUFFER_BIT); | ||
658 | glUseProgram(bloom1Shader); | ||
659 | |||
660 | glActiveTexture(GL_TEXTURE0); | ||
661 | glBindTexture(GL_TEXTURE_2D, preBloomTex); | ||
662 | glUniform1i(glGetUniformLocation(bloom1Shader, "screenTex"), 0); | ||
663 | |||
664 | glUniform1f(glGetUniformLocation(bloom1Shader, "iGlobalTime"), glfwGetTime()); | ||
665 | |||
666 | glEnableVertexAttribArray(0); | ||
667 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | ||
668 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); | ||
669 | glDrawArrays(GL_TRIANGLES, 0, 6); | ||
670 | glDisableVertexAttribArray(0); | ||
671 | |||
672 | // Do the second pass of bloom and render to screen | ||
673 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | ||
674 | glViewport(0, 0, 1024, 768); | ||
675 | glClear(GL_COLOR_BUFFER_BIT); | ||
676 | glUseProgram(bloom2Shader); | ||
677 | |||
678 | glActiveTexture(GL_TEXTURE0); | ||
679 | glBindTexture(GL_TEXTURE_2D, preBloomTex); | ||
680 | glUniform1i(glGetUniformLocation(bloom2Shader, "screenTex"), 0); | ||
681 | |||
682 | glActiveTexture(GL_TEXTURE1); | ||
683 | glBindTexture(GL_TEXTURE_2D, bloomPassTex); | ||
684 | glUniform1i(glGetUniformLocation(bloom2Shader, "downsampledTex"), 1); | ||
685 | |||
686 | glUniform1f(glGetUniformLocation(bloom2Shader, "iGlobalTime"), glfwGetTime()); | ||
687 | |||
688 | glEnableVertexAttribArray(0); | ||
689 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | ||
690 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); | ||
691 | glDrawArrays(GL_TRIANGLES, 0, 6); | ||
692 | glDisableVertexAttribArray(0); | ||
693 | |||
625 | glfwSwapBuffers(window); | 694 | glfwSwapBuffers(window); |
626 | 695 | ||
627 | glDeleteBuffers(1, &g_norms); | 696 | glDeleteBuffers(1, &g_norms); |