Procházet zdrojové kódy

Added math methods for creating a projection matrix

BearishSun před 9 roky
rodič
revize
b3fe1f4a0d

+ 6 - 0
Source/BansheeUtility/Include/BsMatrix4.h

@@ -453,6 +453,12 @@ namespace bs
 		/** Creates a 4x4 transformation matrix that performs rotation. */
 		/** Creates a 4x4 transformation matrix that performs rotation. */
 		static Matrix4 rotation(const Quaternion& rotation);
 		static Matrix4 rotation(const Quaternion& rotation);
 
 
+		/** Creates a 4x4 perspective projection matrix. */
+		static Matrix4 projectionPerspective(const Degree& horzFOV, float aspect, float near, float far);
+
+		/** Creates a view matrix and applies optional reflection. */
+		static Matrix4 view(const Vector3& position, const Quaternion& orientation, const Matrix4* reflectMatrix = nullptr);
+
         /**
         /**
          * Creates a matrix from translation, rotation and scale. 
          * Creates a matrix from translation, rotation and scale. 
          * 			
          * 			

+ 56 - 0
Source/BansheeUtility/Source/BsMatrix4.cpp

@@ -336,6 +336,62 @@ namespace bs
 		return Matrix4(mat);
 		return Matrix4(mat);
 	}
 	}
 
 
+	Matrix4 Matrix4::projectionPerspective(const Degree& horzFOV, float aspect, float near, float far)
+    {
+	    // Note: Duplicate code in Camera, bring it all here eventually
+		static const float INFINITE_FAR_PLANE_ADJUST = 0.00001f;
+
+		Radian thetaX(horzFOV * 0.5f);
+		float tanThetaX = Math::tan(thetaX);
+		float tanThetaY = tanThetaX / aspect;
+
+		float half_w = tanThetaX * near;
+		float half_h = tanThetaY * near;
+
+		float left = -half_w;
+		float right = half_w;
+		float bottom = -half_h;
+		float top = half_h;
+
+		float inv_w = 1 / (right - left);
+		float inv_h = 1 / (top - bottom);
+		float inv_d = 1 / (far - near);
+
+		float A = 2 * near * inv_w;
+		float B = 2 * near * inv_h;
+		float C = (right + left) * inv_w;
+		float D = (top + bottom) * inv_h;
+		float q, qn;
+
+		if (far == 0)
+		{
+			// Infinite far plane
+			q = INFINITE_FAR_PLANE_ADJUST - 1;
+			qn = near * (INFINITE_FAR_PLANE_ADJUST - 2);
+		}
+		else
+		{
+			q = -(far + near) * inv_d;
+			qn = -2 * (far * near) * inv_d;
+		}
+
+		Matrix4 mat;
+		mat[0][0] = A;		mat[0][1] = 0.0f;	mat[0][2] = C;	mat[0][3] = 0.0f;
+		mat[1][0] = 0.0f;	mat[1][1] = B;		mat[1][2] = D;	mat[1][3] = 0.0f;
+		mat[2][0] = 0.0f;	 mat[2][1] = 0.0f;	mat[2][2] = q;	mat[2][3] = qn;
+		mat[3][0] = 0.0f;	mat[3][1] = 0.0f;	mat[3][2] = -1;	mat[3][3] = 0.0f;
+
+		return mat;
+    }
+
+	Matrix4 Matrix4::view(const Vector3& position, const Quaternion& orientation, const Matrix4* reflectMatrix)
+    {
+		Matrix4 mat;
+		mat.makeView(position, orientation, reflectMatrix);
+
+		return mat;
+    }
+
 	Matrix4 Matrix4::TRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
 	Matrix4 Matrix4::TRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
 	{
 	{
 		Matrix4 mat;
 		Matrix4 mat;