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