mirror of https://github.com/bjornbytes/lovr.git
Remove osvr; Use openvr;
This commit is contained in:
parent
d13cefdb9c
commit
4cd0bf06c8
|
@ -1,11 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.0.0)
|
||||
project(lovr)
|
||||
|
||||
option(OSVR_PATH "The path to the osvr snapshot")
|
||||
|
||||
file(GLOB_RECURSE LOVR_SRC src/*.c)
|
||||
|
||||
add_executable(lovr ${LOVR_SRC})
|
||||
|
||||
function(move_dll ARG_TARGET)
|
||||
add_custom_command(TARGET lovr POST_BUILD
|
||||
|
@ -15,6 +8,10 @@ function(move_dll ARG_TARGET)
|
|||
)
|
||||
endfunction()
|
||||
|
||||
project(lovr)
|
||||
file(GLOB_RECURSE LOVR_SRC src/*.c)
|
||||
add_executable(lovr ${LOVR_SRC})
|
||||
|
||||
# LuaJIT
|
||||
add_subdirectory(deps/luajit luajit)
|
||||
include_directories(deps/luajit/src ${CMAKE_BINARY_DIR}/luajit)
|
||||
|
@ -31,21 +28,20 @@ include_directories(deps/glfw/include)
|
|||
set(LOVR_GLFW glfw ${GLFW_LIBRARIES})
|
||||
|
||||
# assimp
|
||||
option(ASSIMP_BUILD_ASSIMP_TOOLS OFF)
|
||||
option(ASSIMP_BUILD_TESTS OFF)
|
||||
add_subdirectory(deps/assimp assimp)
|
||||
include_directories(deps/assimp/include)
|
||||
set(LOVR_ASSIMP assimp)
|
||||
|
||||
# osvr (todo)
|
||||
include(${OSVR_PATH}/lib/cmake/osvr/osvrConfig.cmake)
|
||||
include_directories(${OSVR_PATH}/include)
|
||||
target_link_libraries(lovr osvr::osvrClient)
|
||||
target_link_libraries(lovr osvr::osvrClientKit)
|
||||
target_link_libraries(lovr osvr::osvrCommon)
|
||||
target_link_libraries(lovr osvr::osvrUtil)
|
||||
move_dll(osvr::osvrCommon)
|
||||
move_dll(osvr::osvrUtil)
|
||||
move_dll(osvr::osvrClient)
|
||||
move_dll(osvr::osvrClientKit)
|
||||
# openvr (todo)
|
||||
include_directories(deps/openvr/headers)
|
||||
target_link_libraries(lovr ${CMAKE_SOURCE_DIR}/deps/openvr/lib/win32/openvr_api.lib)
|
||||
add_custom_command(TARGET lovr POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${CMAKE_SOURCE_DIR}/deps/openvr/bin/win32/openvr_api.dll"
|
||||
$<TARGET_FILE_DIR:lovr>
|
||||
)
|
||||
|
||||
set(LOVR_LIB
|
||||
${LOVR_LUA}
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
#include "event.h"
|
||||
#include "../lovr.h"
|
||||
#include "../glfw.h"
|
||||
#include "../osvr.h"
|
||||
|
||||
extern OSVR_ClientContext ctx;
|
||||
|
||||
void lovrEventPoll() {
|
||||
glfwPollEvents();
|
||||
|
||||
if (ctx != NULL && osvrClientCheckStatus(ctx) != OSVR_RETURN_FAILURE) {
|
||||
osvrClientUpdate(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrEventQuit() {
|
||||
|
|
17
src/glfw.c
17
src/glfw.c
|
@ -1,4 +1,5 @@
|
|||
#include "glfw.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "util.h"
|
||||
|
||||
|
@ -15,8 +16,20 @@ void initGlfw(GLFWerrorfun onError, GLFWwindowclosefun onClose) {
|
|||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||
|
||||
// TODO make configurable
|
||||
window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
|
||||
int count;
|
||||
GLFWmonitor** monitors = glfwGetMonitors(&count);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
const GLFWvidmode* mode = glfwGetVideoMode(monitors[i]);
|
||||
if (mode->refreshRate == 90) {
|
||||
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
|
||||
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
|
||||
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
|
||||
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
|
||||
window = glfwCreateWindow(mode->width, mode->height, "Window", monitors[i], NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
|
|
|
@ -1,109 +1,78 @@
|
|||
#include "headset.h"
|
||||
#include "../util.h"
|
||||
#include "../glfw.h"
|
||||
typedef char bool;
|
||||
#include <openvr_capi.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern OSVR_ClientContext ctx;
|
||||
extern __declspec(dllimport) bool VR_IsHmdPresent();
|
||||
extern __declspec(dllimport) bool VR_IsRuntimeInstalled();
|
||||
extern __declspec(dllimport) intptr_t VR_InitInternal(EVRInitError *peError, EVRApplicationType eType);
|
||||
extern __declspec(dllimport) bool VR_IsInterfaceVersionValid(const char *pchInterfaceVersion);
|
||||
extern __declspec(dllimport) intptr_t VR_GetGenericInterface(const char *pchInterfaceVersion, EVRInitError *peError);
|
||||
|
||||
typedef struct {
|
||||
OSVR_ClientInterface interface;
|
||||
OSVR_DisplayConfig displayConfig;
|
||||
struct VR_IVRSystem_FnTable* vrSystem;
|
||||
GLuint framebuffers[2];
|
||||
GLuint renderbuffers[2];
|
||||
GLuint textures[2];
|
||||
} HeadsetState;
|
||||
|
||||
static HeadsetState headsetState;
|
||||
|
||||
void lovrHeadsetInit() {
|
||||
initOsvr();
|
||||
if (!VR_IsHmdPresent()) {
|
||||
error("Warning: HMD not found");
|
||||
} else if (!VR_IsRuntimeInstalled()) {
|
||||
error("Warning: SteamVR not found");
|
||||
}
|
||||
|
||||
if (ctx == NULL) {
|
||||
headsetState.interface = NULL;
|
||||
EVRInitError vrError;
|
||||
uint32_t vrHandle = VR_InitInternal(&vrError, EVRApplicationType_VRApplication_Scene);
|
||||
|
||||
if (vrError != EVRInitError_VRInitError_None) {
|
||||
error("Problem initializing OpenVR");
|
||||
return;
|
||||
}
|
||||
|
||||
osvrClientGetInterface(ctx, "/me/head", &headsetState.interface);
|
||||
if (osvrClientGetDisplay(ctx, &headsetState.displayConfig) != OSVR_RETURN_SUCCESS) {
|
||||
error("Could not get headset display config");
|
||||
if (!VR_IsInterfaceVersionValid(IVRSystem_Version)) {
|
||||
error("Invalid OpenVR version");
|
||||
return;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (osvrClientCheckDisplayStartup(headsetState.displayConfig) != OSVR_RETURN_SUCCESS && i++ < 1000) {
|
||||
osvrClientUpdate(ctx);
|
||||
char fnTableName[128];
|
||||
sprintf(fnTableName, "FnTable:%s", IVRSystem_Version);
|
||||
headsetState.vrSystem = (struct VR_IVRSystem_FnTable*) VR_GetGenericInterface(fnTableName, &vrError);
|
||||
if (vrError != EVRInitError_VRInitError_None || headsetState.vrSystem == NULL) {
|
||||
error("Problem initializing OpenVR");
|
||||
return;
|
||||
}
|
||||
|
||||
glGenFramebuffers(2, headsetState.framebuffers);
|
||||
glGenRenderbuffers(2, headsetState.renderbuffers);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, headsetState.framebuffers[i]);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, headsetState.framebuffers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrHeadsetGetPosition(float* x, float* y, float* z) {
|
||||
if (headsetState.interface) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_PositionState state;
|
||||
osvrGetPositionState(headsetState.interface, ×tamp, &state);
|
||||
*x = osvrVec3GetX(&state);
|
||||
*y = osvrVec3GetY(&state);
|
||||
*z = osvrVec3GetZ(&state);
|
||||
} else {
|
||||
*x = *y = *z = 0.f;
|
||||
}
|
||||
TrackedDevicePose_t poses[16];
|
||||
headsetState.vrSystem->GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin_TrackingUniverseStanding, 0.f, poses, 16);
|
||||
TrackedDevicePose_t hmdPose = poses[k_unTrackedDeviceIndex_Hmd];
|
||||
*x = hmdPose.mDeviceToAbsoluteTracking.m[2][0];
|
||||
*y = hmdPose.mDeviceToAbsoluteTracking.m[2][1];
|
||||
*z = hmdPose.mDeviceToAbsoluteTracking.m[2][2];
|
||||
}
|
||||
|
||||
void lovrHeadsetGetOrientation(float* w, float* x, float* y, float* z) {
|
||||
if (headsetState.interface) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_OrientationState state;
|
||||
osvrGetOrientationState(headsetState.interface, ×tamp, &state);
|
||||
*w = osvrQuatGetW(&state);
|
||||
*x = osvrQuatGetX(&state);
|
||||
*y = osvrQuatGetY(&state);
|
||||
*z = osvrQuatGetZ(&state);
|
||||
} else {
|
||||
*w = *x = *y = *z = 0.f;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int lovrHeadsetIsPresent() {
|
||||
return headsetState.interface != NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void lovrHeadsetRenderTo(headsetRenderCallback callback, void* userdata) {
|
||||
if (!headsetState.interface) {
|
||||
return;
|
||||
}
|
||||
|
||||
OSVR_DisplayConfig displayConfig = headsetState.displayConfig;
|
||||
|
||||
OSVR_ViewerCount viewerCount;
|
||||
osvrClientGetNumViewers(displayConfig, &viewerCount);
|
||||
|
||||
for (OSVR_ViewerCount viewer = 0; viewer < viewerCount; viewer++) {
|
||||
OSVR_EyeCount eyeCount;
|
||||
osvrClientGetNumEyesForViewer(displayConfig, viewer, &eyeCount);
|
||||
|
||||
for (OSVR_EyeCount eye = 0; eye < eyeCount; eye++) {
|
||||
double viewMatrix[OSVR_MATRIX_SIZE];
|
||||
osvrClientGetViewerEyeViewMatrixd(
|
||||
displayConfig, viewer, eye, OSVR_MATRIX_COLMAJOR | OSVR_MATRIX_COLVECTORS, viewMatrix
|
||||
);
|
||||
|
||||
OSVR_SurfaceCount surfaceCount;
|
||||
osvrClientGetNumSurfacesForViewerEye(displayConfig, viewer, eye, &surfaceCount);
|
||||
for (OSVR_SurfaceCount surface = 0; surface < surfaceCount; surface++) {
|
||||
OSVR_ViewportDimension left, bottom, width, height;
|
||||
osvrClientGetRelativeViewportForViewerEyeSurface(
|
||||
displayConfig, viewer, eye, surface, &left, &bottom, &width, &height
|
||||
);
|
||||
|
||||
glViewport(left, bottom, width, height);
|
||||
|
||||
double projectionMatrix[OSVR_MATRIX_SIZE];
|
||||
float near = 0.1f;
|
||||
float far = 100.0f;
|
||||
osvrClientGetViewerEyeSurfaceProjectionMatrixd(
|
||||
displayConfig, viewer, eye, surface, near, far,
|
||||
OSVR_MATRIX_COLMAJOR | OSVR_MATRIX_COLVECTORS | OSVR_MATRIX_SIGNEDZ | OSVR_MATRIX_RHINPUT,
|
||||
projectionMatrix
|
||||
);
|
||||
|
||||
// Do something with viewMatrix, projectionMatrix, left, bottom, width, and height
|
||||
|
||||
callback(eye, userdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
#include "../osvr.h"
|
||||
|
||||
#ifndef LOVR_HEADSET_TYPES
|
||||
typedef void (*headsetRenderCallback)(int eyeIndex, void* userdata);
|
||||
#endif
|
||||
|
@ -8,4 +6,4 @@ void lovrHeadsetInit();
|
|||
void lovrHeadsetGetPosition(float* x, float* y, float* z);
|
||||
void lovrHeadsetGetOrientation(float* w, float* x, float* y, float* z);
|
||||
int lovrHeadsetIsPresent();
|
||||
void lovrHeadsetRenderTo(headsetRenderCallback callback, void* userdata);
|
||||
void lovrHeadsetRenderTo(headsetRenderCallback callback, void* userdata);
|
|
@ -1,180 +0,0 @@
|
|||
#include "joystick.h"
|
||||
#include "../glfw.h"
|
||||
#include "../osvr.h"
|
||||
|
||||
extern OSVR_ClientContext ctx;
|
||||
|
||||
void lovrJoystickDestroy(Joystick* joystick) {
|
||||
if (joystick->isTracked) {
|
||||
osvrClientFreeInterface(ctx, joystick->osvrTrackerInterface);
|
||||
|
||||
int i;
|
||||
OSVR_ClientInterface* interface;
|
||||
|
||||
vec_foreach(&joystick->osvrButtonInterfaces, interface, i) {
|
||||
osvrClientFreeInterface(ctx, *interface);
|
||||
free(joystick->osvrButtonInterfaces.data[i]);
|
||||
}
|
||||
|
||||
vec_foreach(&joystick->osvrAxisInterfaces, interface, i) {
|
||||
osvrClientFreeInterface(ctx, *interface);
|
||||
free(joystick->osvrAxisInterfaces.data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
free(joystick);
|
||||
}
|
||||
|
||||
void lovrJoystickGetAngularAcceleration(Joystick* joystick, float* w, float* x, float* y, float* z) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AngularAccelerationState state;
|
||||
osvrGetAngularAccelerationState(joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
*w = osvrQuatGetW(&state.incrementalRotation);
|
||||
*x = osvrQuatGetX(&state.incrementalRotation);
|
||||
*y = osvrQuatGetY(&state.incrementalRotation);
|
||||
*z = osvrQuatGetZ(&state.incrementalRotation);
|
||||
} else {
|
||||
*w = *x = *y = *z = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoystickGetAngularVelocity(Joystick* joystick, float* w, float* x, float* y, float* z) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AngularVelocityState state;
|
||||
osvrGetAngularVelocityState(joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
*w = osvrQuatGetW(&state.incrementalRotation);
|
||||
*x = osvrQuatGetX(&state.incrementalRotation);
|
||||
*y = osvrQuatGetY(&state.incrementalRotation);
|
||||
*z = osvrQuatGetZ(&state.incrementalRotation);
|
||||
} else {
|
||||
*w = *x = *y = *z = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoystickGetAxes(Joystick* joystick, vec_float_t* result) {
|
||||
if (joystick->isTracked) {
|
||||
interface_vec_t axes = joystick->osvrAxisInterfaces;
|
||||
for (int i = 0; i < axes.length; i++) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AnalogState state;
|
||||
osvrGetAnalogState(*axes.data[i], ×tamp, &state);
|
||||
vec_push(result, state);
|
||||
}
|
||||
} else {
|
||||
int count;
|
||||
const float* axes = glfwGetJoystickAxes(joystick->glfwIndex, &count);
|
||||
vec_pusharr(result, axes, count);
|
||||
}
|
||||
}
|
||||
|
||||
float lovrJoystickGetAxis(Joystick* joystick, int index) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_AnalogState state;
|
||||
OSVR_ClientInterface interface = *joystick->osvrAxisInterfaces.data[index];
|
||||
osvrGetAnalogState(interface, ×tamp, &state);
|
||||
return (float)state;
|
||||
} else {
|
||||
const float* axes = glfwGetJoystickAxes(joystick->glfwIndex, NULL);
|
||||
return axes[index];
|
||||
}
|
||||
}
|
||||
|
||||
int lovrJoystickGetAxisCount(Joystick* joystick) {
|
||||
if (joystick->isTracked) {
|
||||
return joystick->osvrAxisInterfaces.length;
|
||||
} else {
|
||||
int count;
|
||||
glfwGetJoystickAxes(joystick->glfwIndex, &count);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
int lovrJoystickGetButtonCount(Joystick* joystick) {
|
||||
if (joystick->isTracked) {
|
||||
return joystick->osvrButtonInterfaces.length;
|
||||
} else {
|
||||
int count;
|
||||
glfwGetJoystickButtons(joystick->glfwIndex, &count);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoystickGetLinearAcceleration(Joystick* joystick, float* x, float* y, float* z) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_LinearAccelerationState state;
|
||||
osvrGetLinearAccelerationState(joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
*x = osvrVec3GetX(&state);
|
||||
*y = osvrVec3GetY(&state);
|
||||
*z = osvrVec3GetZ(&state);
|
||||
} else {
|
||||
*x = *y = *z = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoystickGetLinearVelocity(Joystick* joystick, float* x, float* y, float* z) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_LinearVelocityState state;
|
||||
osvrGetLinearVelocityState(joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
*x = osvrVec3GetX(&state);
|
||||
*y = osvrVec3GetY(&state);
|
||||
*z = osvrVec3GetZ(&state);
|
||||
} else {
|
||||
*x = *y = *z = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
const char* lovrJoystickGetName(Joystick* joystick) {
|
||||
if (joystick->isTracked) {
|
||||
return "Tracked controller";
|
||||
} else {
|
||||
return glfwGetJoystickName(joystick->glfwIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoystickGetOrientation(Joystick* joystick, float* w, float* x, float* y, float* z) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_OrientationState state;
|
||||
osvrGetOrientationState(joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
*w = osvrQuatGetW(&state);
|
||||
*x = osvrQuatGetX(&state);
|
||||
*y = osvrQuatGetY(&state);
|
||||
*z = osvrQuatGetZ(&state);
|
||||
} else {
|
||||
*w = *x = *y = *z = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoystickGetPosition(Joystick* joystick, float* x, float* y, float* z) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_PositionState state;
|
||||
osvrGetPositionState(joystick->osvrTrackerInterface, ×tamp, &state);
|
||||
*x = osvrVec3GetX(&state);
|
||||
*y = osvrVec3GetY(&state);
|
||||
*z = osvrVec3GetZ(&state);
|
||||
} else {
|
||||
*x = *y = *z = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
int lovrJoystickIsDown(Joystick* joystick, int index) {
|
||||
if (joystick->isTracked) {
|
||||
OSVR_TimeValue timestamp;
|
||||
OSVR_ButtonState state;
|
||||
osvrGetButtonState(*joystick->osvrButtonInterfaces.data[index], ×tamp, &state);
|
||||
return state > 0;
|
||||
} else {
|
||||
const unsigned char* buttons = glfwGetJoystickButtons(joystick->glfwIndex, NULL);
|
||||
return buttons[index];
|
||||
}
|
||||
}
|
||||
|
||||
int lovrJoystickIsTracked(Joystick* joystick) {
|
||||
return joystick->isTracked;
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
#include "../osvr.h"
|
||||
#include "../vendor/vec/vec.h"
|
||||
|
||||
#ifndef LOVR_JOYSTICK_TYPES
|
||||
#define LOVR_JOYSTICK_TYPES
|
||||
typedef struct {
|
||||
int isTracked;
|
||||
int glfwIndex;
|
||||
OSVR_ClientInterface osvrTrackerInterface;
|
||||
interface_vec_t osvrAxisInterfaces;
|
||||
interface_vec_t osvrButtonInterfaces;
|
||||
} Joystick;
|
||||
typedef vec_t(Joystick*) joystick_vec_t;
|
||||
#endif
|
||||
|
||||
void lovrJoystickDestroy(Joystick* joystick);
|
||||
void lovrJoystickGetAngularAcceleration(Joystick* joystick, float* w, float* x, float* y, float* z);
|
||||
void lovrJoystickGetAngularVelocity(Joystick* joystick, float* w, float* x, float* y, float* z);
|
||||
void lovrJoystickGetAxes(Joystick* joystick, vec_float_t* result);
|
||||
float lovrJoystickGetAxis(Joystick* joystick, int index);
|
||||
int lovrJoystickGetAxisCount(Joystick* joystick);
|
||||
int lovrJoystickGetButtonCount(Joystick* joystick);
|
||||
void lovrJoystickGetLinearAcceleration(Joystick* joystick, float* x, float* y, float* z);
|
||||
void lovrJoystickGetLinearVelocity(Joystick* joystick, float* x, float* y, float* z);
|
||||
const char* lovrJoystickGetName(Joystick* joystick);
|
||||
void lovrJoystickGetOrientation(Joystick* joystick, float* w, float* x, float* y, float* z);
|
||||
void lovrJoystickGetPosition(Joystick* joystick, float* x, float* y, float* z);
|
||||
int lovrJoystickIsDown(Joystick* joystick, int index);
|
||||
int lovrJoystickIsTracked(Joystick* joystick);
|
|
@ -1,141 +0,0 @@
|
|||
#include "joysticks.h"
|
||||
#include "../osvr.h"
|
||||
#include "../lovr.h"
|
||||
#include "../vendor/vec/vec.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
joystick_vec_t list;
|
||||
} JoystickState;
|
||||
|
||||
static JoystickState joystickState;
|
||||
|
||||
void lovrJoysticksInit() {
|
||||
glfwSetJoystickCallback(lovrJoysticksOnJoystickChanged);
|
||||
|
||||
vec_init(&joystickState.list);
|
||||
|
||||
// GLFW joysticks
|
||||
for (int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
|
||||
if (glfwJoystickPresent(i)) {
|
||||
Joystick* joystick = malloc(sizeof(Joystick));
|
||||
joystick->isTracked = 0;
|
||||
joystick->glfwIndex = i;
|
||||
joystick->osvrTrackerInterface = NULL;
|
||||
vec_push(&joystickState.list, joystick);
|
||||
}
|
||||
}
|
||||
|
||||
// OSVR joysticks
|
||||
if (osvrClientCheckStatus(ctx) != OSVR_RETURN_FAILURE) {
|
||||
char buffer[128];
|
||||
const char* hands[2] = {
|
||||
"/me/hands/left",
|
||||
"/me/hands/right"
|
||||
};
|
||||
|
||||
Joystick* joystick = NULL;
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (joystick == NULL) {
|
||||
joystick = malloc(sizeof(Joystick));
|
||||
}
|
||||
|
||||
osvrClientGetInterface(ctx, hands[i], &joystick->osvrTrackerInterface);
|
||||
|
||||
if (joystick->osvrTrackerInterface) {
|
||||
joystick->isTracked = 1;
|
||||
joystick->glfwIndex = -1;
|
||||
vec_init(&joystick->osvrAxisInterfaces);
|
||||
vec_init(&joystick->osvrButtonInterfaces);
|
||||
|
||||
// Buttons
|
||||
const char* buttons[6] = {
|
||||
"system",
|
||||
"menu",
|
||||
"grip",
|
||||
"trackpad/touch",
|
||||
"trackpad/button",
|
||||
"trigger/button"
|
||||
};
|
||||
|
||||
OSVR_ClientInterface* buttonInterface = NULL;
|
||||
|
||||
for (int j = 0; j < 6; j++) {
|
||||
if (buttonInterface == NULL) {
|
||||
buttonInterface = malloc(sizeof(buttonInterface));
|
||||
}
|
||||
|
||||
snprintf(buffer, sizeof(buffer), "%s/%s", hands[i], buttons[j]);
|
||||
osvrClientGetInterface(ctx, buffer, buttonInterface);
|
||||
|
||||
if (buttonInterface) {
|
||||
vec_push(&joystick->osvrButtonInterfaces, buttonInterface);
|
||||
buttonInterface = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
free(buttonInterface);
|
||||
|
||||
// Axes
|
||||
const char* axes[3] = {
|
||||
"trackpad/x",
|
||||
"trackpad/y",
|
||||
"trigger"
|
||||
};
|
||||
|
||||
OSVR_ClientInterface* axisInterface = NULL;
|
||||
|
||||
for (int j = 0; j < 3; j++) {
|
||||
if (axisInterface == NULL) {
|
||||
axisInterface = malloc(sizeof(axisInterface));
|
||||
}
|
||||
|
||||
snprintf(buffer, sizeof(buffer), "%s/%s", hands[i], axes[j]);
|
||||
osvrClientGetInterface(ctx, buffer, axisInterface);
|
||||
|
||||
if (axisInterface) {
|
||||
vec_push(&joystick->osvrAxisInterfaces, axisInterface);
|
||||
axisInterface = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
free(axisInterface);
|
||||
|
||||
joystick = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
free(joystick);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrJoysticksOnJoystickChanged(int index, int event) {
|
||||
if (event == GLFW_CONNECTED) {
|
||||
Joystick* joystick = malloc(sizeof(Joystick));
|
||||
joystick->isTracked = 0;
|
||||
joystick->glfwIndex = index;
|
||||
joystick->osvrTrackerInterface = NULL;
|
||||
vec_push(&joystickState.list, joystick);
|
||||
lovrOnJoystickAdded(joystick);
|
||||
} else if (event == GLFW_DISCONNECTED) {
|
||||
Joystick* joystick;
|
||||
int i;
|
||||
vec_foreach(&joystickState.list, joystick, i) {
|
||||
if (joystick->glfwIndex == index) {
|
||||
lovrOnJoystickRemoved(joystick);
|
||||
lovrJoystickDestroy(joystick);
|
||||
vec_splice(&joystickState.list, i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int lovrJoysticksGetJoystickCount() {
|
||||
return joystickState.list.length;
|
||||
}
|
||||
|
||||
void lovrJoysticksGetJoysticks(joystick_vec_t* joysticks) {
|
||||
vec_pusharr(joysticks, joystickState.list.data, joystickState.list.length);
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
#include "joystick.h"
|
||||
#include "../glfw.h"
|
||||
#include "../vendor/vec/vec.h"
|
||||
|
||||
void lovrJoysticksInit();
|
||||
void lovrJoysticksOnJoystickChanged();
|
||||
int lovrJoysticksGetJoystickCount();
|
||||
void lovrJoysticksGetJoysticks(joystick_vec_t* joysticks);
|
27
src/lovr.c
27
src/lovr.c
|
@ -1,11 +1,10 @@
|
|||
#include "lovr.h"
|
||||
#include "util.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lovr/event.h"
|
||||
#include "lovr/graphics.h"
|
||||
#include "lovr/headset.h"
|
||||
#include "lovr/joysticks.h"
|
||||
#include "lovr/joystick.h"
|
||||
#include "lovr/timer.h"
|
||||
|
||||
extern lua_State* L;
|
||||
|
@ -20,7 +19,6 @@ void lovrInit(lua_State* L) {
|
|||
luaPreloadModule(L, "lovr.event", l_lovrEventInit);
|
||||
luaPreloadModule(L, "lovr.graphics", l_lovrGraphicsInit);
|
||||
luaPreloadModule(L, "lovr.headset", l_lovrHeadsetInit);
|
||||
luaPreloadModule(L, "lovr.joystick", l_lovrJoysticksInit);
|
||||
luaPreloadModule(L, "lovr.timer", l_lovrTimerInit);
|
||||
|
||||
// Bootstrap
|
||||
|
@ -31,7 +29,6 @@ void lovrInit(lua_State* L) {
|
|||
" event = true, "
|
||||
" graphics = true, "
|
||||
" headset = true, "
|
||||
" joystick = true, "
|
||||
" timer = true "
|
||||
" } "
|
||||
"} "
|
||||
|
@ -45,7 +42,7 @@ void lovrInit(lua_State* L) {
|
|||
" print('Could not run conf.lua') "
|
||||
"end "
|
||||
|
||||
"local modules = { 'event', 'graphics', 'headset', 'joystick', 'timer' } "
|
||||
"local modules = { 'event', 'graphics', 'headset', 'timer' } "
|
||||
"for _, module in ipairs(modules) do "
|
||||
" if conf.modules[module] then "
|
||||
" lovr[module] = require('lovr.' .. module) "
|
||||
|
@ -137,23 +134,3 @@ void lovrOnClose(GLFWwindow* _window) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lovrOnJoystickAdded(Joystick* joystick) {
|
||||
lua_getglobal(L, "lovr");
|
||||
lua_getfield(L, -1, "joystickadded");
|
||||
|
||||
if (lua_isfunction(L, -1)) {
|
||||
luax_pushjoystick(L, joystick);
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrOnJoystickRemoved(Joystick* joystick) {
|
||||
lua_getglobal(L, "lovr");
|
||||
lua_getfield(L, -1, "joystickremoved");
|
||||
|
||||
if (lua_isfunction(L, -1)) {
|
||||
luax_pushjoystick(L, joystick);
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "glfw.h"
|
||||
#include "joystick/joystick.h"
|
||||
|
||||
void lovrInit(lua_State* L);
|
||||
void lovrDestroy();
|
||||
|
@ -10,5 +9,3 @@ void lovrRun(lua_State* L, char* mainPath);
|
|||
int lovrOnLuaError(lua_State* L);
|
||||
void lovrOnGlfwError(int code, const char* description);
|
||||
void lovrOnClose(GLFWwindow* window);
|
||||
void lovrOnJoystickAdded(Joystick* joystick);
|
||||
void lovrOnJoystickRemoved(Joystick* joystick);
|
||||
|
|
|
@ -1,153 +0,0 @@
|
|||
#include "joystick.h"
|
||||
#include "../joystick/joystick.h"
|
||||
|
||||
void luax_pushjoystick(lua_State* L, Joystick* joystick) {
|
||||
Joystick** userdata = (Joystick**) lua_newuserdata(L, sizeof(Joystick*));
|
||||
luaL_getmetatable(L, "Joystick");
|
||||
lua_setmetatable(L, -2);
|
||||
*userdata = joystick;
|
||||
}
|
||||
|
||||
Joystick* luax_checkjoystick(lua_State* L, int index) {
|
||||
return *(Joystick**) luaL_checkudata(L, index, "Joystick");
|
||||
}
|
||||
|
||||
int luax_destroyjoystick(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lovrJoystickDestroy(joystick);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrJoystick[] = {
|
||||
{ "getAngularAcceleration", l_lovrJoystickGetAngularAcceleration },
|
||||
{ "getAngularVelocity", l_lovrJoystickGetAngularVelocity },
|
||||
{ "getAxes", l_lovrJoystickGetAxes },
|
||||
{ "getAxis", l_lovrJoystickGetAxis },
|
||||
{ "getAxisCount", l_lovrJoystickGetAxisCount },
|
||||
{ "getButtonCount", l_lovrJoystickGetButtonCount },
|
||||
{ "getLinearAcceleration", l_lovrJoystickGetLinearAcceleration },
|
||||
{ "getLinearVelocity", l_lovrJoystickGetLinearVelocity },
|
||||
{ "getName", l_lovrJoystickGetName },
|
||||
{ "getOrientation", l_lovrJoystickGetOrientation },
|
||||
{ "getPosition", l_lovrJoystickGetPosition },
|
||||
{ "isDown", l_lovrJoystickIsDown },
|
||||
{ "isTracked", l_lovrJoystickIsTracked },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int l_lovrJoystickGetAngularAcceleration(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
float w, x, y, z;
|
||||
lovrJoystickGetAngularAcceleration(joystick, &w, &x, &y, &z);
|
||||
lua_pushnumber(L, w);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
lua_pushnumber(L, z);
|
||||
return 4;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetAngularVelocity(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
float w, x, y, z;
|
||||
lovrJoystickGetAngularVelocity(joystick, &w, &x, &y, &z);
|
||||
lua_pushnumber(L, w);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
lua_pushnumber(L, z);
|
||||
return 4;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetAxes(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_newtable(L);
|
||||
vec_float_t axes;
|
||||
vec_init(&axes);
|
||||
lovrJoystickGetAxes(joystick, &axes);
|
||||
float axis;
|
||||
int i;
|
||||
vec_foreach(&axes, axis, i) {
|
||||
lua_pushnumber(L, axis);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
vec_deinit(&axes);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetAxis(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
int axis = luaL_checknumber(L, 2);
|
||||
lua_pushnumber(L, lovrJoystickGetAxis(joystick, axis));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetAxisCount(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushnumber(L, lovrJoystickGetAxisCount(joystick));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetButtonCount(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushnumber(L, lovrJoystickGetButtonCount(joystick));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetLinearAcceleration(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
float x, y, z;
|
||||
lovrJoystickGetLinearAcceleration(joystick, &x, &y, &z);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
lua_pushnumber(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetLinearVelocity(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
float x, y, z;
|
||||
lovrJoystickGetLinearVelocity(joystick, &x, &y, &z);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
lua_pushnumber(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetName(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushstring(L, lovrJoystickGetName(joystick));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetOrientation(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
float w, x, y, z;
|
||||
lovrJoystickGetOrientation(joystick, &w, &x, &y, &z);
|
||||
lua_pushnumber(L, w);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
lua_pushnumber(L, z);
|
||||
return 4;
|
||||
}
|
||||
|
||||
int l_lovrJoystickGetPosition(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
float x, y, z;
|
||||
lovrJoystickGetPosition(joystick, &x, &y, &z);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
lua_pushnumber(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int l_lovrJoystickIsDown(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
int button = luaL_checkint(L, 2);
|
||||
lua_pushinteger(L, lovrJoystickIsDown(joystick, button));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoystickIsTracked(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushinteger(L, lovrJoystickIsTracked(joystick));
|
||||
return 1;
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "../joystick/joystick.h"
|
||||
|
||||
void luax_pushjoystick(lua_State* L, Joystick* joystick);
|
||||
Joystick* luax_checkjoystick(lua_State* L, int index);
|
||||
int luax_destroyjoystick(lua_State* L);
|
||||
extern const luaL_Reg lovrJoystick[];
|
||||
|
||||
int l_lovrJoystickGetAngularAcceleration(lua_State* L);
|
||||
int l_lovrJoystickGetAngularVelocity(lua_State* L);
|
||||
int l_lovrJoystickGetAxes(lua_State* L);
|
||||
int l_lovrJoystickGetAxis(lua_State* L);
|
||||
int l_lovrJoystickGetAxisCount(lua_State* L);
|
||||
int l_lovrJoystickGetButtonCount(lua_State* L);
|
||||
int l_lovrJoystickGetLinearAcceleration(lua_State* L);
|
||||
int l_lovrJoystickGetLinearVelocity(lua_State* L);
|
||||
int l_lovrJoystickGetName(lua_State* L);
|
||||
int l_lovrJoystickGetOrientation(lua_State* L);
|
||||
int l_lovrJoystickGetPosition(lua_State* L);
|
||||
int l_lovrJoystickIsDown(lua_State* L);
|
||||
int l_lovrJoystickIsTracked(lua_State* L);
|
|
@ -1,38 +0,0 @@
|
|||
#include "joysticks.h"
|
||||
#include "joystick.h"
|
||||
#include "../util.h"
|
||||
#include "../joystick/joysticks.h"
|
||||
|
||||
const luaL_Reg lovrJoysticks[] = {
|
||||
{ "getJoystickCount", l_lovrJoysticksGetJoystickCount },
|
||||
{ "getJoysticks", l_lovrJoysticksGetJoysticks },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int l_lovrJoysticksInit(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrJoysticks);
|
||||
luaRegisterType(L, "Joystick", lovrJoystick, luax_destroyjoystick);
|
||||
lovrJoysticksInit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoysticksGetJoystickCount(lua_State* L) {
|
||||
lua_pushnumber(L, lovrJoysticksGetJoystickCount());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrJoysticksGetJoysticks(lua_State* L) {
|
||||
joystick_vec_t joysticks;
|
||||
vec_init(&joysticks);
|
||||
lovrJoysticksGetJoysticks(&joysticks);
|
||||
lua_newtable(L);
|
||||
Joystick* joystick;
|
||||
int i;
|
||||
vec_foreach(&joysticks, joystick, i) {
|
||||
luax_pushjoystick(L, joystick);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
vec_deinit(&joysticks);
|
||||
return 1;
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
extern const luaL_Reg lovrJoysticks[];
|
||||
int l_lovrJoysticksInit(lua_State* L);
|
||||
int l_lovrJoysticksGetJoystickCount(lua_State* L);
|
||||
int l_lovrJoysticksGetJoysticks(lua_State* L);
|
|
@ -1,5 +0,0 @@
|
|||
#include "osvr.h"
|
||||
|
||||
void initOsvr() {
|
||||
ctx = osvrClientInit("es.bjornbyt", 0);
|
||||
}
|
14
src/osvr.h
14
src/osvr.h
|
@ -1,14 +0,0 @@
|
|||
#include <osvr/ClientKit/ContextC.h>
|
||||
#include <osvr/ClientKit/DisplayC.h>
|
||||
#include <osvr/ClientKit/InterfaceC.h>
|
||||
#include <osvr/ClientKit/InterfaceStateC.h>
|
||||
#include "vendor/vec/vec.h"
|
||||
|
||||
#ifndef LOVR_OSVR_TYPES
|
||||
#define LOVR_OSVR_TYPES
|
||||
typedef vec_t(OSVR_ClientInterface*) interface_vec_t;
|
||||
#endif
|
||||
|
||||
OSVR_ClientContext ctx;
|
||||
|
||||
void initOsvr();
|
Loading…
Reference in New Issue