oculus1

annotate libovr/Src/Kernel/OVR_Math.cpp @ 12:d797639e0234

moving on to the distortion... not correct yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 10:14:29 +0300
parents e2f9e4603129
children
rev   line source
nuclear@3 1 /************************************************************************************
nuclear@3 2
nuclear@3 3 Filename : OVR_Math.h
nuclear@3 4 Content : Implementation of 3D primitives such as vectors, matrices.
nuclear@3 5 Created : September 4, 2012
nuclear@3 6 Authors : Andrew Reisse, Michael Antonov
nuclear@3 7
nuclear@3 8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@3 9
nuclear@3 10 Use of this software is subject to the terms of the Oculus license
nuclear@3 11 agreement provided at the time of installation or download, or which
nuclear@3 12 otherwise accompanies this software in either electronic or hard copy form.
nuclear@3 13
nuclear@3 14 *************************************************************************************/
nuclear@3 15
nuclear@3 16 #include "OVR_Math.h"
nuclear@3 17
nuclear@3 18 #include <float.h>
nuclear@3 19
nuclear@3 20 namespace OVR {
nuclear@3 21
nuclear@3 22
nuclear@3 23 //-------------------------------------------------------------------------------------
nuclear@3 24 // ***** Math
nuclear@3 25
nuclear@3 26
nuclear@3 27 // Single-precision Math constants class.
nuclear@3 28 const float Math<float>::Pi = 3.1415926f;
nuclear@3 29 const float Math<float>::TwoPi = 3.1415926f * 2;
nuclear@3 30 const float Math<float>::PiOver2 = 3.1415926f / 2.0f;
nuclear@3 31 const float Math<float>::PiOver4 = 3.1415926f / 4.0f;
nuclear@3 32 const float Math<float>::E = 2.7182818f;
nuclear@3 33
nuclear@3 34 const float Math<float>::MaxValue = FLT_MAX;
nuclear@3 35 const float Math<float>::MinPositiveValue = FLT_MIN;
nuclear@3 36
nuclear@3 37 const float Math<float>::RadToDegreeFactor = 360.0f / Math<float>::TwoPi;
nuclear@3 38 const float Math<float>::DegreeToRadFactor = Math<float>::TwoPi / 360.0f;
nuclear@3 39
nuclear@3 40 const float Math<float>::Tolerance = 0.00001f;
nuclear@3 41 const float Math<float>::SingularityRadius = 0.0000001f; // Use for Gimbal lock numerical problems
nuclear@3 42
nuclear@3 43
nuclear@3 44 // Double-precision Math constants class.
nuclear@3 45 const double Math<double>::Pi = 3.14159265358979;
nuclear@3 46 const double Math<double>::TwoPi = 3.14159265358979 * 2;
nuclear@3 47 const double Math<double>::PiOver2 = 3.14159265358979 / 2.0;
nuclear@3 48 const double Math<double>::PiOver4 = 3.14159265358979 / 4.0;
nuclear@3 49 const double Math<double>::E = 2.71828182845905;
nuclear@3 50
nuclear@3 51 const double Math<double>::MaxValue = DBL_MAX;
nuclear@3 52 const double Math<double>::MinPositiveValue = DBL_MIN;
nuclear@3 53
nuclear@3 54 const double Math<double>::RadToDegreeFactor = 360.0 / Math<double>::TwoPi;
nuclear@3 55 const double Math<double>::DegreeToRadFactor = Math<double>::TwoPi / 360.0;
nuclear@3 56
nuclear@3 57 const double Math<double>::Tolerance = 0.00001;
nuclear@3 58 const double Math<double>::SingularityRadius = 0.000000000001; // Use for Gimbal lock numerical problems
nuclear@3 59
nuclear@3 60
nuclear@3 61
nuclear@3 62 //-------------------------------------------------------------------------------------
nuclear@3 63 // ***** Matrix4f
nuclear@3 64
nuclear@3 65
nuclear@3 66 Matrix4f Matrix4f::LookAtRH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
nuclear@3 67 {
nuclear@3 68 Vector3f z = (eye - at).Normalized(); // Forward
nuclear@3 69 Vector3f x = up.Cross(z).Normalized(); // Right
nuclear@3 70 Vector3f y = z.Cross(x);
nuclear@3 71
nuclear@3 72 Matrix4f m(x.x, x.y, x.z, -(x * eye),
nuclear@3 73 y.x, y.y, y.z, -(y * eye),
nuclear@3 74 z.x, z.y, z.z, -(z * eye),
nuclear@3 75 0, 0, 0, 1 );
nuclear@3 76 return m;
nuclear@3 77 }
nuclear@3 78
nuclear@3 79 Matrix4f Matrix4f::LookAtLH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
nuclear@3 80 {
nuclear@3 81 Vector3f z = (at - eye).Normalized(); // Forward
nuclear@3 82 Vector3f x = up.Cross(z).Normalized(); // Right
nuclear@3 83 Vector3f y = z.Cross(x);
nuclear@3 84
nuclear@3 85 Matrix4f m(x.x, x.y, x.z, -(x * eye),
nuclear@3 86 y.x, y.y, y.z, -(y * eye),
nuclear@3 87 z.x, z.y, z.z, -(z * eye),
nuclear@3 88 0, 0, 0, 1 );
nuclear@3 89 return m;
nuclear@3 90 }
nuclear@3 91
nuclear@3 92
nuclear@3 93 Matrix4f Matrix4f::PerspectiveLH(float yfov, float aspect, float znear, float zfar)
nuclear@3 94 {
nuclear@3 95 Matrix4f m;
nuclear@3 96 float tanHalfFov = tan(yfov * 0.5f);
nuclear@3 97
nuclear@3 98 m.M[0][0] = 1.0f / (aspect * tanHalfFov);
nuclear@3 99 m.M[1][1] = 1.0f / tanHalfFov;
nuclear@3 100 m.M[2][2] = zfar / (zfar - znear);
nuclear@3 101 m.M[3][2] = 1.0f;
nuclear@3 102 m.M[2][3] = (zfar * znear) / (znear - zfar);
nuclear@3 103 m.M[3][3] = 0.0f;
nuclear@3 104
nuclear@3 105 // Note: Post-projection matrix result assumes Left-Handed coordinate system,
nuclear@3 106 // with Y up, X right and Z forward. This supports positive z-buffer values.
nuclear@3 107 return m;
nuclear@3 108 }
nuclear@3 109
nuclear@3 110
nuclear@3 111 Matrix4f Matrix4f::PerspectiveRH(float yfov, float aspect, float znear, float zfar)
nuclear@3 112 {
nuclear@3 113 Matrix4f m;
nuclear@3 114 float tanHalfFov = tan(yfov * 0.5f);
nuclear@3 115
nuclear@3 116 m.M[0][0] = 1.0f / (aspect * tanHalfFov);
nuclear@3 117 m.M[1][1] = 1.0f / tanHalfFov;
nuclear@3 118 m.M[2][2] = zfar / (znear - zfar);
nuclear@3 119 // m.M[2][2] = zfar / (zfar - znear);
nuclear@3 120 m.M[3][2] = -1.0f;
nuclear@3 121 m.M[2][3] = (zfar * znear) / (znear - zfar);
nuclear@3 122 m.M[3][3] = 0.0f;
nuclear@3 123
nuclear@3 124 // Note: Post-projection matrix result assumes Left-Handed coordinate system,
nuclear@3 125 // with Y up, X right and Z forward. This supports positive z-buffer values.
nuclear@3 126 // This is the case even for RHS cooridnate input.
nuclear@3 127 return m;
nuclear@3 128 }
nuclear@3 129
nuclear@3 130
nuclear@3 131 /*
nuclear@3 132 OffCenterLH
nuclear@3 133
nuclear@3 134 2*zn/(r-l) 0 0 0
nuclear@3 135 0 2*zn/(t-b) 0 0
nuclear@3 136 (l+r)/(l-r) (t+b)/(b-t) zf/(zf-zn) 1
nuclear@3 137 0 0 zn*zf/(zn-zf) 0
nuclear@3 138
nuclear@3 139 */
nuclear@3 140
nuclear@3 141
nuclear@3 142 Matrix4f Matrix4f::Ortho2D(float w, float h)
nuclear@3 143 {
nuclear@3 144 Matrix4f m;
nuclear@3 145 m.M[0][0] = 2.0f/w;
nuclear@3 146 m.M[1][1] = -2.0f/h;
nuclear@3 147 m.M[0][3] = -1.0;
nuclear@3 148 m.M[1][3] = 1.0;
nuclear@3 149 m.M[2][2] = 0;
nuclear@3 150 return m;
nuclear@3 151 }
nuclear@3 152
nuclear@3 153 }