From 48c8cde66b8293c2e2af492493ac3eb9c7519d40 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 16 Feb 2018 11:35:09 +0100 Subject: [PATCH] Added projection matrices code, tweaked stuff --- data/shaders/fs_depthbuffer.glsl | 37 ++++++++++++++++++++++-------- data/shaders/fs_simple.glsl | 4 ++-- data/shaders/fs_simpletexture.glsl | 14 +++++------ data/shaders/vs_passthrough.glsl | 9 ++++---- src/SimpleMath/SimpleMathGL.h | 26 +++++++++++++++++++++ src/main.cc | 4 ---- src/modules/RenderModule.cc | 36 +++++++++++++++++++++-------- src/modules/RenderModule.h | 4 +++- src/modules/RenderUtils.cc | 14 +++++------ 9 files changed, 104 insertions(+), 44 deletions(-) diff --git a/data/shaders/fs_depthbuffer.glsl b/data/shaders/fs_depthbuffer.glsl index 01e2638..1228392 100644 --- a/data/shaders/fs_depthbuffer.glsl +++ b/data/shaders/fs_depthbuffer.glsl @@ -1,17 +1,36 @@ #version 330 core -in vec2 uv; +in vec2 ioUV; -out vec3 color; +out vec3 outColor; -uniform sampler2D rendered_texture; -uniform float near; -uniform float far; +uniform sampler2D uTexture; +uniform float uNear; +uniform float uFar; void main() { - float z = texture(rendered_texture, uv).r; - float c = (2.0 * near) / (far + near - z * (far - near)); - c = 2.0 * near * far / (far + near - z * (far - near)); + float z = texture(uTexture, ioUV).r; + float c = (2.0 * uNear) / (uFar + uNear - z * (uFar - uNear)); +// c = 2.0 * uNear * uFar / (uFar + uNear - z * (uFar - uNear)); +// c = (uNear + (z - uNear) / (uFar - uNear); - color = vec3(z); + c = (z - uNear) / (uFar - uNear); + + outColor = vec3(c); + + if (abs(c + 1) < 0.2) + outColor = vec3(1, 0, 0); +// if (abs(c - 0.1) < 0.1) +// outColor = vec3(0, 0, 1); +// if (abs(c - 0.8) < 0.1) +// outColor = vec3(1, 0, 0); + + +// n ---- z ------- f +// 0 1 +// +// (n + (z - n)) (f - n) +// +// (f - n) +// outColor = vec3(z); } diff --git a/data/shaders/fs_simple.glsl b/data/shaders/fs_simple.glsl index 0cb324f..b28256a 100644 --- a/data/shaders/fs_simple.glsl +++ b/data/shaders/fs_simple.glsl @@ -1,7 +1,7 @@ #version 330 core -layout(location = 0) out vec3 color; +layout(location = 0) out vec3 outColor; void main() { - color = vec3(1, 0, 0); + outColor = vec3(1, 0, 0); } diff --git a/data/shaders/fs_simpletexture.glsl b/data/shaders/fs_simpletexture.glsl index 83c035b..0d9fce5 100644 --- a/data/shaders/fs_simpletexture.glsl +++ b/data/shaders/fs_simpletexture.glsl @@ -1,17 +1,17 @@ #version 330 core -in vec2 uv; +in vec2 ioUV; -out vec3 color; +out vec3 outColor; -uniform sampler2D rendered_texture; -uniform float time; +uniform sampler2D uTexture; +uniform float uTime; void main() { - color = texture(rendered_texture, uv + outColor = texture(uTexture, ioUV + 0.00 * vec2( - sin(time + 1024.0 * uv.x), - cos(time + 768.0 * uv.y) + sin(uTime + 1024.0 * ioUV.x), + cos(uTime + 768.0 * ioUV.y) ) ).xyz; } diff --git a/data/shaders/vs_passthrough.glsl b/data/shaders/vs_passthrough.glsl index 29cf1a5..fa8b585 100644 --- a/data/shaders/vs_passthrough.glsl +++ b/data/shaders/vs_passthrough.glsl @@ -1,10 +1,11 @@ #version 330 core -in vec3 vertex_position_modelspace; +in vec3 inVertex; +uniform mat4 uModelViewProj; -out vec2 uv; +out vec2 ioUV; void main() { - gl_Position = vec4(vertex_position_modelspace, 1); - uv = (vertex_position_modelspace.xy + vec2(1,1)) / 2.0; + gl_Position = uModelViewProj * vec4(inVertex, 1); + ioUV = (inVertex.xy + vec2(1,1)) / 2.0; } diff --git a/src/SimpleMath/SimpleMathGL.h b/src/SimpleMath/SimpleMathGL.h index d4c9bea..5de0fde 100644 --- a/src/SimpleMath/SimpleMathGL.h +++ b/src/SimpleMath/SimpleMathGL.h @@ -75,6 +75,32 @@ inline Matrix44f ScaleMat44 (float x, float y, float z) { ); } +inline Matrix44f Ortho(float left, float right, + float bottom, float top, + float near, float far) { + float tx = -(right + left) / (right - left); + float ty = -(top + bottom) / (top - bottom); + float tz = -(far + near) / (far - near); + return Matrix44f( + 2.0f / (right - left), 0.0f, 0.0f, 0.0f, + 0, 2.0f / (top - bottom), 0.0f, 0.0f, + 0.0f, 0.0f, -2.0f / (far - near), 0.0f, + tx, ty, tz, 1.0f + ); +} + +inline Matrix44f Perspective(float fovy, float aspect, + float near, float far) { + float x = fovy / 2.0f; + float f = cos(x) / sin(x); + return Matrix44f( + f / aspect, 0.0f, 0.0f, 0.0f, + 0.0f, f, 0.0f, 0.0f, + 0.0f, 0.0f, (far + near) / (near - far), -1.0f, + 0.0f, 0.0f, (2.0f * far * near) / (near - far), 0.0f + ); +} + /** Quaternion * * order: x,y,z,w diff --git a/src/main.cc b/src/main.cc index d599f69..0439d87 100644 --- a/src/main.cc +++ b/src/main.cc @@ -230,10 +230,6 @@ int main(void) usleep(16000); - if (frame_counter > 1) { - gLog ("End of frame %d", frame_counter); - } - glfwSwapBuffers(gWindow); } diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 79a9a38..a234e93 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -3,12 +3,14 @@ #include "RenderModule.h" #include +using namespace SimpleMath::GL; + struct Renderer; static const GLfloat g_vertex_buffer_data[] = { - -1.0f, -1.0f, 0.0f, - 1.0f, -1.0f, -1.2f, - 0.0f, 1.0f, 0.0f + -0.9f, -0.9f, 1.0f, + 0.9f, -0.9f, -1.0f, + 0.0f, 0.9f, 0.0f }; static const GLfloat g_quad_vertex_buffer_data[] = { @@ -149,15 +151,23 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl"); load_result = mRenderQuadProgramColor.Load(); assert(load_result); - muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "rendered_texture"); - muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "time"); + muRenderQuadModelViewProj = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uModelViewProj"); + gLog("muRenderQuadModelViewProj %d", muRenderQuadModelViewProj); + muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTexture"); + gLog("muRenderQuadTexture %d", muRenderQuadTexture ); + muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTime"); + gLog("muRenderQuadTime %d", muRenderQuadTime); // Program for depth texture rendering mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); load_result = mRenderQuadProgramDepth.Load(); assert(load_result); - muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "near"); - muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "far"); + muRenderQuadDepthModelViewProj = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uModelViewProj"); + gLog("muModelViewProj %d", muRenderQuadDepthModelViewProj); + muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uNear"); + gLog("muRenderQuadDepthNear %d", muRenderQuadDepthNear); + muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uFar"); + gLog("muRenderQuadDepthFar %d", muRenderQuadDepthFar); } void Renderer::Shutdown() { @@ -208,20 +218,26 @@ void Renderer::RenderGui() { glActiveTexture(GL_TEXTURE0); bool render_color = false; - + + mCamera.mtxProj = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f); + + Matrix44f model_view_projection = mCamera.mtxProj * mCamera.mtxView; + if (render_color) { // Render the full screen quad glUseProgram(mRenderQuadProgramColor.mProgramId); glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture); + glUniformMatrix4fv(muRenderQuadModelViewProj, 1, GL_FALSE, model_view_projection.data()); glUniform1i(muRenderQuadTexture, 0); glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f)); } else { // render depth texture glUseProgram(mRenderQuadProgramDepth.mProgramId); glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture); + glUniformMatrix4fv(muRenderQuadModelViewProj, 1, GL_FALSE, model_view_projection.data()); glUniform1i(muRenderQuadTexture, 0); - glUniform1f(muRenderQuadDepthNear, 1.0); - glUniform1f(muRenderQuadDepthFar, -1.0); + glUniform1f(muRenderQuadDepthNear, -1.0); + glUniform1f(muRenderQuadDepthFar, 1.0); } glEnableVertexAttribArray(0); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index ac29df1..3e34fd5 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -111,7 +111,7 @@ struct Renderer { uint32_t mWidth = 1; uint32_t mHeight = 1; - std::vector cameras; + Camera mCamera; Mesh mMesh; RenderProgram mProgram; RenderTarget mRenderTarget; @@ -120,10 +120,12 @@ struct Renderer { GLuint mRenderQuadVertexBufferId; RenderProgram mRenderQuadProgramColor; + GLuint muRenderQuadModelViewProj; GLuint muRenderQuadTexture; GLuint muRenderQuadTime; RenderProgram mRenderQuadProgramDepth; + GLuint muRenderQuadDepthModelViewProj; GLuint muRenderQuadDepthNear; GLuint muRenderQuadDepthFar; diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 94a3d97..86fe2e8 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -33,7 +33,7 @@ bool RenderProgram::Load() { VertexShaderCode = sstr.str(); VertexShaderStream.close(); }else{ - gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", mVertexShaderFilename.c_str()); + gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !", mVertexShaderFilename.c_str()); getchar(); return false; } @@ -53,7 +53,7 @@ bool RenderProgram::Load() { // Compile Vertex Shader - gLog("Compiling shader : %s\n", mVertexShaderFilename.c_str()); + gLog("Compiling shader : %s", mVertexShaderFilename.c_str()); char const * VertexSourcePointer = VertexShaderCode.c_str(); glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); glCompileShader(VertexShaderID); @@ -64,13 +64,13 @@ bool RenderProgram::Load() { if ( InfoLogLength > 0 ){ std::vector VertexShaderErrorMessage(InfoLogLength+1); glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); - gLog("%s\n", &VertexShaderErrorMessage[0]); + gLog("%s", &VertexShaderErrorMessage[0]); } // Compile Fragment Shader - gLog("Compiling shader : %s\n", mFragmentShaderFilename.c_str()); + gLog("Compiling shader : %s", mFragmentShaderFilename.c_str()); char const * FragmentSourcePointer = FragmentShaderCode.c_str(); glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); glCompileShader(FragmentShaderID); @@ -81,13 +81,13 @@ bool RenderProgram::Load() { if ( InfoLogLength > 0 ){ std::vector FragmentShaderErrorMessage(InfoLogLength+1); glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); - gLog("%s\n", &FragmentShaderErrorMessage[0]); + gLog("%s", &FragmentShaderErrorMessage[0]); } // Link the program - gLog("Linking program\n"); + gLog("Linking program"); GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); @@ -99,7 +99,7 @@ bool RenderProgram::Load() { if ( InfoLogLength > 0 ){ std::vector ProgramErrorMessage(InfoLogLength+1); glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); - gLog("%s\n", &ProgramErrorMessage[0]); + gLog("%s", &ProgramErrorMessage[0]); }