This commit is contained in:
Crispy 2023-04-07 13:26:42 +02:00
parent e4a79eefd3
commit 74d2c71c53
4 changed files with 24 additions and 25 deletions

View file

@ -5,7 +5,7 @@
App::App() App::App()
{ {
_tracking_origin = vr::ETrackingUniverseOrigin::TrackingUniverseStanding; _tracking_origin = vr::TrackingUniverseStanding;
InitOVR(); InitOVR();
InitX11(); InitX11();
@ -36,8 +36,9 @@ void App::InitX11()
void App::InitOVR() void App::InitOVR()
{ {
vr::EVRInitError init_err; vr::EVRInitError init_err;
vr_sys = vr::VR_Init(&init_err, vr::EVRApplicationType::VRApplication_Background); // would normally be using VRApplication_Overlay, but Background allows it to quit if steamvr is not running, instead of opening steamvr.
if (init_err == vr::EVRInitError::VRInitError_Init_NoServerForBackgroundApp) vr_sys = vr::VR_Init(&init_err, vr::VRApplication_Background);
if (init_err == vr::VRInitError_Init_NoServerForBackgroundApp)
{ {
printf("SteamVR is not running\n"); printf("SteamVR is not running\n");
exit(1); exit(1);
@ -55,7 +56,7 @@ void App::InitGLFW()
{ {
assert(glfwInit() == true); assert(glfwInit() == true);
glfwWindowHint(GLFW_VISIBLE, false); glfwWindowHint(GLFW_VISIBLE, false);
// TODO this is creating a 1x1 window, should it be bigger? // creating a 1x1 window, since it is hidden anyway
_gl_window = glfwCreateWindow(1, 1, "Overlay", nullptr, nullptr); _gl_window = glfwCreateWindow(1, 1, "Overlay", nullptr, nullptr);
assert(_gl_window != nullptr); assert(_gl_window != nullptr);
glfwMakeContextCurrent(_gl_window); glfwMakeContextCurrent(_gl_window);
@ -70,6 +71,14 @@ void App::Update()
} }
} }
std::vector<TrackerID> App::GetControllers()
{
static const auto max_len = 64;
TrackerID controllers[max_len];
int controller_count = vr_sys->GetSortedTrackedDeviceIndicesOfClass(vr::TrackedDeviceClass_Controller, controllers, max_len);
return std::vector<TrackerID>(controllers, controllers + controller_count);
}
glm::mat4 App::GetTrackerPose(TrackerID tracker) glm::mat4 App::GetTrackerPose(TrackerID tracker)
{ {
vr::VRControllerState_t state; vr::VRControllerState_t state;
@ -83,19 +92,15 @@ glm::mat4 App::GetTrackerPose(TrackerID tracker)
return ConvertMat(tracked_pose.mDeviceToAbsoluteTracking); return ConvertMat(tracked_pose.mDeviceToAbsoluteTracking);
} }
bool App::IsGrabActive(vr::TrackedDeviceIndex_t controller) bool App::IsGrabActive(TrackerID controller)
{ {
vr::VRControllerState_t state; vr::VRControllerState_t state;
auto get_state_err = vr_sys->GetControllerState(controller, &state, sizeof(vr::VRControllerState_t)); auto get_state_err = vr_sys->GetControllerState(controller, &state, sizeof(vr::VRControllerState_t));
if (get_state_err == false) if (get_state_err == false)
{
printf("Error getting controller state: %d\n", get_state_err);
return false; return false;
}
// printf("got state\n");
auto trigger_mask = vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_SteamVR_Trigger); auto trigger_mask = vr::ButtonMaskFromId(vr::k_EButton_SteamVR_Trigger);
auto b_mask = vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_IndexController_B); auto b_mask = vr::ButtonMaskFromId(vr::k_EButton_IndexController_B);
auto mask = trigger_mask | b_mask; auto mask = trigger_mask | b_mask;
return (state.ulButtonPressed & mask) == mask; return (state.ulButtonPressed & mask) == mask;
} }

View file

@ -17,8 +17,9 @@ class App
~App(); ~App();
void Update(); void Update();
std::vector<TrackerID> GetControllers();
glm::mat4 GetTrackerPose(TrackerID tracker); glm::mat4 GetTrackerPose(TrackerID tracker);
bool IsGrabActive(vr::TrackedDeviceIndex_t controller); bool IsGrabActive(TrackerID controller);
CursorPos GetCursorPosition(); CursorPos GetCursorPosition();
Display *_xdisplay; Display *_xdisplay;

View file

@ -20,8 +20,8 @@ Panel::Panel(App *app, int index, int x, int y, int width, int height)
glGenTextures(1, &_gl_texture); glGenTextures(1, &_gl_texture);
glBindTexture(GL_TEXTURE_2D, _gl_texture); glBindTexture(GL_TEXTURE_2D, _gl_texture);
_texture.eColorSpace = vr::EColorSpace::ColorSpace_Auto; _texture.eColorSpace = vr::ColorSpace_Auto;
_texture.eType = vr::ETextureType::TextureType_OpenGL; _texture.eType = vr::TextureType_OpenGL;
_texture.handle = (void *)(uintptr_t)_gl_texture; _texture.handle = (void *)(uintptr_t)_gl_texture;
// create overlay // create overlay
@ -37,7 +37,6 @@ Panel::Panel(App *app, int index, int x, int y, int width, int height)
// (flipping uv on y axis because opengl and xorg are opposite) // (flipping uv on y axis because opengl and xorg are opposite)
vr::VRTextureBounds_t bounds{0, 1, 1, 0}; vr::VRTextureBounds_t bounds{0, 1, 1, 0};
_app->vr_overlay->SetOverlayTextureBounds(_id, &bounds); _app->vr_overlay->SetOverlayTextureBounds(_id, &bounds);
_app->vr_overlay->SetOverlayTransformAbsolute(_id, _app->_tracking_origin, &DEFAULT_POSE); _app->vr_overlay->SetOverlayTransformAbsolute(_id, _app->_tracking_origin, &DEFAULT_POSE);
} }
} }
@ -49,23 +48,16 @@ void Panel::Update()
if (!_is_held) if (!_is_held)
{ {
vr::TrackedDeviceIndex_t controllers[8]; for (auto controller : _app->GetControllers())
auto controller_count = _app->vr_sys->GetSortedTrackedDeviceIndicesOfClass(vr::ETrackedDeviceClass::TrackedDeviceClass_Controller, controllers, 8);
for (unsigned int i = 0; i < controller_count; i++)
{ {
auto controller = controllers[i];
vr::HmdMatrix34_t overlay_pose; vr::HmdMatrix34_t overlay_pose;
vr::ETrackingUniverseOrigin tracking_universe; vr::ETrackingUniverseOrigin tracking_universe;
_app->vr_overlay->GetOverlayTransformAbsolute(_id, &tracking_universe, &overlay_pose); _app->vr_overlay->GetOverlayTransformAbsolute(_id, &tracking_universe, &overlay_pose);
auto controller_pose = _app->GetTrackerPose(controller); auto controller_pos = GetPos(_app->GetTrackerPose(controller));
auto controller_pos = glm::vec3(controller_pose[3]); auto overlay_pos = GetPos(ConvertMat(overlay_pose));
auto overlay_pos = glm::vec3(ConvertMat(overlay_pose)[3]);
bool close_enough = glm::length(overlay_pos - controller_pos) < 1.0f; bool close_enough = glm::length(overlay_pos - controller_pos) < 1.0f;
// close_enough = true;
if (close_enough && _app->IsGrabActive(controller)) if (close_enough && _app->IsGrabActive(controller))
{ {

View file

@ -3,6 +3,7 @@
#include <string> #include <string>
const vr::HmdMatrix34_t DEFAULT_POSE = {{{1, 0, 0, 0}, {0, 1, 0, 1}, {0, 0, 1, 0}}}; const vr::HmdMatrix34_t DEFAULT_POSE = {{{1, 0, 0, 0}, {0, 1, 0, 1}, {0, 0, 1, 0}}};
class App; class App;
class Panel class Panel