From 540e0d6db808111ed1232e1c518273ab77a94340 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 16 Feb 2018 22:06:09 +0100 Subject: [PATCH] Implemente LookAt and using values from camera for near and far values for depth rendering --- src/SimpleMath/SimpleMathGL.h | 16 +++++++++++++++ src/modules/RenderModule.cc | 37 +++++++++++++++++++++++------------ src/modules/RenderModule.h | 18 ++++++++--------- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/SimpleMath/SimpleMathGL.h b/src/SimpleMath/SimpleMathGL.h index 5de0fde..6541852 100644 --- a/src/SimpleMath/SimpleMathGL.h +++ b/src/SimpleMath/SimpleMathGL.h @@ -101,6 +101,22 @@ inline Matrix44f Perspective(float fovy, float aspect, ); } +inline Matrix44f LookAt( + const Vector3f& eye, + const Vector3f& poi, + const Vector3f& up) { + Vector3f d = (poi - eye).normalized(); + Vector3f s = d.cross(up.normalized()); + Vector3f u = s.cross(d); + + return Matrix44f( + s[0], u[0], -d[0], 0.0f, + s[1], u[1], -d[1], 0.0f, + s[2], u[2], -d[2], 0.0f, + -eye[0], -eye[1], -eye[2], 1.0f + ); +} + /** Quaternion * * order: x,y,z,w diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 9ceb30c..33d670b 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -8,9 +8,9 @@ using namespace SimpleMath::GL; struct Renderer; static const GLfloat g_vertex_buffer_data[] = { - -0.9f, -0.9f, 1.0f, - 0.9f, -0.9f, -1.0f, - 0.0f, 0.9f, 0.0f + -0.7f, -0.9f, 0.0f, + 0.9f, -0.9f, 0.0f, + 0.0f, 0.9f, 1.0f }; static const GLfloat g_quad_vertex_buffer_data[] = { @@ -117,7 +117,12 @@ const struct module_api MODULE_API = { // // Camera // -void Camera::updateMatrices() { +void Camera::UpdateMatrices() { + mViewMatrix = LookAt(eye, poi, up); + + if (orthographic) { + mProjectionMatrix = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, near, far); + } } @@ -178,9 +183,20 @@ void Renderer::RenderGl() { if (width != mWidth || height != mHeight) Resize(width, height); - mCamera.mtxProj = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f); + mCamera.eye = Vector3f (0.0f, 0.0f, 1.0f); + mCamera.poi = Vector3f (0.0f, 0.0f, 0.0f); + mCamera.up = Vector3f (0.0f, 1.0f, 0.0f); + mCamera.near = 0.0f; + mCamera.far = 1.0f; + mCamera.orthographic = true; - Matrix44f model_view_projection = mCamera.mtxProj * mCamera.mtxView; + mCamera.UpdateMatrices(); + + Matrix44f model_matrix = TranslateMat44(0.0f, 0.0f, 0.0f); + Matrix44f model_view_projection = + model_matrix + * mCamera.mViewMatrix + * mCamera.mProjectionMatrix; // enable the render target glBindFramebuffer(GL_FRAMEBUFFER, mRenderTarget.mFrameBufferId); @@ -198,7 +214,6 @@ void Renderer::RenderGl() { glUseProgram(mDefaultProgram.mProgramId); glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); - glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); @@ -223,9 +238,7 @@ void Renderer::RenderGui() { 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; + Matrix44f model_view_projection = Matrix44f::Identity(); if (render_color) { // Render the full screen quad @@ -240,8 +253,8 @@ void Renderer::RenderGui() { 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, mCamera.near); + glUniform1f(muRenderQuadDepthFar, mCamera.far); } glEnableVertexAttribArray(0); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index aabe80b..4135b96 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -24,8 +24,8 @@ struct Camera { float width; float height; - Matrix44f mtxProj; - Matrix44f mtxView; + Matrix44f mProjectionMatrix; + Matrix44f mViewMatrix; Camera() : eye {5.f, 4.f, 5.f}, @@ -38,27 +38,27 @@ struct Camera { width (-1.f), height (-1.f), - mtxProj ( + mProjectionMatrix ( 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f), - mtxView ( + mViewMatrix ( 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f) {} - void updateMatrices(); + void UpdateMatrices(); }; struct Light { Vector3f pos; Vector3f dir; - float mtxView[16]; - float mtxProj[16]; + float mViewMatrix[16]; + float mProjectionMatrix[16]; float mtxLight[16]; float mtxShadow[16]; @@ -73,13 +73,13 @@ struct Light { Light() : pos (Vector3f(0.f, 10.f, 10.f)), dir (Vector3f(-1.f, -1.f, -1.f)), - mtxView { + mViewMatrix { 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f }, - mtxProj { + mProjectionMatrix { 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f,