Implemente LookAt and using values from camera for near and far values for depth rendering

simple_math_single_header
Martin Felis 2018-02-16 22:06:09 +01:00
parent 7bf244be37
commit 540e0d6db8
3 changed files with 50 additions and 21 deletions

View File

@ -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

View File

@ -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);

View File

@ -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,