From 38d09ce35c924fce5e59b112ca8d671b7bf8fad8 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Sun, 29 Oct 2017 23:41:59 +0000 Subject: [PATCH] Adds a perspective matrix, refracts a matrix projection setup to prepare for an isometric view. Reviewed By: wraitii Differential Revision: https://code.wildfiregames.com/D971 This was SVN commit r20377. --- source/graphics/Camera.cpp | 24 ++++++++---------------- source/maths/Matrix3D.cpp | 33 ++++++++++++++++++++++++--------- source/maths/Matrix3D.h | 6 ++++-- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/source/graphics/Camera.cpp b/source/graphics/Camera.cpp index 5cf69d154d..ed24da15f5 100644 --- a/source/graphics/Camera.cpp +++ b/source/graphics/Camera.cpp @@ -54,27 +54,19 @@ void CCamera::SetProjection(float nearp, float farp, float fov) m_FarPlane = farp; m_FOV = fov; - float aspect = (float)m_ViewPort.m_Width/(float)m_ViewPort.m_Height; - float f = 1.0f/tanf(m_FOV/2); - - m_ProjMat.SetZero (); - m_ProjMat._11 = f/aspect; - m_ProjMat._22 = f; - m_ProjMat._33 = -(m_FarPlane+m_NearPlane)/(m_NearPlane-m_FarPlane); - m_ProjMat._34 = 2*m_FarPlane*m_NearPlane/(m_NearPlane-m_FarPlane); - m_ProjMat._43 = 1.0f; + const float aspect = static_cast(m_ViewPort.m_Width) / static_cast(m_ViewPort.m_Height); + m_ProjMat.SetPerspective(m_FOV, aspect, m_NearPlane, m_FarPlane); } void CCamera::SetProjectionTile(int tiles, int tile_x, int tile_y) { + const float aspect = static_cast(m_ViewPort.m_Width) / static_cast(m_ViewPort.m_Height); + const float f = 1.f / tanf(m_FOV / 2.f); - float aspect = (float)m_ViewPort.m_Width/(float)m_ViewPort.m_Height; - float f = 1.0f/tanf(m_FOV/2); - - m_ProjMat._11 = tiles*f/aspect; - m_ProjMat._22 = tiles*f; - m_ProjMat._13 = -(1-tiles + 2*tile_x); - m_ProjMat._23 = -(1-tiles + 2*tile_y); + m_ProjMat._11 = tiles * f / aspect; + m_ProjMat._22 = tiles * f; + m_ProjMat._13 = -(1 - tiles + 2 * tile_x); + m_ProjMat._23 = -(1 - tiles + 2 * tile_y); } //Updates the frustum planes. Should be called diff --git a/source/maths/Matrix3D.cpp b/source/maths/Matrix3D.cpp index ef12f6f425..589da4dab8 100644 --- a/source/maths/Matrix3D.cpp +++ b/source/maths/Matrix3D.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -36,7 +36,7 @@ void CMatrix3D::SetIdentity () } //Sets the zero matrix -void CMatrix3D::SetZero () +void CMatrix3D::SetZero() { _11=0.0f; _12=0.0f; _13=0.0f; _14=0.0f; _21=0.0f; _22=0.0f; _23=0.0f; _24=0.0f; @@ -44,15 +44,30 @@ void CMatrix3D::SetZero () _41=0.0f; _42=0.0f; _43=0.0f; _44=0.0f; } -void CMatrix3D::SetOrtho (float l, float r, float b, float t, float n, float f) +void CMatrix3D::SetOrtho(float left, float right, float bottom, float top, float near, float far) { // Based on OpenGL spec - *this = CMatrix3D( - 2/(r-l), 0, 0, -(r+l)/(r-l), - 0, 2/(t-b), 0, -(t+b)/(t-b), - 0, 0, -2/(f-n), -(f+n)/(f-n), - 0, 0, 0, 1 - ); + SetZero(); + _11 = 2 / (right - left); + _22 = 2 / (top - bottom); + _33 = -2 / (far - near); + _44 = 1; + + _14 = -(right + left) / (right - left); + _24 = -(top + bottom) / (top - bottom); + _34 = -(far + near) / (far - near); +} + +void CMatrix3D::SetPerspective(float fov, float aspect, float near, float far) +{ + const float f = 1.f / tanf(fov / 2.f); + + SetZero(); + _11 = f / aspect; + _22 = f; + _33 = -(far + near) / (near - far); + _34 = 2 * far * near / (near - far); + _43 = 1; } //The following clear the matrix and set the diff --git a/source/maths/Matrix3D.h b/source/maths/Matrix3D.h index 77cb290f9a..d86ad096fa 100644 --- a/source/maths/Matrix3D.h +++ b/source/maths/Matrix3D.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -182,7 +182,9 @@ public: // set this matrix to the zero matrix void SetZero(); // set this matrix to the orthogonal projection matrix (as with glOrtho) - void SetOrtho(float l, float r, float b, float t, float n, float f); + void SetOrtho(float left, float right, float bottom, float top, float near, float far); + // set this matrix to the perspective projection matrix + void SetPerspective(float fov, float aspect, float near, float far); // concatenate arbitrary matrix onto this matrix void Concatenate(const CMatrix3D& m)