From cb133248461025649692755392c0c5794e2fec9e Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 15 Mar 2018 11:01:55 +0100 Subject: [PATCH] Working on shadow maps --- 3rdparty/imgui_dock_lumix/imgui_dock.cpp | 8 +- data/shaders/fs_default.glsl | 47 +++++++-- data/shaders/fs_depthbuffer.glsl | 2 +- data/shaders/fs_shadowmap.frag | 23 +---- data/shaders/fs_simpletexture.glsl | 13 +-- data/shaders/vs_default.glsl | 16 +++- data/shaders/vs_passthrough.glsl | 12 +-- data/shaders/vs_shadowmap.vert | 15 +-- src/modules/RenderModule.cc | 115 +++++++++++++++-------- src/modules/RenderModule.h | 18 +--- src/modules/RenderUtils.cc | 25 ++--- src/modules/RenderUtils.h | 31 +++--- 12 files changed, 181 insertions(+), 144 deletions(-) diff --git a/3rdparty/imgui_dock_lumix/imgui_dock.cpp b/3rdparty/imgui_dock_lumix/imgui_dock.cpp index 68c6f26..c582647 100644 --- a/3rdparty/imgui_dock_lumix/imgui_dock.cpp +++ b/3rdparty/imgui_dock_lumix/imgui_dock.cpp @@ -1090,7 +1090,13 @@ struct DockContext } - Dock* getDockByIndex(int idx) { return idx < 0 ? nullptr : m_docks[(int)idx]; } + Dock* getDockByIndex(int idx) { + if (idx >= 0 && idx >= m_docks.size()) { + fprintf(stderr, "Error loading docks: trying to get dock with id %d but only have %d docks", idx, m_docks.size()); + IM_ASSERT(idx < m_docks.size()); + } + return idx < 0 ? nullptr : m_docks[(int)idx]; + } void load() diff --git a/data/shaders/fs_default.glsl b/data/shaders/fs_default.glsl index 43747df..6e2ba9e 100644 --- a/data/shaders/fs_default.glsl +++ b/data/shaders/fs_default.glsl @@ -3,23 +3,44 @@ uniform vec4 uColor; uniform vec3 uLightDirection; uniform vec3 uViewPosition; +uniform sampler2D uAlbedoTexture; +uniform sampler2D uShadowMap; -smooth in vec4 ioFragColor; -in vec3 ioNormal; in vec3 ioFragPosition; +in vec3 ioFragNormal; +in vec2 ioFragTexCoords; +smooth in vec4 ioFragColor; +in vec4 ioFragPosLightSpace; out vec4 outColor; +float ShadowCalculation(vec4 frag_pos_light_space) { + vec3 projected_coordinates = frag_pos_light_space.xyz / frag_pos_light_space.w; + projected_coordinates = projected_coordinates * 0.5 + 0.5; + + float closest_depth = texture(uShadowMap, projected_coordinates.xy).r; +// float closest_depth = texture(uAlbedoTexture, projected_coordinates.xy).r; + float current_depth = projected_coordinates.z; + +// return current_depth; + return closest_depth; +// float bias = 0.005; +// return current_depth - bias > closest_depth ? 1.0 : 0.0; +// return current_depth; +} + void main() { + vec4 albedo_color = texture(uAlbedoTexture, ioFragTexCoords) * ioFragColor * uColor; + // ambient lighting float ambient_strength = 0.1; - vec4 ambient = ambient_strength * ioFragColor; + vec4 ambient = ambient_strength * albedo_color; // diffuse lighting - vec3 normal_dir = normalize(ioNormal); + vec3 normal_dir = normalize(ioFragNormal); vec3 light_dir = normalize(uLightDirection); float diff = max(dot(normal_dir, light_dir), 0.0); - vec4 diffuse = diff * ioFragColor; + vec4 diffuse = diff * albedo_color; // specular lighting vec3 view_dir = normalize(uViewPosition - ioFragPosition); @@ -28,5 +49,19 @@ void main() { float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32); vec4 specular = spec * vec4(0.5); - outColor = ambient + diffuse + specular; + // shadow + float shadow = ShadowCalculation(ioFragPosLightSpace); + outColor = ambient + (1.0 - shadow) * (diffuse + specular); +// +// +// vec3 projected_coordinates = ioFragPosLightSpace.xyz / ioFragPosLightSpace.w; +// projected_coordinates = projected_coordinates * 0.5 + 0.5; +// float shadow_map_value = texture(uShadowMap, projected_coordinates.xy).r; +// outColor = shadow_map_value * vec4(1.0, 1.0, 1.0, 1.0); +// +// outColor = vec4(vec3(1.0f - shadow_map_value), 1.0); + +// outColor = (shadow) * vec4(1.0, 1.0, 1.0, 1.0); +// outColor = ioFragPosLightSpace / ioFragPosLightSpace.w; +// outColor = ambient + diffuse + specular; } diff --git a/data/shaders/fs_depthbuffer.glsl b/data/shaders/fs_depthbuffer.glsl index 3c9d0b7..2f67730 100644 --- a/data/shaders/fs_depthbuffer.glsl +++ b/data/shaders/fs_depthbuffer.glsl @@ -1,4 +1,4 @@ -#version 330 core +#version 150 core in vec2 ioUV; diff --git a/data/shaders/fs_shadowmap.frag b/data/shaders/fs_shadowmap.frag index 43747df..37fe227 100644 --- a/data/shaders/fs_shadowmap.frag +++ b/data/shaders/fs_shadowmap.frag @@ -1,32 +1,13 @@ #version 150 core -uniform vec4 uColor; -uniform vec3 uLightDirection; -uniform vec3 uViewPosition; smooth in vec4 ioFragColor; -in vec3 ioNormal; in vec3 ioFragPosition; out vec4 outColor; void main() { - // ambient lighting - float ambient_strength = 0.1; - vec4 ambient = ambient_strength * ioFragColor; + gl_FragDepth = gl_FragCoord.z; - // diffuse lighting - vec3 normal_dir = normalize(ioNormal); - vec3 light_dir = normalize(uLightDirection); - float diff = max(dot(normal_dir, light_dir), 0.0); - vec4 diffuse = diff * ioFragColor; - - // specular lighting - vec3 view_dir = normalize(uViewPosition - ioFragPosition); - vec3 halfway_dir = normalize(light_dir + view_dir); - - float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32); - vec4 specular = spec * vec4(0.5); - - outColor = ambient + diffuse + specular; + outColor = ioFragColor; } diff --git a/data/shaders/fs_simpletexture.glsl b/data/shaders/fs_simpletexture.glsl index 70b88e9..d6a2915 100644 --- a/data/shaders/fs_simpletexture.glsl +++ b/data/shaders/fs_simpletexture.glsl @@ -1,18 +1,11 @@ -#version 330 core +#version 150 core in vec2 ioUV; -out vec3 outColor; +out vec4 outColor; uniform sampler2D uTexture; -uniform float uTime; void main() { - outColor = texture(uTexture, - ioUV - + 0.01 * vec2( - sin(uTime + 1024.0 * ioUV.x), - cos(uTime + 768.0 * ioUV.y) - ) - ).xyz; + outColor = texture(uTexture, ioUV); } diff --git a/data/shaders/vs_default.glsl b/data/shaders/vs_default.glsl index 549947c..5bb1b01 100644 --- a/data/shaders/vs_default.glsl +++ b/data/shaders/vs_default.glsl @@ -9,17 +9,23 @@ in vec4 inColor; uniform mat4 uModelMatrix; uniform mat4 uViewMatrix; uniform mat4 uProjectionMatrix; +uniform mat4 uLightSpaceMatrix; uniform mat3 uNormalMatrix; uniform vec3 uLightDirection; uniform vec3 uViewPosition; -smooth out vec4 ioFragColor; -out vec3 ioNormal; out vec3 ioFragPosition; +out vec3 ioFragNormal; +out vec2 ioFragTexCoords; +smooth out vec4 ioFragColor; +out vec4 ioFragPosLightSpace; void main() { - gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord; - ioFragColor = inColor; - ioNormal = uNormalMatrix * inNormal; ioFragPosition = (uModelMatrix * inCoord).xyz; + ioFragNormal = uNormalMatrix * inNormal; + ioFragTexCoords = inUV; + ioFragColor = inColor; + ioFragPosLightSpace = uLightSpaceMatrix * vec4(ioFragPosition, 1.0); + + gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord; } diff --git a/data/shaders/vs_passthrough.glsl b/data/shaders/vs_passthrough.glsl index 286c571..4fc7165 100644 --- a/data/shaders/vs_passthrough.glsl +++ b/data/shaders/vs_passthrough.glsl @@ -1,13 +1,11 @@ -#version 330 core +#version 150 core -in vec3 inVertex; - -uniform mat4 uModelViewProj; -uniform float uTime; +in vec4 inCoord; +in vec2 inUV; out vec2 ioUV; void main() { - gl_Position = uModelViewProj * vec4(inVertex, 1); - ioUV = (inVertex.xy + vec2(1,1)) / 2.0; + ioUV = inUV; + gl_Position = inCoord; } diff --git a/data/shaders/vs_shadowmap.vert b/data/shaders/vs_shadowmap.vert index 549947c..ccc0f6d 100644 --- a/data/shaders/vs_shadowmap.vert +++ b/data/shaders/vs_shadowmap.vert @@ -1,25 +1,14 @@ #version 150 core -#extension GL_ARB_explicit_attrib_location : require in vec4 inCoord; -in vec3 inNormal; -in vec2 inUV; in vec4 inColor; uniform mat4 uModelMatrix; -uniform mat4 uViewMatrix; -uniform mat4 uProjectionMatrix; -uniform mat3 uNormalMatrix; -uniform vec3 uLightDirection; -uniform vec3 uViewPosition; +uniform mat4 uLightSpaceMatrix; smooth out vec4 ioFragColor; -out vec3 ioNormal; -out vec3 ioFragPosition; void main() { - gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord; + gl_Position = uLightSpaceMatrix * uModelMatrix * inCoord; ioFragColor = inColor; - ioNormal = uNormalMatrix * inNormal; - ioFragPosition = (uModelMatrix * inCoord).xyz; } diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 8c6c09a..ff0933f 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -53,8 +53,10 @@ static const GLfloat g_coordinate_system_vertex_buffer_data[] = { VertexArray gVertexArray; VertexArrayMesh gCoordinateFrameMesh; +VertexArrayMesh gXZPlaneGrid; VertexArrayMesh gXZPlaneMesh; VertexArrayMesh gUnitCubeMesh; +VertexArrayMesh gScreenQuad; // // Module @@ -141,6 +143,7 @@ static bool module_step(struct module_state *state, float dt) { assert (gWindow != nullptr); state->renderer->RenderGui(); state->renderer->RenderGl(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); return true; } @@ -175,13 +178,13 @@ void Light::Initialize() { void Light::UpdateMatrices() { mCamera.mIsOrthographic = true; - mCamera.mWidth = mShadowMapSize; - mCamera.mHeight = mShadowMapSize; + mCamera.mWidth = 20.0f; + mCamera.mHeight = 20.0f; mCamera.mNear = mNear; mCamera.mFar = mFar; - mCamera.mEye = mDirection * mBBoxSize * 0.5; - mCamera.mPoi = mCamera.mEye - mDirection; + mCamera.mEye = Vector3f (10.0f, 10.0f, 10.0f); + mCamera.mPoi = Vector3f (0.0f, 0.0f, 0.0f); mCamera.UpdateMatrices(); @@ -210,9 +213,9 @@ void Renderer::Initialize(int width, int height) { gCoordinateFrameMesh.SetData(vertex_data, 6); - // Plane + // Plane Grid const int plane_grid_size = 20; - gXZPlaneMesh.Initialize(gVertexArray, (plane_grid_size + 1) * 4); + gXZPlaneGrid.Initialize(gVertexArray, (plane_grid_size + 1) * 4); std::vector plane_data((plane_grid_size + 1) * 4); for (int i = 0, n = plane_grid_size + 1; i < n; ++i) { @@ -240,7 +243,22 @@ void Renderer::Initialize(int width, int height) { -1.0f * i, plane_grid_size * 0.5f, 255, 255, 255, 255); } - gXZPlaneMesh.SetData(plane_data.data(), plane_data.size()); + gXZPlaneGrid.SetData(plane_data.data(), plane_data.size()); + + // Plane Mesh + gXZPlaneMesh.Initialize(gVertexArray, 4); + VertexArray::VertexData plane_mesh_vertex_data[] = { + {-10.0f, 0.0f, -10.0f, 1.0f, 0.0f, 1.0f, 0.0f, -10.0f, -10.0f, 255, 255, 255, 255}, + {-10.0f, 0.0f, 10.0f, 1.0f, 0.0f, 1.0f, 0.0f, -10.0f, 10.0f, 255, 255, 255, 255}, + { 10.0f, 0.0f, 10.0f, 1.0f, 0.0f, 1.0f, 0.0f, 10.0f, 10.0f, 255, 255, 255, 255}, + { 10.0f, 0.0f, -10.0f, 1.0f, 0.0f, 1.0f, 0.0f, 10.0f, -10.0f, 255, 255, 255, 255} + }; + gXZPlaneMesh.SetData(plane_mesh_vertex_data, 4); + GLuint xz_plane_index_data[] = { + 0, 1, 2, + 2, 3, 0 + }; + gXZPlaneMesh.SetIndexData(xz_plane_index_data, 6); // Unit Cube gUnitCubeMesh.Initialize(gVertexArray, 4 * 6); @@ -292,6 +310,21 @@ void Renderer::Initialize(int width, int height) { }; gUnitCubeMesh.SetIndexData(unit_cube_index_data, 36); + // Screen Quad + gScreenQuad.Initialize(gVertexArray, 4); + VertexArray::VertexData screen_quad_data[] = { + {-1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 255, 255, 255, 255}, + { 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 255, 255, 255, 255}, + { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 255, 255, 255, 255}, + {-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 255, 255, 255, 255} + }; + gScreenQuad.SetData(screen_quad_data, 4); + GLuint screen_quad_indices[] = { + 0, 1, 2, + 2, 3, 0 + }; + gScreenQuad.SetIndexData(screen_quad_indices, 6); + // Simple Shader mSimpleProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); bool load_result = mSimpleProgram.Load(); @@ -308,19 +341,12 @@ void Renderer::Initialize(int width, int height) { load_result = mRenderQuadProgramColor.Load(); mRenderQuadProgramColor.RegisterFileModification(); assert(load_result); - muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj"); - muRenderQuadTexture = mRenderQuadProgramColor.GetUniformLocation("uTexture"); - muRenderQuadTime = mRenderQuadProgramColor.GetUniformLocation("uTime"); - // Program for depth texture rendering mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); load_result = mRenderQuadProgramDepth.Load(); mRenderQuadProgramDepth.RegisterFileModification(); assert(load_result); - muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj"); - muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear"); - muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar"); // Render Target gLog("Initializing main render target size: %d,%d", width, height); @@ -329,23 +355,15 @@ void Renderer::Initialize(int width, int height) { | RenderTarget::EnableDepthTexture | RenderTarget::EnableLinearizedDepthTexture); - // Render Target Quad - glGenVertexArrays(1, &mRenderQuadVertexArrayId); - glBindVertexArray(mRenderQuadVertexArrayId); - - glGenBuffers(1, &mRenderQuadVertexBufferId); - glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId); - glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); - - mRenderTarget.mQuadVertexArray = mRenderQuadVertexArrayId; - mRenderTarget.mQuadVertexBuffer = mRenderQuadVertexBufferId; + mRenderTarget.mVertexArray = &gVertexArray; + mRenderTarget.mQuadMesh = &gScreenQuad; mRenderTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth; mRenderTarget.mLinearizeDepthProgram.RegisterFileModification(); // Light mLight.Initialize(); - mLight.mShadowMapTarget.mQuadVertexArray = mRenderQuadVertexArrayId; - mLight.mShadowMapTarget.mQuadVertexBuffer = mRenderQuadVertexBufferId; + mLight.mShadowMapTarget.mVertexArray = &gVertexArray; + mLight.mShadowMapTarget.mQuadMesh = &gScreenQuad; mLight.mShadowMapTarget.mLinearizeDepthProgram = mRenderQuadProgramDepth; mLight.mShadowMapTarget.mLinearizeDepthProgram.RegisterFileModification(); } @@ -372,8 +390,10 @@ void Renderer::RenderGl() { glViewport(0, 0, mLight.mShadowMapSize, mLight.mShadowMapSize); mLight.UpdateMatrices(); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); glUseProgram(mLight.mShadowMapProgram.mProgramId); + if (mLight.mShadowMapProgram.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix) == -1) { + gLog ("Warning: Uniform %s not found!", "uLightSpaceMatrix"); + } RenderScene(mLight.mShadowMapProgram, mLight.mCamera); mLight.mShadowMapTarget.RenderToLinearizedDepth(mLight.mCamera.mNear, mLight.mCamera.mFar, mLight.mCamera.mIsOrthographic); glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -404,7 +424,7 @@ void Renderer::RenderGl() { // clear color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); glUseProgram(mSimpleProgram.mProgramId); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); @@ -412,7 +432,7 @@ void Renderer::RenderGl() { // Coordinate System: VertexArrayMesh model_view_projection = - TranslateMat44(0.0f, 0.0f, 0.0f) + TranslateMat44(0.0f, 0.002f, 0.0f) * mCamera.mViewMatrix * mCamera.mProjectionMatrix; mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); @@ -422,27 +442,28 @@ void Renderer::RenderGl() { // Plane model_view_projection = - TranslateMat44(0.0f, 0.0f, 0.0f) + TranslateMat44(0.0f, 0.001f, 0.0f) * mCamera.mViewMatrix * mCamera.mProjectionMatrix; mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); mSimpleProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); gVertexArray.Bind(); - gXZPlaneMesh.Draw(GL_LINES); + gXZPlaneGrid.Draw(GL_LINES); // Scene glUseProgram(mDefaultProgram.mProgramId); - glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture); glEnable(GL_DEPTH_TEST); + if (mDefaultProgram.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix) == -1) { + gLog ("Warning: Uniform %s not found!", "uLightSpaceMatrix"); + } + + RenderScene(mDefaultProgram, mCamera); if (mSettings->DrawDepth) { mRenderTarget.RenderToLinearizedDepth(mCamera.mNear, mCamera.mFar, mCamera.mIsOrthographic); } - - glDisableVertexAttribArray(0); - glBindFramebuffer(GL_FRAMEBUFFER, 0); } void Renderer::RenderScene(RenderProgram &program, const Camera& camera) { @@ -452,16 +473,34 @@ void Renderer::RenderScene(RenderProgram &program, const Camera& camera) { Matrix33f normal_matrix = model_matrix.block<3,3>(0,0).transpose(); normal_matrix = normal_matrix.inverse(); - program.SetMat44("uModelMatrix", model_matrix); + if (program.SetMat44("uModelMatrix", model_matrix) == -1) { + gLog ("Warning: could not find uModelMatrix"); + } program.SetMat44("uViewMatrix", camera.mViewMatrix); program.SetMat44("uProjectionMatrix", camera.mProjectionMatrix); program.SetMat33("uNormalMatrix", normal_matrix); - program.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); + program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f)); program.SetVec3("uLightDirection", mLight.mDirection); program.SetVec3("uViewPosition", camera.mEye); + + program.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture); + program.SetInt("uShadowMap", 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, mDefaultTexture.mTextureId); + program.SetInt("uAlbedoTexture", 1); + gVertexArray.Bind(); + program.SetMat44("uModelMatrix", TranslateMat44(3.0, 2.0, 0.0)); gUnitCubeMesh.Draw(GL_TRIANGLES); + + program.SetMat44("uModelMatrix", Matrix44f::Identity()); + program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f)); + gXZPlaneMesh.Draw(GL_TRIANGLES); } void Renderer::RenderGui() { @@ -494,7 +533,7 @@ void Renderer::RenderGui() { ImGui::SliderFloat3("Direction", mLight.mDirection.data(), -10.0f, 10.0f); ImVec2 content_avail = ImGui::GetContentRegionAvail(); - ImGui::SliderFloat("Light Near", &mLight.mNear, -10.0f, 10.0f); + ImGui::SliderFloat("Light Near", &mLight.mNear, -10.0f, 50.0f); ImGui::SliderFloat("Light Far", &mLight.mFar, -10.0f, 50.0f); ImGui::Checkbox("Draw Light Depth", &mSettings->DrawLightDepth); void* texture; diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index fb3969e..d7b71d2 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -65,27 +65,13 @@ struct Renderer { Texture mDefaultTexture; RenderProgram mSimpleProgram; - RenderProgram mDefaultProgram; - GLuint muDefaultModelViewProjection; - GLuint muDefaultColor; + RenderProgram mRenderQuadProgramColor; + RenderProgram mRenderQuadProgramDepth; RenderTarget mRenderTarget; GLTextureRef mRenderTextureRef = { (int)0xbadface }; - GLuint mRenderQuadVertexArrayId; - GLuint mRenderQuadVertexBufferId; - - RenderProgram mRenderQuadProgramColor; - GLuint muRenderQuadModelViewProj; - GLuint muRenderQuadTexture; - GLuint muRenderQuadTime; - - RenderProgram mRenderQuadProgramDepth; - GLuint muRenderQuadDepthModelViewProj; - GLuint muRenderQuadDepthNear; - GLuint muRenderQuadDepthFar; - Renderer() : mInitialized(false), mWidth (0), diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index be76051..08d9bf3 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -29,6 +29,9 @@ void Camera::UpdateMatrices() { float width = mWidth * 0.5f * (mFar - mNear * 0.5f) * 0.001f; float height = width * mHeight / mWidth; +// width = mWidth; +// height = mHeight; + mProjectionMatrix = Ortho(-width * 0.5f, width * 0.5f, -height * 0.5f, height * 0.5f, mNear, mFar); } else { mProjectionMatrix = Perspective(mFov, mWidth / mHeight, mNear, mFar); @@ -355,6 +358,8 @@ void RenderTarget::Resize(int width, int height) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 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_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTexture, 0); @@ -397,8 +402,8 @@ void RenderTarget::Resize(int width, int height) { void RenderTarget::RenderToLinearizedDepth(const float& near, const float& far, bool is_orthographic) { assert(mFlags & EnableLinearizedDepthTexture); assert(mLinearizedDepthTexture != -1); - assert(mQuadVertexArray != -1); - assert(mQuadVertexBuffer != -1); + assert(mVertexArray != nullptr); + assert(mQuadMesh != nullptr); glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferId); GLenum draw_attachment_1[] = { GL_COLOR_ATTACHMENT1 }; @@ -420,24 +425,14 @@ void RenderTarget::RenderToLinearizedDepth(const float& near, const float& far, mLinearizeDepthProgram.SetFloat("uIsOrthographic", is_orthographic ? 1.0f : 0.0f); mLinearizeDepthProgram.SetInt("uDepthTexture", 0); - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, mQuadVertexBuffer); - glVertexAttribPointer( - 0, // attribute 0 - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // offset - ); - - glDrawArrays(GL_TRIANGLES, 0, 6); // starting from vertex 0; 3 vertices total + mVertexArray->Bind(); + mQuadMesh->Draw(GL_TRIANGLES); if (mFlags & EnableColor) { GLenum draw_attachment_0[] = { GL_COLOR_ATTACHMENT1 }; glDrawBuffers(1, draw_attachment_0); + glEnable(GL_DEPTH_TEST); } - glBindFramebuffer(GL_FRAMEBUFFER, 0); } // diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 9a78c67..d3007e3 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -9,6 +9,10 @@ #include +// Forward declarations +struct VertexArray; +struct VertexArrayMesh; + struct Transform { Quaternion rotation = Quaternion (0.0f, 0.0f, 0.0f, 1.0f); Vector3f translation = Vector3f (0.0f, 0.0f, 0.0f); @@ -197,33 +201,40 @@ struct RenderProgram : AFileModificationListener { GLuint CompileFragmentShader(); GLuint LinkProgram(GLuint vertex_shader, GLuint fragment_shader); - void SetInt(const char* name, const GLint& val) { + GLint SetInt(const char* name, const GLint& val) { GLint location = glGetUniformLocation(mProgramId, name); glUniform1i(location, val); + return location; } - void SetFloat(const char* name, const float& val) { + GLint SetFloat(const char* name, const float& val) { GLint location = glGetUniformLocation(mProgramId, name); glUniform1f(location, val); + return location; } - void SetVec3(const char* name, const Vector3f& vec) { + GLint SetVec3(const char* name, const Vector3f& vec) { GLint location = glGetUniformLocation(mProgramId, name); glUniform3fv(location, 1, vec.data()); + return location; } - void SetVec4(const char* name, const Vector3f& vec, float w = 1.0f) { + GLint SetVec4(const char* name, const Vector3f& vec, float w = 1.0f) { GLint location = glGetUniformLocation(mProgramId, name); glUniform4f(location, vec[0], vec[1], vec[2], w); + return location; } - void SetVec4(const char* name, const Vector4f& vec) { + GLint SetVec4(const char* name, const Vector4f& vec) { GLint location = glGetUniformLocation(mProgramId, name); glUniform4fv(location, 1, vec.data()); + return location; } - void SetMat44 (const char* name, const Matrix44f& mat) { + GLint SetMat44 (const char* name, const Matrix44f& mat) { GLint location = glGetUniformLocation(mProgramId, name); glUniformMatrix4fv(location, 1, GL_FALSE, mat.data()); + return location; } - void SetMat33 (const char* name, const Matrix33f& mat) { + GLint SetMat33 (const char* name, const Matrix33f& mat) { GLint location = glGetUniformLocation(mProgramId, name); glUniformMatrix3fv(location, 1, GL_FALSE, mat.data()); + return location; } void RegisterFileModification(); @@ -252,8 +263,8 @@ struct RenderTarget { int mFlags = 0; RenderProgram mLinearizeDepthProgram; - GLuint mQuadVertexArray = -1; - GLuint mQuadVertexBuffer = -1; + VertexArray* mVertexArray = nullptr; + VertexArrayMesh* mQuadMesh = nullptr; RenderTarget() {}; ~RenderTarget(); @@ -276,8 +287,6 @@ struct Texture { bool Load(const char* path, int num_components = 3); }; -struct VertexArrayMesh; - /** * Multiple VertexArrayMeshes can be stored in a single VertexArray. * Storage order is: