OVR_Math.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   OVR_Math.h
00004 Content     :   Implementation of 3D primitives such as vectors, matrices.
00005 Created     :   September 4, 2012
00006 Authors     :   Andrew Reisse, Michael Antonov
00007 
00008 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00009 
00010 Use of this software is subject to the terms of the Oculus license
00011 agreement provided at the time of installation or download, or which
00012 otherwise accompanies this software in either electronic or hard copy form.
00013 
00014 *************************************************************************************/
00015 
00016 #include "OVR_Math.h"
00017 
00018 #include <float.h>
00019 
00020 namespace OVR {
00021 
00022 
00023 //-------------------------------------------------------------------------------------
00024 // ***** Math
00025 
00026 
00027 // Single-precision Math constants class.
00028 const float Math<float>::Pi      = 3.1415926f;
00029 const float Math<float>::TwoPi   = 3.1415926f * 2;
00030 const float Math<float>::PiOver2 = 3.1415926f / 2.0f;
00031 const float Math<float>::PiOver4 = 3.1415926f / 4.0f;
00032 const float Math<float>::E       = 2.7182818f;
00033 
00034 const float Math<float>::MaxValue = FLT_MAX;
00035 const float Math<float>::MinPositiveValue = FLT_MIN;
00036 
00037 const float Math<float>::RadToDegreeFactor = 360.0f / Math<float>::TwoPi;
00038 const float Math<float>::DegreeToRadFactor = Math<float>::TwoPi / 360.0f;
00039 
00040 const float Math<float>::Tolerance = 0.00001f;
00041 const float Math<float>::SingularityRadius = 0.0000001f; // Use for Gimbal lock numerical problems
00042 
00043 
00044 // Double-precision Math constants class.
00045 const double Math<double>::Pi      = 3.14159265358979;
00046 const double Math<double>::TwoPi   = 3.14159265358979 * 2;
00047 const double Math<double>::PiOver2 = 3.14159265358979 / 2.0;
00048 const double Math<double>::PiOver4 = 3.14159265358979 / 4.0;
00049 const double Math<double>::E       = 2.71828182845905;
00050 
00051 const double Math<double>::MaxValue = DBL_MAX;
00052 const double Math<double>::MinPositiveValue = DBL_MIN;
00053 
00054 const double Math<double>::RadToDegreeFactor = 360.0 / Math<double>::TwoPi;
00055 const double Math<double>::DegreeToRadFactor = Math<double>::TwoPi / 360.0;
00056 
00057 const double Math<double>::Tolerance = 0.00001;
00058 const double Math<double>::SingularityRadius = 0.000000000001; // Use for Gimbal lock numerical problems
00059 
00060 
00061 
00062 //-------------------------------------------------------------------------------------
00063 // ***** Matrix4f
00064 
00065 
00066 Matrix4f Matrix4f::LookAtRH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
00067 {
00068     Vector3f z = (eye - at).Normalized();  // Forward
00069     Vector3f x = up.Cross(z).Normalized(); // Right
00070     Vector3f y = z.Cross(x);
00071 
00072     Matrix4f m(x.x,  x.y,  x.z,  -(x * eye),
00073                y.x,  y.y,  y.z,  -(y * eye),
00074                z.x,  z.y,  z.z,  -(z * eye),
00075                0,    0,    0,    1 );
00076     return m;
00077 }
00078 
00079 Matrix4f Matrix4f::LookAtLH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
00080 {
00081     Vector3f z = (at - eye).Normalized();  // Forward
00082     Vector3f x = up.Cross(z).Normalized(); // Right
00083     Vector3f y = z.Cross(x);
00084 
00085     Matrix4f m(x.x,  x.y,  x.z,  -(x * eye),
00086                y.x,  y.y,  y.z,  -(y * eye),
00087                z.x,  z.y,  z.z,  -(z * eye),
00088                0,    0,    0,    1 ); 
00089     return m;
00090 }
00091 
00092 
00093 Matrix4f Matrix4f::PerspectiveLH(float yfov, float aspect, float znear, float zfar)
00094 {
00095     Matrix4f m;
00096     float    tanHalfFov = tan(yfov * 0.5f);
00097 
00098     m.M[0][0] = 1.0f / (aspect * tanHalfFov);
00099     m.M[1][1] = 1.0f / tanHalfFov;
00100     m.M[2][2] = zfar / (zfar - znear);
00101     m.M[3][2] = 1.0f;
00102     m.M[2][3] = (zfar * znear) / (znear - zfar);
00103     m.M[3][3] = 0.0f;
00104 
00105     // Note: Post-projection matrix result assumes Left-Handed coordinate system,
00106     //       with Y up, X right and Z forward. This supports positive z-buffer values.
00107     return m;
00108 }
00109 
00110 
00111 Matrix4f Matrix4f::PerspectiveRH(float yfov, float aspect, float znear, float zfar)
00112 {
00113     Matrix4f m;
00114     float    tanHalfFov = tan(yfov * 0.5f);
00115   
00116     m.M[0][0] = 1.0f / (aspect * tanHalfFov);
00117     m.M[1][1] = 1.0f / tanHalfFov;
00118     m.M[2][2] = zfar / (znear - zfar);
00119    // m.M[2][2] = zfar / (zfar - znear);
00120     m.M[3][2] = -1.0f;
00121     m.M[2][3] = (zfar * znear) / (znear - zfar);
00122     m.M[3][3] = 0.0f;
00123 
00124     // Note: Post-projection matrix result assumes Left-Handed coordinate system,    
00125     //       with Y up, X right and Z forward. This supports positive z-buffer values.
00126     // This is the case even for RHS cooridnate input.       
00127     return m;
00128 }
00129 
00130 
00131 /*
00132 OffCenterLH
00133 
00134 2*zn/(r-l)   0            0              0
00135 0            2*zn/(t-b)   0              0
00136 (l+r)/(l-r)  (t+b)/(b-t)  zf/(zf-zn)     1
00137 0            0            zn*zf/(zn-zf)  0
00138 
00139 */
00140 
00141 
00142 Matrix4f Matrix4f::Ortho2D(float w, float h)
00143 {
00144     Matrix4f m;
00145     m.M[0][0] = 2.0f/w;
00146     m.M[1][1] = -2.0f/h;
00147     m.M[0][3] = -1.0;
00148     m.M[1][3] = 1.0;
00149     m.M[2][2] = 0;
00150     return m;
00151 }
00152 
00153 }


oculus_sdk
Author(s): Tully Foote
autogenerated on Thu Jun 6 2019 20:13:48