2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2014-07-09 23:24:49 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2015-04-01 23:02:20 +03:00
|
|
|
#include "gfxVR.h"
|
2016-11-04 10:53:01 +03:00
|
|
|
#include "mozilla/dom/GamepadEventTypes.h"
|
|
|
|
#include "mozilla/dom/GamepadBinding.h"
|
2015-07-02 18:58:24 +03:00
|
|
|
|
|
|
|
#ifndef M_PI
|
|
|
|
# define M_PI 3.14159265358979323846
|
|
|
|
#endif
|
|
|
|
|
2015-04-07 20:26:44 +03:00
|
|
|
using namespace mozilla;
|
2014-07-09 23:24:49 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2017-01-24 12:49:11 +03:00
|
|
|
Atomic<uint32_t> VRSystemManager::sDisplayBase(0);
|
2017-07-14 12:01:09 +03:00
|
|
|
Atomic<uint32_t> VRSystemManager::sControllerBase(0);
|
2015-04-07 20:26:44 +03:00
|
|
|
|
|
|
|
/* static */ uint32_t
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManager::AllocateDisplayID()
|
2015-07-02 18:58:24 +03:00
|
|
|
{
|
2016-02-25 02:54:50 +03:00
|
|
|
return ++sDisplayBase;
|
2015-07-02 18:58:24 +03:00
|
|
|
}
|
|
|
|
|
2017-07-14 12:01:09 +03:00
|
|
|
/* static */ uint32_t
|
|
|
|
VRSystemManager::AllocateControllerID()
|
|
|
|
{
|
|
|
|
return ++sControllerBase;
|
|
|
|
}
|
|
|
|
|
2015-07-02 18:58:24 +03:00
|
|
|
Matrix4x4
|
2016-10-01 02:43:33 +03:00
|
|
|
VRFieldOfView::ConstructProjectionMatrix(float zNear, float zFar,
|
|
|
|
bool rightHanded) const
|
2015-07-02 18:58:24 +03:00
|
|
|
{
|
|
|
|
float upTan = tan(upDegrees * M_PI / 180.0);
|
|
|
|
float downTan = tan(downDegrees * M_PI / 180.0);
|
|
|
|
float leftTan = tan(leftDegrees * M_PI / 180.0);
|
|
|
|
float rightTan = tan(rightDegrees * M_PI / 180.0);
|
|
|
|
|
|
|
|
float handednessScale = rightHanded ? -1.0 : 1.0;
|
|
|
|
|
|
|
|
float pxscale = 2.0f / (leftTan + rightTan);
|
|
|
|
float pxoffset = (leftTan - rightTan) * pxscale * 0.5;
|
|
|
|
float pyscale = 2.0f / (upTan + downTan);
|
|
|
|
float pyoffset = (upTan - downTan) * pyscale * 0.5;
|
|
|
|
|
|
|
|
Matrix4x4 mobj;
|
|
|
|
float *m = &mobj._11;
|
|
|
|
|
|
|
|
m[0*4+0] = pxscale;
|
|
|
|
m[2*4+0] = pxoffset * handednessScale;
|
|
|
|
|
|
|
|
m[1*4+1] = pyscale;
|
|
|
|
m[2*4+1] = -pyoffset * handednessScale;
|
|
|
|
|
|
|
|
m[2*4+2] = zFar / (zNear - zFar) * -handednessScale;
|
|
|
|
m[3*4+2] = (zFar * zNear) / (zNear - zFar);
|
|
|
|
|
|
|
|
m[2*4+3] = handednessScale;
|
|
|
|
m[3*4+3] = 0.0f;
|
|
|
|
|
|
|
|
return mobj;
|
|
|
|
}
|
2016-10-07 11:58:01 +03:00
|
|
|
|
|
|
|
void
|
2017-02-06 11:12:52 +03:00
|
|
|
VRSystemManager::AddGamepad(const VRControllerInfo& controllerInfo)
|
2016-10-07 11:58:01 +03:00
|
|
|
{
|
2017-02-06 11:12:52 +03:00
|
|
|
dom::GamepadAdded a(NS_ConvertUTF8toUTF16(controllerInfo.GetControllerName()),
|
|
|
|
controllerInfo.GetMappingType(),
|
|
|
|
controllerInfo.GetHand(),
|
2017-07-14 12:01:09 +03:00
|
|
|
controllerInfo.GetDisplayID(),
|
2017-02-06 11:12:52 +03:00
|
|
|
controllerInfo.GetNumButtons(),
|
2017-02-02 09:57:58 +03:00
|
|
|
controllerInfo.GetNumAxes(),
|
|
|
|
controllerInfo.GetNumHaptics());
|
2016-10-07 11:58:01 +03:00
|
|
|
|
|
|
|
VRManager* vm = VRManager::Get();
|
2017-07-17 06:44:39 +03:00
|
|
|
vm->NotifyGamepadChange<dom::GamepadAdded>(mControllerCount, a);
|
2016-10-07 11:58:01 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 11:00:12 +03:00
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManager::RemoveGamepad(uint32_t aIndex)
|
2016-10-28 11:00:12 +03:00
|
|
|
{
|
2017-07-17 06:44:39 +03:00
|
|
|
dom::GamepadRemoved a;
|
2016-10-28 11:00:12 +03:00
|
|
|
|
|
|
|
VRManager* vm = VRManager::Get();
|
2017-07-17 06:44:39 +03:00
|
|
|
vm->NotifyGamepadChange<dom::GamepadRemoved>(aIndex, a);
|
2016-10-28 11:00:12 +03:00
|
|
|
}
|
|
|
|
|
2016-10-07 11:58:01 +03:00
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManager::NewButtonEvent(uint32_t aIndex, uint32_t aButton,
|
2017-04-06 14:00:20 +03:00
|
|
|
bool aPressed, bool aTouched, double aValue)
|
2016-10-07 11:58:01 +03:00
|
|
|
{
|
2017-07-17 06:44:39 +03:00
|
|
|
dom::GamepadButtonInformation a(aButton, aValue, aPressed, aTouched);
|
2016-10-07 11:58:01 +03:00
|
|
|
|
|
|
|
VRManager* vm = VRManager::Get();
|
2017-07-17 06:44:39 +03:00
|
|
|
vm->NotifyGamepadChange<dom::GamepadButtonInformation>(aIndex, a);
|
2016-10-07 11:58:01 +03:00
|
|
|
}
|
2016-10-21 19:01:26 +03:00
|
|
|
|
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManager::NewAxisMove(uint32_t aIndex, uint32_t aAxis,
|
|
|
|
double aValue)
|
2016-10-21 19:01:26 +03:00
|
|
|
{
|
2017-07-17 06:44:39 +03:00
|
|
|
dom::GamepadAxisInformation a(aAxis, aValue);
|
2016-10-21 19:01:26 +03:00
|
|
|
|
|
|
|
VRManager* vm = VRManager::Get();
|
2017-07-17 06:44:39 +03:00
|
|
|
vm->NotifyGamepadChange<dom::GamepadAxisInformation>(aIndex, a);
|
2016-10-21 19:01:26 +03:00
|
|
|
}
|
2016-10-24 13:09:11 +03:00
|
|
|
|
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManager::NewPoseState(uint32_t aIndex,
|
|
|
|
const dom::GamepadPoseState& aPose)
|
2016-10-24 13:09:11 +03:00
|
|
|
{
|
2017-07-17 06:44:39 +03:00
|
|
|
dom::GamepadPoseInformation a(aPose);
|
2016-10-24 13:09:11 +03:00
|
|
|
|
|
|
|
VRManager* vm = VRManager::Get();
|
2017-07-17 06:44:39 +03:00
|
|
|
vm->NotifyGamepadChange<dom::GamepadPoseInformation>(aIndex, a);
|
2016-10-24 13:09:11 +03:00
|
|
|
}
|
2017-04-18 10:58:34 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
VRSystemManager::NewHandChangeEvent(uint32_t aIndex,
|
|
|
|
const dom::GamepadHand aHand)
|
|
|
|
{
|
2017-07-17 06:44:39 +03:00
|
|
|
dom::GamepadHandInformation a(aHand);
|
2017-04-18 10:58:34 +03:00
|
|
|
|
|
|
|
VRManager* vm = VRManager::Get();
|
2017-07-17 06:44:39 +03:00
|
|
|
vm->NotifyGamepadChange<dom::GamepadHandInformation>(aIndex, a);
|
2017-04-18 10:58:34 +03:00
|
|
|
}
|
|
|
|
|