tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gfxVR.cpp (3159B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <math.h>
      8 
      9 #include "gfxVR.h"
     10 
     11 #ifndef M_PI
     12 #  define M_PI 3.14159265358979323846
     13 #endif
     14 
     15 using namespace mozilla;
     16 using namespace mozilla::gfx;
     17 
     18 Matrix4x4 VRFieldOfView::ConstructProjectionMatrix(float zNear, float zFar,
     19                                                   bool rightHanded) const {
     20  float upTan = tan(upDegrees * M_PI / 180.0);
     21  float downTan = tan(downDegrees * M_PI / 180.0);
     22  float leftTan = tan(leftDegrees * M_PI / 180.0);
     23  float rightTan = tan(rightDegrees * M_PI / 180.0);
     24 
     25  float handednessScale = rightHanded ? -1.0 : 1.0;
     26 
     27  float pxscale = 2.0f / (leftTan + rightTan);
     28  float pxoffset = (leftTan - rightTan) * pxscale * 0.5;
     29  float pyscale = 2.0f / (upTan + downTan);
     30  float pyoffset = (upTan - downTan) * pyscale * 0.5;
     31 
     32  Matrix4x4 mobj;
     33  float* m = &mobj._11;
     34 
     35  m[0 * 4 + 0] = pxscale;
     36  m[2 * 4 + 0] = pxoffset * handednessScale;
     37 
     38  m[1 * 4 + 1] = pyscale;
     39  m[2 * 4 + 1] = -pyoffset * handednessScale;
     40 
     41  m[2 * 4 + 2] = zFar / (zNear - zFar) * -handednessScale;
     42  m[3 * 4 + 2] = (zFar * zNear) / (zNear - zFar);
     43 
     44  m[2 * 4 + 3] = handednessScale;
     45  m[3 * 4 + 3] = 0.0f;
     46 
     47  return mobj;
     48 }
     49 
     50 void VRHMDSensorState::CalcViewMatrices(
     51    const gfx::Matrix4x4* aHeadToEyeTransforms) {
     52  gfx::Matrix4x4 matHead;
     53  if (flags & VRDisplayCapabilityFlags::Cap_Orientation) {
     54    matHead.SetRotationFromQuaternion(
     55        gfx::Quaternion(-pose.orientation[0], -pose.orientation[1],
     56                        -pose.orientation[2], pose.orientation[3]));
     57  }
     58  matHead.PreTranslate(-pose.position[0], -pose.position[1], -pose.position[2]);
     59 
     60  gfx::Matrix4x4 matView =
     61      matHead * aHeadToEyeTransforms[VRDisplayState::Eye_Left];
     62  matView.Normalize();
     63  memcpy(leftViewMatrix.data(), matView.components, sizeof(matView.components));
     64  matView = matHead * aHeadToEyeTransforms[VRDisplayState::Eye_Right];
     65  matView.Normalize();
     66  memcpy(rightViewMatrix.data(), matView.components,
     67         sizeof(matView.components));
     68 }
     69 
     70 const IntSize VRDisplayInfo::SuggestedEyeResolution() const {
     71  return IntSize(mDisplayState.eyeResolution.width,
     72                 mDisplayState.eyeResolution.height);
     73 }
     74 
     75 const Point3D VRDisplayInfo::GetEyeTranslation(uint32_t whichEye) const {
     76  return Point3D(mDisplayState.eyeTranslation[whichEye].x,
     77                 mDisplayState.eyeTranslation[whichEye].y,
     78                 mDisplayState.eyeTranslation[whichEye].z);
     79 }
     80 
     81 const Size VRDisplayInfo::GetStageSize() const {
     82  return Size(mDisplayState.stageSize.width, mDisplayState.stageSize.height);
     83 }
     84 
     85 const Matrix4x4 VRDisplayInfo::GetSittingToStandingTransform() const {
     86  Matrix4x4 m;
     87  // If we could replace Matrix4x4 with a pod type, we could
     88  // use it directly from the VRDisplayInfo struct.
     89  memcpy(m.components, mDisplayState.sittingToStandingTransform.data(),
     90         sizeof(float) * 16);
     91  return m;
     92 }