oculus1

diff libovr/Src/Kernel/OVR_Math.cpp @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children b069a5c27388
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/Kernel/OVR_Math.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,1 @@
     1.4 +/************************************************************************************
     1.5 
     1.6 Filename    :   OVR_Math.h
     1.7 Content     :   Implementation of 3D primitives such as vectors, matrices.
     1.8 Created     :   September 4, 2012
     1.9 Authors     :   Andrew Reisse, Michael Antonov
    1.10 
    1.11 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
    1.12 
    1.13 Use of this software is subject to the terms of the Oculus license
    1.14 agreement provided at the time of installation or download, or which
    1.15 otherwise accompanies this software in either electronic or hard copy form.
    1.16 
    1.17 *************************************************************************************/
    1.18 
    1.19 #include "OVR_Math.h"
    1.20 
    1.21 #include <float.h>
    1.22 
    1.23 namespace OVR {
    1.24 
    1.25 
    1.26 //-------------------------------------------------------------------------------------
    1.27 // ***** Math
    1.28 
    1.29 
    1.30 // Single-precision Math constants class.
    1.31 const float Math<float>::Pi      = 3.1415926f;
    1.32 const float Math<float>::TwoPi   = 3.1415926f * 2;
    1.33 const float Math<float>::PiOver2 = 3.1415926f / 2.0f;
    1.34 const float Math<float>::PiOver4 = 3.1415926f / 4.0f;
    1.35 const float Math<float>::E       = 2.7182818f;
    1.36 
    1.37 const float Math<float>::MaxValue = FLT_MAX;
    1.38 const float Math<float>::MinPositiveValue = FLT_MIN;
    1.39 
    1.40 const float Math<float>::RadToDegreeFactor = 360.0f / Math<float>::TwoPi;
    1.41 const float Math<float>::DegreeToRadFactor = Math<float>::TwoPi / 360.0f;
    1.42 
    1.43 const float Math<float>::Tolerance = 0.00001f;
    1.44 const float Math<float>::SingularityRadius = 0.0000001f; // Use for Gimbal lock numerical problems
    1.45 
    1.46 
    1.47 // Double-precision Math constants class.
    1.48 const double Math<double>::Pi      = 3.14159265358979;
    1.49 const double Math<double>::TwoPi   = 3.14159265358979 * 2;
    1.50 const double Math<double>::PiOver2 = 3.14159265358979 / 2.0;
    1.51 const double Math<double>::PiOver4 = 3.14159265358979 / 4.0;
    1.52 const double Math<double>::E       = 2.71828182845905;
    1.53 
    1.54 const double Math<double>::MaxValue = DBL_MAX;
    1.55 const double Math<double>::MinPositiveValue = DBL_MIN;
    1.56 
    1.57 const double Math<double>::RadToDegreeFactor = 360.0 / Math<double>::TwoPi;
    1.58 const double Math<double>::DegreeToRadFactor = Math<double>::TwoPi / 360.0;
    1.59 
    1.60 const double Math<double>::Tolerance = 0.00001;
    1.61 const double Math<double>::SingularityRadius = 0.000000000001; // Use for Gimbal lock numerical problems
    1.62 
    1.63 
    1.64 
    1.65 //-------------------------------------------------------------------------------------
    1.66 // ***** Matrix4f
    1.67 
    1.68 
    1.69 Matrix4f Matrix4f::LookAtRH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
    1.70 {
    1.71     Vector3f z = (eye - at).Normalized();  // Forward
    1.72     Vector3f x = up.Cross(z).Normalized(); // Right
    1.73     Vector3f y = z.Cross(x);
    1.74 
    1.75     Matrix4f m(x.x,  x.y,  x.z,  -(x * eye),
    1.76                y.x,  y.y,  y.z,  -(y * eye),
    1.77                z.x,  z.y,  z.z,  -(z * eye),
    1.78                0,    0,    0,    1 );
    1.79     return m;
    1.80 }
    1.81 
    1.82 Matrix4f Matrix4f::LookAtLH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
    1.83 {
    1.84     Vector3f z = (at - eye).Normalized();  // Forward
    1.85     Vector3f x = up.Cross(z).Normalized(); // Right
    1.86     Vector3f y = z.Cross(x);
    1.87 
    1.88     Matrix4f m(x.x,  x.y,  x.z,  -(x * eye),
    1.89                y.x,  y.y,  y.z,  -(y * eye),
    1.90                z.x,  z.y,  z.z,  -(z * eye),
    1.91                0,    0,    0,    1 ); 
    1.92     return m;
    1.93 }
    1.94 
    1.95 
    1.96 Matrix4f Matrix4f::PerspectiveLH(float yfov, float aspect, float znear, float zfar)
    1.97 {
    1.98     Matrix4f m;
    1.99     float    tanHalfFov = tan(yfov * 0.5f);
   1.100 
   1.101     m.M[0][0] = 1.0f / (aspect * tanHalfFov);
   1.102     m.M[1][1] = 1.0f / tanHalfFov;
   1.103     m.M[2][2] = zfar / (zfar - znear);
   1.104     m.M[3][2] = 1.0f;
   1.105     m.M[2][3] = (zfar * znear) / (znear - zfar);
   1.106     m.M[3][3] = 0.0f;
   1.107 
   1.108     // Note: Post-projection matrix result assumes Left-Handed coordinate system,
   1.109     //       with Y up, X right and Z forward. This supports positive z-buffer values.
   1.110     return m;
   1.111 }
   1.112 
   1.113 
   1.114 Matrix4f Matrix4f::PerspectiveRH(float yfov, float aspect, float znear, float zfar)
   1.115 {
   1.116     Matrix4f m;
   1.117     float    tanHalfFov = tan(yfov * 0.5f);
   1.118   
   1.119     m.M[0][0] = 1.0f / (aspect * tanHalfFov);
   1.120     m.M[1][1] = 1.0f / tanHalfFov;
   1.121     m.M[2][2] = zfar / (znear - zfar);
   1.122    // m.M[2][2] = zfar / (zfar - znear);
   1.123     m.M[3][2] = -1.0f;
   1.124     m.M[2][3] = (zfar * znear) / (znear - zfar);
   1.125     m.M[3][3] = 0.0f;
   1.126 
   1.127     // Note: Post-projection matrix result assumes Left-Handed coordinate system,    
   1.128     //       with Y up, X right and Z forward. This supports positive z-buffer values.
   1.129     // This is the case even for RHS cooridnate input.       
   1.130     return m;
   1.131 }
   1.132 
   1.133 
   1.134 /*
   1.135 OffCenterLH
   1.136 
   1.137 2*zn/(r-l)   0            0              0
   1.138 0            2*zn/(t-b)   0              0
   1.139 (l+r)/(l-r)  (t+b)/(b-t)  zf/(zf-zn)     1
   1.140 0            0            zn*zf/(zn-zf)  0
   1.141 
   1.142 */
   1.143 
   1.144 
   1.145 Matrix4f Matrix4f::Ortho2D(float w, float h)
   1.146 {
   1.147     Matrix4f m;
   1.148     m.M[0][0] = 2.0f/w;
   1.149     m.M[1][1] = -2.0f/h;
   1.150     m.M[0][3] = -1.0;
   1.151     m.M[1][3] = 1.0;
   1.152     m.M[2][2] = 0;
   1.153     return m;
   1.154 }
   1.155 
   1.156 }
   1.157 \ No newline at end of file