Backed out 6 changesets (bug 1843477) for causing hazards failures. CLOSED TREE
[gecko.git] / gfx / vr / gfxVR.cpp
blobed1c6d32d6f6cf9444bd4ec40d190ff476b100fe
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/. */
7 #include <math.h>
9 #include "gfxVR.h"
11 #ifndef M_PI
12 # define M_PI 3.14159265358979323846
13 #endif
15 using namespace mozilla;
16 using namespace mozilla::gfx;
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);
25 float handednessScale = rightHanded ? -1.0 : 1.0;
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;
32 Matrix4x4 mobj;
33 float* m = &mobj._11;
35 m[0 * 4 + 0] = pxscale;
36 m[2 * 4 + 0] = pxoffset * handednessScale;
38 m[1 * 4 + 1] = pyscale;
39 m[2 * 4 + 1] = -pyoffset * handednessScale;
41 m[2 * 4 + 2] = zFar / (zNear - zFar) * -handednessScale;
42 m[3 * 4 + 2] = (zFar * zNear) / (zNear - zFar);
44 m[2 * 4 + 3] = handednessScale;
45 m[3 * 4 + 3] = 0.0f;
47 return mobj;
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]));
58 matHead.PreTranslate(-pose.position[0], -pose.position[1], -pose.position[2]);
60 gfx::Matrix4x4 matView =
61 matHead * aHeadToEyeTransforms[VRDisplayState::Eye_Left];
62 matView.Normalize();
63 memcpy(leftViewMatrix, matView.components, sizeof(matView.components));
64 matView = matHead * aHeadToEyeTransforms[VRDisplayState::Eye_Right];
65 matView.Normalize();
66 memcpy(rightViewMatrix, matView.components, sizeof(matView.components));
69 const IntSize VRDisplayInfo::SuggestedEyeResolution() const {
70 return IntSize(mDisplayState.eyeResolution.width,
71 mDisplayState.eyeResolution.height);
74 const Point3D VRDisplayInfo::GetEyeTranslation(uint32_t whichEye) const {
75 return Point3D(mDisplayState.eyeTranslation[whichEye].x,
76 mDisplayState.eyeTranslation[whichEye].y,
77 mDisplayState.eyeTranslation[whichEye].z);
80 const Size VRDisplayInfo::GetStageSize() const {
81 return Size(mDisplayState.stageSize.width, mDisplayState.stageSize.height);
84 const Matrix4x4 VRDisplayInfo::GetSittingToStandingTransform() const {
85 Matrix4x4 m;
86 // If we could replace Matrix4x4 with a pod type, we could
87 // use it directly from the VRDisplayInfo struct.
88 memcpy(m.components, mDisplayState.sittingToStandingTransform,
89 sizeof(float) * 16);
90 return m;