From 58a2d60ef7bda540db7946527d9d10c39c5bd7bf Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 2 Oct 2016 18:09:33 -0700 Subject: [PATCH] Controller basics; --- src/headset/headset.c | 4 ++++ src/headset/headset.h | 11 +++++++++++ src/headset/vive.c | 27 +++++++++++++++++++++++---- src/headset/vive.h | 1 + 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/headset/headset.c b/src/headset/headset.c index 201fb81e..2180806a 100644 --- a/src/headset/headset.c +++ b/src/headset/headset.c @@ -51,6 +51,10 @@ void lovrHeadsetGetAngularVelocity(float* x, float* y, float* z) { headset->interface->getAngularVelocity(headset, x, y, z); } +Controller* lovrHeadsetGetController(ControllerHand hand) { + return headset->interface->getController(headset, hand); +} + void lovrHeadsetRenderTo(headsetRenderCallback callback, void* userdata) { headset->interface->renderTo(headset, callback, userdata); } diff --git a/src/headset/headset.h b/src/headset/headset.h index 7a553734..07aa70bd 100644 --- a/src/headset/headset.h +++ b/src/headset/headset.h @@ -2,6 +2,15 @@ #define LOVR_HEADSET_TYPES typedef void (*headsetRenderCallback)(int eyeIndex, void* userdata); +typedef enum { + CONTROLLER_HAND_LEFT, + CONTROLLER_HAND_RIGHT +} ControllerHand; + +typedef struct { + ControllerHand hand; +} Controller; + typedef struct { int (*isPresent)(void* headset); const char* (*getType)(void* headset); @@ -14,6 +23,7 @@ typedef struct { void (*getOrientation)(void* headset, float* x, float* y, float* z, float* w); void (*getVelocity)(void* headset, float* x, float* y, float* z); void (*getAngularVelocity)(void* headset, float* x, float* y, float* z); + Controller* (*getController)(void* headset, ControllerHand hand); void (*renderTo)(void* headset, headsetRenderCallback callback, void* userdata); } HeadsetInterface; @@ -35,4 +45,5 @@ void lovrHeadsetGetPosition(float* x, float* y, float* z); void lovrHeadsetGetOrientation(float* x, float* y, float* z, float* w); void lovrHeadsetGetVelocity(float* x, float* y, float* z); void lovrHeadsetGetAngularVelocity(float* x, float* y, float* z); +Controller* lovrHeadsetGetController(ControllerHand hand); void lovrHeadsetRenderTo(headsetRenderCallback callback, void* userdata); diff --git a/src/headset/vive.c b/src/headset/vive.c index 6cc61219..add63ef0 100644 --- a/src/headset/vive.c +++ b/src/headset/vive.c @@ -9,7 +9,10 @@ typedef struct { struct VR_IVRCompositor_FnTable* vrCompositor; struct VR_IVRChaperone_FnTable* vrChaperone; - unsigned int deviceIndex; + unsigned int headsetIndex; + unsigned int controllerIndex[CONTROLLER_HAND_RIGHT + 1]; + + Controller* controllers[CONTROLLER_HAND_RIGHT + 1]; float clipNear; float clipFar; @@ -36,6 +39,7 @@ static HeadsetInterface interface = { .getOrientation = viveGetOrientation, .getVelocity = viveGetVelocity, .getAngularVelocity = viveGetAngularVelocity, + .getController = viveGetController, .renderTo = viveRenderTo }; @@ -83,11 +87,21 @@ Headset* viveInit() { return NULL; } - state->deviceIndex = k_unTrackedDeviceIndex_Hmd; + state->headsetIndex = k_unTrackedDeviceIndex_Hmd; state->clipNear = 0.1f; state->clipFar = 30.f; state->vrSystem->GetRecommendedRenderTargetSize(&state->renderWidth, &state->renderHeight); + Controller* leftController = malloc(sizeof(Controller)); + leftController->hand = CONTROLLER_HAND_LEFT; + state->controllers[CONTROLLER_HAND_LEFT] = leftController; + state->controllerIndex[CONTROLLER_HAND_LEFT] = state->vrSystem->GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole_TrackedControllerRole_LeftHand); + + Controller* rightController = malloc(sizeof(Controller)); + rightController->hand = CONTROLLER_HAND_RIGHT; + state->controllers[CONTROLLER_HAND_RIGHT] = rightController; + state->controllerIndex[CONTROLLER_HAND_RIGHT] = state->vrSystem->GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole_TrackedControllerRole_RightHand); + glGenFramebuffers(1, &state->framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, state->framebuffer); @@ -123,8 +137,7 @@ Headset* viveInit() { int viveIsPresent(void* headset) { Headset* this = (Headset*) headset; ViveState* state = this->state; - - return (int) state->vrSystem->IsTrackedDeviceConnected(state->deviceIndex); + return (int) state->vrSystem->IsTrackedDeviceConnected(state->headsetIndex); } const char* viveGetType(void* headset) { @@ -225,6 +238,12 @@ void viveGetAngularVelocity(void* headset, float* x, float* y, float* z) { *z = pose.vAngularVelocity.v[2]; } +Controller* viveGetController(void* headset, ControllerHand hand) { + Headset* this = headset; + ViveState* state = this->state; + return state->controllers[hand]; +} + void viveRenderTo(void* headset, headsetRenderCallback callback, void* userdata) { Headset* this = headset; ViveState* state = this->state; diff --git a/src/headset/vive.h b/src/headset/vive.h index 0402993b..e0640b78 100644 --- a/src/headset/vive.h +++ b/src/headset/vive.h @@ -14,4 +14,5 @@ void viveGetPosition(void* headset, float* x, float* y, float* z); void viveGetOrientation(void* headset, float* x, float* y, float* z, float* w); void viveGetVelocity(void* headset, float* x, float* y, float* z); void viveGetAngularVelocity(void* headset, float* x, float* y, float* z); +Controller* viveGetController(void* headset, ControllerHand hand); void viveRenderTo(void* headset, headsetRenderCallback callback, void* userdata);