Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include <math.h>
7 :
8 : #include "gfxVR.h"
9 : #include "mozilla/dom/GamepadEventTypes.h"
10 : #include "mozilla/dom/GamepadBinding.h"
11 :
12 : #ifndef M_PI
13 : # define M_PI 3.14159265358979323846
14 : #endif
15 :
16 : using namespace mozilla;
17 : using namespace mozilla::gfx;
18 :
19 : Atomic<uint32_t> VRSystemManager::sDisplayBase(0);
20 :
21 : /* static */ uint32_t
22 0 : VRSystemManager::AllocateDisplayID()
23 : {
24 0 : return ++sDisplayBase;
25 : }
26 :
27 : Matrix4x4
28 0 : VRFieldOfView::ConstructProjectionMatrix(float zNear, float zFar,
29 : bool rightHanded) const
30 : {
31 0 : float upTan = tan(upDegrees * M_PI / 180.0);
32 0 : float downTan = tan(downDegrees * M_PI / 180.0);
33 0 : float leftTan = tan(leftDegrees * M_PI / 180.0);
34 0 : float rightTan = tan(rightDegrees * M_PI / 180.0);
35 :
36 0 : float handednessScale = rightHanded ? -1.0 : 1.0;
37 :
38 0 : float pxscale = 2.0f / (leftTan + rightTan);
39 0 : float pxoffset = (leftTan - rightTan) * pxscale * 0.5;
40 0 : float pyscale = 2.0f / (upTan + downTan);
41 0 : float pyoffset = (upTan - downTan) * pyscale * 0.5;
42 :
43 0 : Matrix4x4 mobj;
44 0 : float *m = &mobj._11;
45 :
46 0 : m[0*4+0] = pxscale;
47 0 : m[2*4+0] = pxoffset * handednessScale;
48 :
49 0 : m[1*4+1] = pyscale;
50 0 : m[2*4+1] = -pyoffset * handednessScale;
51 :
52 0 : m[2*4+2] = zFar / (zNear - zFar) * -handednessScale;
53 0 : m[3*4+2] = (zFar * zNear) / (zNear - zFar);
54 :
55 0 : m[2*4+3] = handednessScale;
56 0 : m[3*4+3] = 0.0f;
57 :
58 0 : return mobj;
59 : }
60 :
61 : void
62 0 : VRSystemManager::AddGamepad(const VRControllerInfo& controllerInfo)
63 : {
64 0 : dom::GamepadAdded a(NS_ConvertUTF8toUTF16(controllerInfo.GetControllerName()),
65 : mControllerCount,
66 0 : controllerInfo.GetMappingType(),
67 0 : controllerInfo.GetHand(),
68 : dom::GamepadServiceType::VR,
69 0 : controllerInfo.GetNumButtons(),
70 0 : controllerInfo.GetNumAxes(),
71 0 : controllerInfo.GetNumHaptics());
72 :
73 0 : VRManager* vm = VRManager::Get();
74 0 : vm->NotifyGamepadChange<dom::GamepadAdded>(a);
75 0 : }
76 :
77 : void
78 0 : VRSystemManager::RemoveGamepad(uint32_t aIndex)
79 : {
80 0 : dom::GamepadRemoved a(aIndex, dom::GamepadServiceType::VR);
81 :
82 0 : VRManager* vm = VRManager::Get();
83 0 : vm->NotifyGamepadChange<dom::GamepadRemoved>(a);
84 0 : }
85 :
86 : void
87 0 : VRSystemManager::NewButtonEvent(uint32_t aIndex, uint32_t aButton,
88 : bool aPressed, bool aTouched, double aValue)
89 : {
90 : dom::GamepadButtonInformation a(aIndex, dom::GamepadServiceType::VR,
91 0 : aButton, aValue, aPressed, aTouched);
92 :
93 0 : VRManager* vm = VRManager::Get();
94 0 : vm->NotifyGamepadChange<dom::GamepadButtonInformation>(a);
95 0 : }
96 :
97 : void
98 0 : VRSystemManager::NewAxisMove(uint32_t aIndex, uint32_t aAxis,
99 : double aValue)
100 : {
101 : dom::GamepadAxisInformation a(aIndex, dom::GamepadServiceType::VR,
102 0 : aAxis, aValue);
103 :
104 0 : VRManager* vm = VRManager::Get();
105 0 : vm->NotifyGamepadChange<dom::GamepadAxisInformation>(a);
106 0 : }
107 :
108 : void
109 0 : VRSystemManager::NewPoseState(uint32_t aIndex,
110 : const dom::GamepadPoseState& aPose)
111 : {
112 : dom::GamepadPoseInformation a(aIndex, dom::GamepadServiceType::VR,
113 0 : aPose);
114 :
115 0 : VRManager* vm = VRManager::Get();
116 0 : vm->NotifyGamepadChange<dom::GamepadPoseInformation>(a);
117 0 : }
118 :
119 : void
120 0 : VRSystemManager::NewHandChangeEvent(uint32_t aIndex,
121 : const dom::GamepadHand aHand)
122 : {
123 : dom::GamepadHandInformation a(aIndex, dom::GamepadServiceType::VR,
124 0 : aHand);
125 :
126 0 : VRManager* vm = VRManager::Get();
127 0 : vm->NotifyGamepadChange<dom::GamepadHandInformation>(a);
128 0 : }
129 :
|