summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/renderer.cpp55
1 files changed, 50 insertions, 5 deletions
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
13static GLuint FramebufferName; // The framebuffer 13static GLuint FramebufferName; // The framebuffer
14static GLuint depthrenderbuffer; 14static GLuint depthrenderbuffer;
15static int buffer_width = 1024;
16static int buffer_height = 768;
15 17
16static GLuint ntscShader; // The NTSC shader 18static GLuint ntscShader; // The NTSC shader
17static GLuint finalShader; // The passthrough shader 19static 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
254void 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
252GLFWwindow* initRenderer() 292GLFWwindow* 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);