Commits

David Lin committed 695fd0c

Matrix4: Frustum matrix

  • Participants
  • Parent commits baa8fb6

Comments (0)

Files changed (2)

File engine/include/Mocha/MochaMath.h

     static const F32 ONE = 1.0f;
     static const F32 TWO = 2.0f;
     static const F32 HALF = 0.5f;
+    static const F32 NEG_ONE = -1.0f;
     static const F32 PI = 3.141592653589793f;
     static const F32 HALF_PI = 1.570796326794897f;
     static const F32 RECIP_PI = 0.318309886183791f;

File engine/include/Mocha/MochaMathMatrix.h

   public:
     static const Matrix4<T> identity();
     
+    static const Matrix4<T> frustumOpenGL
+    ( T xLeft, T xRight, T yBottom, T yTop, T zNear, T zFar );
+    
   private:
     Vector4<T> m_Col0;
     Vector4<T> m_Col1;
   }
   
   template<typename T>
-  const Matrix3<T> Matrix3<T>::rotation( T radians, const Vector3<T>& unitVec ){
+  const Matrix3<T> Matrix3<T>::rotation( T radians, const Vector3<T>& unitVec )
+  {
     T s, c;
     sincos( radians, s, c );
     const T x = unitVec.getX(), y = unitVec.getY(), z = unitVec.getZ();
                       Vector4<T>::zAxis(), Vector4<T>::wAxis());
   }
   
+  template<typename T>
+  const Matrix4<T> Matrix4<T>::frustumOpenGL
+  ( T xLeft, T xRight, T yBottom, T yTop, T zNear, T zFar ){
+    const T n2 = add( zNear, zNear );
+    const T rl = recip(sub( xRight, xLeft ));
+    const T tb = recip(sub( yTop, yBottom ));
+    const T nf = recip(sub( zNear, zFar ));
+    const T zero = Const<T>::ZERO;
+    
+    const Vector4<T> col0( mul(n2, rl), zero, zero, zero );
+    
+    const Vector4<T> col1( zero, mul(n2, tb), zero, zero );
+    
+    const Vector4<T> col2(mul( add( xRight, xLeft ), rl ), 
+                          mul( add( yTop, yBottom ), tb ),
+                          mul( add( zFar, zNear ), nf ), 
+                          Const<T>::NEG_ONE );
+    
+    const Vector4<T> col3( zero, zero, mul(mul(n2, zFar), nf), zero );
+    
+    return Matrix4<T>(col0, col1, col2, col3);
+  }
+  
+  
 }
 
 ////////////////////////////////////////////////////////////////////////////////