diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-02-19 22:54:40 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-02-19 22:54:40 -0500 |
commit | e3e5a247c58c6e0b45d81ab61314bd8d1bd530ac (patch) | |
tree | 261c046c1dac2ee0473fae547b9a15b0ee46f0c4 | |
parent | cd48894563052baeddff64f6bbc13ccc7fa6e081 (diff) | |
download | therapy-e3e5a247c58c6e0b45d81ab61314bd8d1bd530ac.tar.gz therapy-e3e5a247c58c6e0b45d81ab61314bd8d1bd530ac.tar.bz2 therapy-e3e5a247c58c6e0b45d81ab61314bd8d1bd530ac.zip |
Added ability to resize window (full screen is really laggy though!)
-rw-r--r-- | shaders/bloom1.fragment | 3 | ||||
-rw-r--r-- | shaders/bloom2.fragment | 3 | ||||
-rw-r--r-- | shaders/final.fragment | 14 | ||||
-rw-r--r-- | src/renderer.cpp | 55 |
4 files changed, 59 insertions, 16 deletions
diff --git a/shaders/bloom1.fragment b/shaders/bloom1.fragment index 4940933..cfb2799 100644 --- a/shaders/bloom1.fragment +++ b/shaders/bloom1.fragment | |||
@@ -6,6 +6,7 @@ out vec3 color; | |||
6 | 6 | ||
7 | uniform sampler2D screenTex; | 7 | uniform sampler2D screenTex; |
8 | uniform float iGlobalTime; | 8 | uniform float iGlobalTime; |
9 | uniform vec2 resolution; | ||
9 | 10 | ||
10 | float nrand(vec2 n) | 11 | float nrand(vec2 n) |
11 | { | 12 | { |
@@ -45,7 +46,7 @@ void main() | |||
45 | vec2 ofs = fTaps_Poisson[i]; | 46 | vec2 ofs = fTaps_Poisson[i]; |
46 | ofs = vec2(dot(ofs, basis.xz), dot(ofs, basis.yw)); | 47 | ofs = vec2(dot(ofs, basis.xz), dot(ofs, basis.yw)); |
47 | 48 | ||
48 | vec2 texcoord = UV + max_siz * ofs / vec2(1024.0,768.0); | 49 | vec2 texcoord = UV + max_siz * ofs / resolution; |
49 | sum += texture(screenTex, texcoord, -10.0); | 50 | sum += texture(screenTex, texcoord, -10.0); |
50 | } | 51 | } |
51 | 52 | ||
diff --git a/shaders/bloom2.fragment b/shaders/bloom2.fragment index aa69f0f..1d9cac4 100644 --- a/shaders/bloom2.fragment +++ b/shaders/bloom2.fragment | |||
@@ -7,6 +7,7 @@ out vec3 color; | |||
7 | uniform sampler2D screenTex; | 7 | uniform sampler2D screenTex; |
8 | uniform sampler2D downsampledTex; | 8 | uniform sampler2D downsampledTex; |
9 | uniform float iGlobalTime; | 9 | uniform float iGlobalTime; |
10 | uniform vec2 resolution; | ||
10 | 11 | ||
11 | float nrand(vec2 n) | 12 | float nrand(vec2 n) |
12 | { | 13 | { |
@@ -46,7 +47,7 @@ void main() | |||
46 | vec2 ofs = fTaps_Poisson[i]; | 47 | vec2 ofs = fTaps_Poisson[i]; |
47 | ofs = vec2(dot(ofs, basis.xz), dot(ofs, basis.yw)); | 48 | ofs = vec2(dot(ofs, basis.xz), dot(ofs, basis.yw)); |
48 | 49 | ||
49 | vec2 texcoord = UV + max_siz * ofs / vec2(64.0,48.0); | 50 | vec2 texcoord = UV + max_siz * ofs / (resolution / vec2(16.0)); |
50 | sum += texture(downsampledTex, texcoord, -10.0); | 51 | sum += texture(downsampledTex, texcoord, -10.0); |
51 | } | 52 | } |
52 | 53 | ||
diff --git a/shaders/final.fragment b/shaders/final.fragment index 7eeb78e..4a51ad3 100644 --- a/shaders/final.fragment +++ b/shaders/final.fragment | |||
@@ -11,10 +11,6 @@ out vec3 color; | |||
11 | uniform sampler2D rendertex; | 11 | uniform sampler2D rendertex; |
12 | uniform sampler2D scanlinestex; | 12 | uniform sampler2D scanlinestex; |
13 | 13 | ||
14 | const vec2 UVScalar = vec2(1.0, 1.0); | ||
15 | const vec2 UVOffset = vec2(0.0, 0.0); | ||
16 | const vec2 CRTMask_Scale = vec2(85.0, 153.6)*4.0; | ||
17 | const vec2 CRTMask_Offset = vec2(0.0, 0.0); | ||
18 | const float Tuning_Overscan = 1.08; | 14 | const float Tuning_Overscan = 1.08; |
19 | const float Tuning_Dimming = 0.0; | 15 | const float Tuning_Dimming = 0.0; |
20 | const float Tuning_Satur = 1.0; | 16 | const float Tuning_Satur = 1.0; |
@@ -27,18 +23,18 @@ const float Tuning_Spec_Brightness = 0.35; | |||
27 | const float Tuning_Spec_Power = 50.0; | 23 | const float Tuning_Spec_Power = 50.0; |
28 | const float Tuning_Fres_Brightness = 1.0; | 24 | const float Tuning_Fres_Brightness = 1.0; |
29 | 25 | ||
26 | uniform vec2 resolution; | ||
27 | |||
30 | vec4 sampleCRT(vec2 uv) | 28 | vec4 sampleCRT(vec2 uv) |
31 | { | 29 | { |
32 | vec2 ScaledUV = uv; | 30 | vec2 CRTMask_Scale = resolution / vec2(6.0, 5.0); |
33 | ScaledUV *= UVScalar; | ||
34 | ScaledUV += UVOffset; | ||
35 | 31 | ||
36 | vec2 scanuv = ScaledUV * CRTMask_Scale; | 32 | vec2 scanuv = uv * CRTMask_Scale; |
37 | vec3 scantex = texture(scanlinestex, scanuv).rgb; | 33 | vec3 scantex = texture(scanlinestex, scanuv).rgb; |
38 | scantex += Tuning_Scanline_Brightness; | 34 | scantex += Tuning_Scanline_Brightness; |
39 | scantex = mix(vec3(1,1,1), scantex, Tuning_Scanline_Opacity); | 35 | scantex = mix(vec3(1,1,1), scantex, Tuning_Scanline_Opacity); |
40 | 36 | ||
41 | vec2 overscanuv = (ScaledUV * Tuning_Overscan) - ((Tuning_Overscan - 1.0f) * 0.5f); | 37 | vec2 overscanuv = (uv * Tuning_Overscan) - ((Tuning_Overscan - 1.0f) * 0.5f); |
42 | overscanuv = overscanuv - vec2(0.5, 0.5); | 38 | overscanuv = overscanuv - vec2(0.5, 0.5); |
43 | float rsq = (overscanuv.x*overscanuv.x) + (overscanuv.y*overscanuv.y); | 39 | float rsq = (overscanuv.x*overscanuv.x) + (overscanuv.y*overscanuv.y); |
44 | overscanuv = overscanuv + (overscanuv * (Tuning_Barrel * rsq)) + vec2(0.5,0.5); | 40 | overscanuv = overscanuv + (overscanuv * (Tuning_Barrel * rsq)) + vec2(0.5,0.5); |
diff --git a/src/renderer.cpp b/src/renderer.cpp index 00ae7ef..b0421e3 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -12,6 +12,8 @@ static GLFWwindow* window; | |||
12 | 12 | ||
13 | static GLuint FramebufferName; // The framebuffer | 13 | static GLuint FramebufferName; // The framebuffer |
14 | static GLuint depthrenderbuffer; | 14 | static GLuint depthrenderbuffer; |
15 | static int buffer_width = 1024; | ||
16 | static int buffer_height = 768; | ||
15 | 17 | ||
16 | static GLuint ntscShader; // The NTSC shader | 18 | static GLuint ntscShader; // The NTSC shader |
17 | static GLuint finalShader; // The passthrough shader | 19 | static GLuint finalShader; // The passthrough shader |
@@ -249,6 +251,44 @@ void loadMesh(const char* filename, std::vector<glm::vec3>& out_vertices, std::v | |||
249 | } | 251 | } |
250 | } | 252 | } |
251 | 253 | ||
254 | void setFramebufferSize(GLFWwindow* w, int width, int height) | ||
255 | { | ||
256 | buffer_width = width; | ||
257 | buffer_height = height; | ||
258 | |||
259 | glDeleteFramebuffers(1, &FramebufferName); | ||
260 | glDeleteRenderbuffers(1, &depthrenderbuffer); | ||
261 | |||
262 | glGenFramebuffers(1, &FramebufferName); | ||
263 | glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); | ||
264 | GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; | ||
265 | glDrawBuffers(1, DrawBuffers); | ||
266 | |||
267 | glGenRenderbuffers(1, &depthrenderbuffer); | ||
268 | glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer); | ||
269 | glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); | ||
270 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer); | ||
271 | |||
272 | glDeleteTextures(1, &preBloomTex); | ||
273 | glDeleteTextures(1, &bloomPassTex); | ||
274 | |||
275 | glGenTextures(1, &preBloomTex); | ||
276 | glBindTexture(GL_TEXTURE_2D, preBloomTex); | ||
277 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); | ||
278 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
279 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
280 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
281 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
282 | |||
283 | glGenTextures(1, &bloomPassTex); | ||
284 | glBindTexture(GL_TEXTURE_2D, bloomPassTex); | ||
285 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width/16, height/16, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); | ||
286 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
287 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
288 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
289 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
290 | } | ||
291 | |||
252 | GLFWwindow* initRenderer() | 292 | GLFWwindow* initRenderer() |
253 | { | 293 | { |
254 | if (rendererInitialized) | 294 | if (rendererInitialized) |
@@ -287,6 +327,8 @@ GLFWwindow* initRenderer() | |||
287 | exit(-1); | 327 | exit(-1); |
288 | } | 328 | } |
289 | 329 | ||
330 | glfwSetFramebufferSizeCallback(window, &setFramebufferSize); | ||
331 | |||
290 | // Set up vertex array object | 332 | // Set up vertex array object |
291 | glGenVertexArrays(1, &VertexArrayID); | 333 | glGenVertexArrays(1, &VertexArrayID); |
292 | glBindVertexArray(VertexArrayID); | 334 | glBindVertexArray(VertexArrayID); |
@@ -656,7 +698,7 @@ void renderWithoutEffects(Texture* tex) | |||
656 | } | 698 | } |
657 | 699 | ||
658 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 700 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
659 | glViewport(0, 0, 1024, 768); | 701 | glViewport(0, 0, buffer_width, buffer_height); |
660 | glClear(GL_COLOR_BUFFER_BIT); | 702 | glClear(GL_COLOR_BUFFER_BIT); |
661 | glUseProgram(blitShader); | 703 | glUseProgram(blitShader); |
662 | 704 | ||
@@ -755,7 +797,7 @@ void renderScreen(Texture* tex) | |||
755 | // We're going to render the screen now | 797 | // We're going to render the screen now |
756 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, preBloomTex, 0); | 798 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, preBloomTex, 0); |
757 | //glBindFramebuffer(GL_FRAMEBUFFER, 0); | 799 | //glBindFramebuffer(GL_FRAMEBUFFER, 0); |
758 | glViewport(0,0,1024,768); | 800 | glViewport(0,0,buffer_width,buffer_height); |
759 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 801 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
760 | glUseProgram(finalShader); | 802 | glUseProgram(finalShader); |
761 | 803 | ||
@@ -777,7 +819,7 @@ void renderScreen(Texture* tex) | |||
777 | glUniform1i(glGetUniformLocation(finalShader, "scanlinestex"), 1); | 819 | glUniform1i(glGetUniformLocation(finalShader, "scanlinestex"), 1); |
778 | 820 | ||
779 | // Initialize the MVP matrices | 821 | // Initialize the MVP matrices |
780 | mat4 p_matrix = perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); | 822 | mat4 p_matrix = perspective(45.0f, (float) buffer_width / (float) buffer_height, 0.1f, 100.0f); |
781 | mat4 v_matrix = lookAt(vec3(2,0,0), vec3(0,0,0), vec3(0,1,0)); | 823 | mat4 v_matrix = lookAt(vec3(2,0,0), vec3(0,0,0), vec3(0,1,0)); |
782 | mat4 m_matrix = mat4(1.0); | 824 | mat4 m_matrix = mat4(1.0); |
783 | mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; | 825 | mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; |
@@ -785,6 +827,7 @@ void renderScreen(Texture* tex) | |||
785 | 827 | ||
786 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); | 828 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); |
787 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); | 829 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); |
830 | glUniform2f(glGetUniformLocation(finalShader, "resolution"), buffer_width, buffer_height); | ||
788 | 831 | ||
789 | glEnableVertexAttribArray(0); | 832 | glEnableVertexAttribArray(0); |
790 | glBindBuffer(GL_ARRAY_BUFFER, mesh_vertexbuffer); | 833 | glBindBuffer(GL_ARRAY_BUFFER, mesh_vertexbuffer); |
@@ -805,7 +848,7 @@ void renderScreen(Texture* tex) | |||
805 | 848 | ||
806 | // Do the first pass of bloom (downsampling and tapping) | 849 | // Do the first pass of bloom (downsampling and tapping) |
807 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, bloomPassTex, 0); | 850 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, bloomPassTex, 0); |
808 | glViewport(0, 0, 64, 48); | 851 | glViewport(0, 0, buffer_width/16, buffer_height/16); |
809 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 852 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
810 | glUseProgram(bloom1Shader); | 853 | glUseProgram(bloom1Shader); |
811 | 854 | ||
@@ -814,6 +857,7 @@ void renderScreen(Texture* tex) | |||
814 | glUniform1i(glGetUniformLocation(bloom1Shader, "screenTex"), 0); | 857 | glUniform1i(glGetUniformLocation(bloom1Shader, "screenTex"), 0); |
815 | 858 | ||
816 | glUniform1f(glGetUniformLocation(bloom1Shader, "iGlobalTime"), glfwGetTime()); | 859 | glUniform1f(glGetUniformLocation(bloom1Shader, "iGlobalTime"), glfwGetTime()); |
860 | glUniform2f(glGetUniformLocation(bloom1Shader, "resolution"), buffer_width, buffer_height); | ||
817 | 861 | ||
818 | glEnableVertexAttribArray(0); | 862 | glEnableVertexAttribArray(0); |
819 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | 863 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); |
@@ -823,7 +867,7 @@ void renderScreen(Texture* tex) | |||
823 | 867 | ||
824 | // Do the second pass of bloom and render to screen | 868 | // Do the second pass of bloom and render to screen |
825 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 869 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
826 | glViewport(0, 0, 1024, 768); | 870 | glViewport(0, 0, buffer_width, buffer_height); |
827 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 871 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
828 | glUseProgram(bloom2Shader); | 872 | glUseProgram(bloom2Shader); |
829 | 873 | ||
@@ -836,6 +880,7 @@ void renderScreen(Texture* tex) | |||
836 | glUniform1i(glGetUniformLocation(bloom2Shader, "downsampledTex"), 1); | 880 | glUniform1i(glGetUniformLocation(bloom2Shader, "downsampledTex"), 1); |
837 | 881 | ||
838 | glUniform1f(glGetUniformLocation(bloom2Shader, "iGlobalTime"), glfwGetTime()); | 882 | glUniform1f(glGetUniformLocation(bloom2Shader, "iGlobalTime"), glfwGetTime()); |
883 | glUniform2f(glGetUniformLocation(bloom2Shader, "resolution"), buffer_width, buffer_height); | ||
839 | 884 | ||
840 | glEnableVertexAttribArray(0); | 885 | glEnableVertexAttribArray(0); |
841 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | 886 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); |