oculus1

diff libovr/Src/Kernel/OVR_Math.cpp @ 3:b069a5c27388

added a couple more stuff, fixed all the LibOVR line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 04:10:05 +0300
parents e2f9e4603129
children
line diff
     1.1 --- a/libovr/Src/Kernel/OVR_Math.cpp	Sat Sep 14 17:51:03 2013 +0300
     1.2 +++ b/libovr/Src/Kernel/OVR_Math.cpp	Sun Sep 15 04:10:05 2013 +0300
     1.3 @@ -1,1 +1,153 @@
     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
   1.158 +/************************************************************************************
   1.159 +
   1.160 +Filename    :   OVR_Math.h
   1.161 +Content     :   Implementation of 3D primitives such as vectors, matrices.
   1.162 +Created     :   September 4, 2012
   1.163 +Authors     :   Andrew Reisse, Michael Antonov
   1.164 +
   1.165 +Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
   1.166 +
   1.167 +Use of this software is subject to the terms of the Oculus license
   1.168 +agreement provided at the time of installation or download, or which
   1.169 +otherwise accompanies this software in either electronic or hard copy form.
   1.170 +
   1.171 +*************************************************************************************/
   1.172 +
   1.173 +#include "OVR_Math.h"
   1.174 +
   1.175 +#include <float.h>
   1.176 +
   1.177 +namespace OVR {
   1.178 +
   1.179 +
   1.180 +//-------------------------------------------------------------------------------------
   1.181 +// ***** Math
   1.182 +
   1.183 +
   1.184 +// Single-precision Math constants class.
   1.185 +const float Math<float>::Pi      = 3.1415926f;
   1.186 +const float Math<float>::TwoPi   = 3.1415926f * 2;
   1.187 +const float Math<float>::PiOver2 = 3.1415926f / 2.0f;
   1.188 +const float Math<float>::PiOver4 = 3.1415926f / 4.0f;
   1.189 +const float Math<float>::E       = 2.7182818f;
   1.190 +
   1.191 +const float Math<float>::MaxValue = FLT_MAX;
   1.192 +const float Math<float>::MinPositiveValue = FLT_MIN;
   1.193 +
   1.194 +const float Math<float>::RadToDegreeFactor = 360.0f / Math<float>::TwoPi;
   1.195 +const float Math<float>::DegreeToRadFactor = Math<float>::TwoPi / 360.0f;
   1.196 +
   1.197 +const float Math<float>::Tolerance = 0.00001f;
   1.198 +const float Math<float>::SingularityRadius = 0.0000001f; // Use for Gimbal lock numerical problems
   1.199 +
   1.200 +
   1.201 +// Double-precision Math constants class.
   1.202 +const double Math<double>::Pi      = 3.14159265358979;
   1.203 +const double Math<double>::TwoPi   = 3.14159265358979 * 2;
   1.204 +const double Math<double>::PiOver2 = 3.14159265358979 / 2.0;
   1.205 +const double Math<double>::PiOver4 = 3.14159265358979 / 4.0;
   1.206 +const double Math<double>::E       = 2.71828182845905;
   1.207 +
   1.208 +const double Math<double>::MaxValue = DBL_MAX;
   1.209 +const double Math<double>::MinPositiveValue = DBL_MIN;
   1.210 +
   1.211 +const double Math<double>::RadToDegreeFactor = 360.0 / Math<double>::TwoPi;
   1.212 +const double Math<double>::DegreeToRadFactor = Math<double>::TwoPi / 360.0;
   1.213 +
   1.214 +const double Math<double>::Tolerance = 0.00001;
   1.215 +const double Math<double>::SingularityRadius = 0.000000000001; // Use for Gimbal lock numerical problems
   1.216 +
   1.217 +
   1.218 +
   1.219 +//-------------------------------------------------------------------------------------
   1.220 +// ***** Matrix4f
   1.221 +
   1.222 +
   1.223 +Matrix4f Matrix4f::LookAtRH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
   1.224 +{
   1.225 +    Vector3f z = (eye - at).Normalized();  // Forward
   1.226 +    Vector3f x = up.Cross(z).Normalized(); // Right
   1.227 +    Vector3f y = z.Cross(x);
   1.228 +
   1.229 +    Matrix4f m(x.x,  x.y,  x.z,  -(x * eye),
   1.230 +               y.x,  y.y,  y.z,  -(y * eye),
   1.231 +               z.x,  z.y,  z.z,  -(z * eye),
   1.232 +               0,    0,    0,    1 );
   1.233 +    return m;
   1.234 +}
   1.235 +
   1.236 +Matrix4f Matrix4f::LookAtLH(const Vector3f& eye, const Vector3f& at, const Vector3f& up)
   1.237 +{
   1.238 +    Vector3f z = (at - eye).Normalized();  // Forward
   1.239 +    Vector3f x = up.Cross(z).Normalized(); // Right
   1.240 +    Vector3f y = z.Cross(x);
   1.241 +
   1.242 +    Matrix4f m(x.x,  x.y,  x.z,  -(x * eye),
   1.243 +               y.x,  y.y,  y.z,  -(y * eye),
   1.244 +               z.x,  z.y,  z.z,  -(z * eye),
   1.245 +               0,    0,    0,    1 ); 
   1.246 +    return m;
   1.247 +}
   1.248 +
   1.249 +
   1.250 +Matrix4f Matrix4f::PerspectiveLH(float yfov, float aspect, float znear, float zfar)
   1.251 +{
   1.252 +    Matrix4f m;
   1.253 +    float    tanHalfFov = tan(yfov * 0.5f);
   1.254 +
   1.255 +    m.M[0][0] = 1.0f / (aspect * tanHalfFov);
   1.256 +    m.M[1][1] = 1.0f / tanHalfFov;
   1.257 +    m.M[2][2] = zfar / (zfar - znear);
   1.258 +    m.M[3][2] = 1.0f;
   1.259 +    m.M[2][3] = (zfar * znear) / (znear - zfar);
   1.260 +    m.M[3][3] = 0.0f;
   1.261 +
   1.262 +    // Note: Post-projection matrix result assumes Left-Handed coordinate system,
   1.263 +    //       with Y up, X right and Z forward. This supports positive z-buffer values.
   1.264 +    return m;
   1.265 +}
   1.266 +
   1.267 +
   1.268 +Matrix4f Matrix4f::PerspectiveRH(float yfov, float aspect, float znear, float zfar)
   1.269 +{
   1.270 +    Matrix4f m;
   1.271 +    float    tanHalfFov = tan(yfov * 0.5f);
   1.272 +  
   1.273 +    m.M[0][0] = 1.0f / (aspect * tanHalfFov);
   1.274 +    m.M[1][1] = 1.0f / tanHalfFov;
   1.275 +    m.M[2][2] = zfar / (znear - zfar);
   1.276 +   // m.M[2][2] = zfar / (zfar - znear);
   1.277 +    m.M[3][2] = -1.0f;
   1.278 +    m.M[2][3] = (zfar * znear) / (znear - zfar);
   1.279 +    m.M[3][3] = 0.0f;
   1.280 +
   1.281 +    // Note: Post-projection matrix result assumes Left-Handed coordinate system,    
   1.282 +    //       with Y up, X right and Z forward. This supports positive z-buffer values.
   1.283 +    // This is the case even for RHS cooridnate input.       
   1.284 +    return m;
   1.285 +}
   1.286 +
   1.287 +
   1.288 +/*
   1.289 +OffCenterLH
   1.290 +
   1.291 +2*zn/(r-l)   0            0              0
   1.292 +0            2*zn/(t-b)   0              0
   1.293 +(l+r)/(l-r)  (t+b)/(b-t)  zf/(zf-zn)     1
   1.294 +0            0            zn*zf/(zn-zf)  0
   1.295 +
   1.296 +*/
   1.297 +
   1.298 +
   1.299 +Matrix4f Matrix4f::Ortho2D(float w, float h)
   1.300 +{
   1.301 +    Matrix4f m;
   1.302 +    m.M[0][0] = 2.0f/w;
   1.303 +    m.M[1][1] = -2.0f/h;
   1.304 +    m.M[0][3] = -1.0;
   1.305 +    m.M[1][3] = 1.0;
   1.306 +    m.M[2][2] = 0;
   1.307 +    return m;
   1.308 +}
   1.309 +
   1.310 +}