Work on WebVR pose input;

This commit is contained in:
bjorn 2018-05-01 15:10:51 -07:00 committed by bjornbytes
parent c343a3feb5
commit 498a1843ad
4 changed files with 68 additions and 14 deletions

View File

@ -12,6 +12,7 @@ if(EMSCRIPTEN)
"-s USE_GLFW=3 "
"-s USE_ZLIB=1 "
"-s ALLOW_MEMORY_GROWTH=1 "
"-s \"EXPORTED_FUNCTIONS=['_main','_quat_getAngleAxis']\" "
"-s \"EXTRA_EXPORTED_RUNTIME_METHODS=['getValue','setValue']\" "
"-s WASM=1 "
"--js-library ../src/resources/lovr.js"

View File

@ -301,8 +301,10 @@ int l_lovrHeadsetRenderTo(lua_State* L) {
}
int l_lovrHeadsetUpdate(lua_State* L) {
float dt = luaL_checknumber(L, 1);
lovrHeadsetDriver->update(dt);
if (lovrHeadsetDriver->update) {
lovrHeadsetDriver->update(luaL_checknumber(L, 1));
}
return 0;
}

View File

@ -3,6 +3,7 @@
#include "lib/vec/vec.h"
#include <stdbool.h>
// Provided by resources/lovr.js
extern void webvrInit(void);
extern HeadsetOrigin webvrGetOriginType(void);
extern bool webvrIsMirrored(void);
@ -11,6 +12,8 @@ extern void webvrGetDisplayDimensions(int32_t* width, int32_t* height);
extern void webvrGetClipDistance(float* near, float* far);
extern void webvrSetClipDistance(float near, float far);
extern void webvrGetBoundsDimensions(float* width, float* depth);
extern void webvrGetPose(float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az);
extern void webvrGetVelocity(float* x, float* y, float* z);
extern void webvrRenderTo(void (*callback)(void*), void* userdata);
typedef struct {
@ -21,6 +24,7 @@ static HeadsetState state;
static bool webvrDriverInit(float offset) {
state.offset = offset;
webvrInit();
return true;
}
@ -41,9 +45,9 @@ HeadsetInterface lovrHeadsetWebVRDriver = {
webvrGetClipDistance,
webvrSetClipDistance,
webvrGetBoundsDimensions,
NULL, //void (*getPose)(float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az);
webvrGetPose,
NULL, //void (*getEyePose)(HeadsetEye eye, float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az);
NULL, //void (*getVelocity)(float* x, float* y, float* z);
webvrGetVelocity,
NULL, //void (*getAngularVelocity)(float* x, float* y, float* z);
NULL, //vec_controller_t* (*getControllers)();
NULL, //bool (*controllerIsConnected)(Controller* controller);

View File

@ -24,13 +24,13 @@ var LibraryLOVR = {
lovr.WebVR.height = canvas.height;
lovr.WebVR.frameData = new VRFrameData();
!function findDisplay() {
function findDisplay() {
navigator.getVRDisplays && navigator.getVRDisplays().then(function(displays) {
lovr.WebVR.display = display = displays[0];
});
}();
}
!function onResize() {
function onResize() {
if (display && display.isPresenting) {
var eyeParams = display.getEyeParameters('left');
lovr.WebVR.width = eyeParams.renderWidth;
@ -41,7 +41,7 @@ var LibraryLOVR = {
canvas.width = lovr.WebVR.width = canvas.parentElement.offsetWidth * window.devicePixelRatio;
canvas.height = lovr.WebVR.height = canvas.parentElement.offsetHeight * window.devicePixelRatio;
}
}();
}
window.requestAnimationFrame(function onAnimationFrame() {
if (display) {
@ -84,6 +84,9 @@ var LibraryLOVR = {
window.addEventListener('vrdisplaypresentchange', onResize);
window.addEventListener('resize', onResize);
findDisplay();
onResize();
}
}
},
@ -105,13 +108,13 @@ var LibraryLOVR = {
},
webvrGetDisplayDimensions: function(width, height) {
Module.setValue(width, lovr.WebVR.width, 'i32');
Module.setValue(height, lovr.WebVR.height, 'i32');
HEAP32[width >> 2] = lovr.WebVR.width;
HEAP32[height >> 2] = lovr.WebVR.height;
},
webvrGetClipDistance: function(clipNear, clipFar) {
Module.setValue(clipNear, lovr.WebVR.display ? lovr.WebVR.display.depthNear : 0, 'float');
Module.setValue(clipFar, lovr.WebVR.display ? lovr.WebVR.display.depthFar : 0, 'float');
HEAPF32[clipNear >> 2] = lovr.WebVR.display ? lovr.WebVR.display.depthNear : 0;
HEAPF32[clipFar >> 2] = lovr.WebVR.display ? lovr.WebVR.display.depthFar : 0;
},
webvrSetClipDistance: function(clipNear, clipFar) {
@ -122,8 +125,52 @@ var LibraryLOVR = {
},
webvrGetBoundsDimensions: function(width, depth) {
Module.setValue(width, lovr.WebVR.display && lovr.WebVR.display.stageParameters ? lovr.WebVR.display.stageParameters.sizeX : 0);
Module.setValue(depth, lovr.WebVR.display && lovr.WebVR.display.stageParameters ? lovr.WebVR.display.stageParameters.sizeZ : 0);
var stage = lovr.WebVR.display && lovr.WebVR.display.stageParameters;
HEAPF32[width >> 2] = stage ? stage.sizeX : 0;
HEAPF32[depth >> 2] = stage ? stage.sizeZ : 0;
},
webvrGetPose: function(x, y, z, angle, ax, ay, az) {
var sittingToStanding = lovr.WebVR.display && lovr.WebVR.display.stageParameters && lovr.WebVR.display.stageParameters.sittingToStandingTransform;
var pose = lovr.WebVR.frameData.pose;
if (pose.position) {
HEAPF32[x >> 2] = pose.position[0];
HEAPF32[y >> 2] = pose.position[1];
HEAPF32[z >> 2] = pose.position[2];
if (sittingToStanding) {
//Module._mat4_transformPoint(sittingToStanding, x, y, z);
}
} else {
HEAPF32[x >> 2] = HEAPF32[y >> 2] = HEAPF32[z >> 2] = 0;
}
if (pose.orientation) {
if (sittingToStanding) {
//Module._mat4_set(lovr.WebVR.tempMat4, sittingToStanding);
//Module._mat4_rotateQuat(lovr.WebVR.tempMat4, pose.orientation);
//Module._quat_fromMat4(lovr.WebVR.tempQuat, lovr.WebVR.tempMat4);
//Module._quat_getAngleAxis(lovr.WebVR.tempQuat, angle, ax, ay, az);
} else {
Module._quat_getAngleAxis(pose.orientation, angle, ax, ay, az);
}
} else {
HEAPF32[angle >> 2] = HEAPF32[ax >> 2] = HEAPF32[ay >> 2] = HEAPF32[az >> 2] = 0;
}
},
webvrGetVelocity: function(x, y, z) {
var stage = lovr.WebVR.display && lovr.WebVR.display.stageParameters;
var pose = lovr.WebVR.frameData.pose;
if (pose.linearVelocity) {
HEAPF32[x >> 2] = pose.linearVelocity[0];
HEAPF32[y >> 2] = pose.linearVelocity[1];
HEAPF32[z >> 2] = pose.linearVelocity[2];
} else {
HEAPF32[x >> 2] = HEAPF32[y >> 2] = HEAPF32[z >> 2] = 0;
}
},
webvrRenderTo: function(callback, userdata) {