move grabbing logic to component

This commit is contained in:
Crispy 2023-04-11 19:26:26 +02:00
parent 41f01e29a6
commit 809edb810e
6 changed files with 129 additions and 97 deletions

View file

@ -1,13 +1,11 @@
#include "app.h" #include "app.h"
#include "panel.h" #include "panel.h"
#include "util.h"
#include <X11/extensions/Xrandr.h> #include <X11/extensions/Xrandr.h>
#include <cassert> #include <cassert>
App::App() App::App()
{ {
_tracking_origin = vr::TrackingUniverseStanding; _tracking_origin = vr::TrackingUniverseStanding;
// _hidden = false;
InitOVR(); InitOVR();
InitX11(); InitX11();
@ -24,6 +22,7 @@ App::App()
float pixels_per_meter = 1920; float pixels_per_meter = 1920;
float x_min = -(monitor_info[0].x + monitor_info[0].width / 2.0f); float x_min = -(monitor_info[0].x + monitor_info[0].width / 2.0f);
// float x_min = _root_width / -2.0f;
for (int i = 0; i < monitor_count; i++) for (int i = 0; i < monitor_count; i++)
{ {

View file

@ -1,4 +1,6 @@
#pragma once
#define GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES
#include "util.h" #include "util.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>

94
src/grab_component.cpp Normal file
View file

@ -0,0 +1,94 @@
#include "grab_component.h"
GrabComponent::GrabComponent(App *app)
{
_app = app;
_is_held = false;
_active_hand = 0;
}
void GrabComponent::Update(OverlayID id, float *meters)
{
_id = id;
if (!_is_held)
{
for (auto controller : _app->GetControllers())
{
if (_app->IsInputJustPressed(controller, _app->_input_handles.grab))
{
vr::HmdMatrix34_t overlay_pose;
vr::ETrackingUniverseOrigin tracking_universe;
_app->vr_overlay->GetOverlayTransformAbsolute(id, &tracking_universe, &overlay_pose);
auto controller_pos = GetPos(_app->GetTrackerPose(controller));
auto local_pos = glm::inverse(ConvertMat(overlay_pose)) * glm::vec4(controller_pos - GetPos(overlay_pose), 0);
float grab_area_thickness = 0.3f;
bool close_enough = glm::abs(local_pos.z) < grab_area_thickness;
close_enough &= glm::abs(local_pos.x) < *meters / 2.0f;
close_enough &= glm::abs(local_pos.y) < *meters / 2.0f;
if (close_enough)
{
ControllerGrab(controller);
}
}
}
}
else
{
if (!_app->GetControllerInputDigital(_active_hand, _app->_input_handles.grab).bState)
{
ControllerRelease();
}
}
}
bool GrabComponent::IsHeld()
{
return _is_held;
}
TrackerID GrabComponent::ActiveHand()
{
return _active_hand;
}
void GrabComponent::ControllerGrab(TrackerID controller)
{
_is_held = true;
_active_hand = controller;
_app->vr_overlay->SetOverlayColor(_id, 0.6f, 0.8f, 0.8f);
vr::HmdMatrix34_t abs_pose;
vr::ETrackingUniverseOrigin tracking_universe;
_app->vr_overlay->GetOverlayTransformAbsolute(_id, &tracking_universe, &abs_pose);
auto abs_mat = ConvertMat(abs_pose);
auto controller_mat = _app->GetTrackerPose(controller);
vr::HmdMatrix34_t relative_pose = ConvertMat(glm::inverse(controller_mat) * (abs_mat));
_app->vr_overlay->SetOverlayTransformTrackedDeviceRelative(_id, controller, &relative_pose);
}
void GrabComponent::ControllerRelease()
{
_is_held = false;
_active_hand = -1;
_app->vr_overlay->SetOverlayColor(_id, 1.0f, 1.0f, 1.0f);
vr::HmdMatrix34_t relative_pose;
_app->vr_overlay->GetOverlayTransformTrackedDeviceRelative(_id, &_active_hand, &relative_pose);
auto relative_mat = ConvertMat(relative_pose);
auto controller_mat = _app->GetTrackerPose(_active_hand);
vr::HmdMatrix34_t new_pose = ConvertMat(controller_mat * relative_mat);
_app->vr_overlay->SetOverlayTransformAbsolute(_id, _app->_tracking_origin, &new_pose);
}

23
src/grab_component.h Normal file
View file

@ -0,0 +1,23 @@
#pragma once
#include "app.h"
#include "util.h"
class GrabComponent
{
public:
GrabComponent(App *app);
bool IsHeld();
TrackerID ActiveHand();
void Update(OverlayID id, float *meters);
private:
void ControllerRelease();
void ControllerGrab(TrackerID controller);
App *_app;
OverlayID _id;
bool _is_held;
TrackerID _active_hand;
};

View file

@ -1,10 +1,4 @@
#include "panel.h" #include "panel.h"
#include "app.h"
#include "util.h"
#include <X11/Xutil.h>
#include <glm/common.hpp>
#include <glm/glm.hpp>
Panel::Panel(App *app, vr::HmdMatrix34_t start_pose, int index, int x, int y, int width, int height) Panel::Panel(App *app, vr::HmdMatrix34_t start_pose, int index, int x, int y, int width, int height)
: _app(app), : _app(app),
@ -12,12 +6,13 @@ Panel::Panel(App *app, vr::HmdMatrix34_t start_pose, int index, int x, int y, in
_x(x), _x(x),
_y(y), _y(y),
_width(width), _width(width),
_height(height) _height(height),
_grab_component(app)
{ {
_name = "screen_view_" + std::to_string(index); _name = "screen_view_" + std::to_string(index);
_alpha = 1.0f; _alpha = 1.0f;
_meters = 1.0f; _meters = 1.0f;
_active_hand = -1;
glGenTextures(1, &_gl_texture); glGenTextures(1, &_gl_texture);
glBindTexture(GL_TEXTURE_2D, _gl_texture); glBindTexture(GL_TEXTURE_2D, _gl_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@ -51,48 +46,7 @@ void Panel::Update()
Render(); Render();
UpdateCursor(); UpdateCursor();
if (!_is_held) _grab_component.Update(_id, &_meters);
{
for (auto controller : _app->GetControllers())
{
if (_app->IsInputJustPressed(controller, _app->_input_handles.grab))
{
vr::HmdMatrix34_t overlay_pose;
vr::ETrackingUniverseOrigin tracking_universe;
_app->vr_overlay->GetOverlayTransformAbsolute(_id, &tracking_universe, &overlay_pose);
auto controller_pos = GetPos(_app->GetTrackerPose(controller));
auto local_pos = glm::inverse(ConvertMat(overlay_pose)) * glm::vec4(controller_pos - GetPos(overlay_pose), 0);
float grab_area_thickness = 0.3f;
bool close_enough = glm::abs(local_pos.z) < grab_area_thickness;
close_enough &= glm::abs(local_pos.x) < _meters / 2.0f;
close_enough &= glm::abs(local_pos.y) < _meters / 2.0f;
if (close_enough)
{
ControllerGrab(controller);
}
}
}
}
else
{
if (!_app->GetControllerInputDigital(_active_hand, _app->_input_handles.grab).bState)
{
ControllerRelease();
}
// auto state = _app->GetControllerState(_active_hand);
// auto touchpad = state.rAxis[0];
// if (touchpad.x != 0.0f)
// {
// // TODO take into account the current framerate
// _alpha += touchpad.x * 0.05;
// _alpha = glm::clamp(_alpha, 0.1f, 1.0f);
// _app->vr_overlay->SetOverlayAlpha(_id, _alpha);
// }
}
} }
void Panel::Render() void Panel::Render()
@ -136,42 +90,3 @@ void Panel::UpdateCursor()
auto pos = vr::HmdVector2_t{x, y}; auto pos = vr::HmdVector2_t{x, y};
_app->vr_overlay->SetOverlayCursorPositionOverride(_id, &pos); _app->vr_overlay->SetOverlayCursorPositionOverride(_id, &pos);
} }
void Panel::ControllerGrab(TrackerID controller)
{
// printf("Grabbed panel %d\n", _index);
_is_held = true;
_active_hand = controller;
_app->vr_overlay->SetOverlayColor(_id, 0.6f, 1.0f, 1.0f);
vr::HmdMatrix34_t abs_pose;
vr::ETrackingUniverseOrigin tracking_universe;
_app->vr_overlay->GetOverlayTransformAbsolute(_id, &tracking_universe, &abs_pose);
auto abs_mat = ConvertMat(abs_pose);
auto controller_mat = _app->GetTrackerPose(controller);
vr::HmdMatrix34_t relative_pose = ConvertMat(glm::inverse(controller_mat) * (abs_mat));
_app->vr_overlay->SetOverlayTransformTrackedDeviceRelative(_id, controller, &relative_pose);
}
void Panel::ControllerRelease()
{
// printf("Released panel %d\n", _index);
_is_held = false;
_app->vr_overlay->SetOverlayColor(_id, 1.0f, 1.0f, 1.0f);
vr::HmdMatrix34_t relative_pose;
_app->vr_overlay->GetOverlayTransformTrackedDeviceRelative(_id, &_active_hand, &relative_pose);
auto relative_mat = ConvertMat(relative_pose);
auto controller_mat = _app->GetTrackerPose(_active_hand);
vr::HmdMatrix34_t new_pose = ConvertMat(controller_mat * relative_mat);
_app->vr_overlay->SetOverlayTransformAbsolute(_id, _app->_tracking_origin, &new_pose);
}

View file

@ -1,7 +1,9 @@
#pragma once
#define GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES
#include "grab_component.h"
#include "util.h" #include "util.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#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}}};
@ -18,23 +20,20 @@ class Panel
private: private:
void Render(); void Render();
void UpdateCursor(); void UpdateCursor();
void ControllerGrab(TrackerID);
void ControllerRelease();
App *_app; App *_app;
OverlayID _id; OverlayID _id;
int _index; int _index;
std::string _name; std::string _name;
TrackerID _active_hand;
bool _is_held;
int _x, _y; int _x, _y;
int _width, _height; int _width, _height;
float _meters; float _meters;
float _alpha; float _alpha;
bool _hidden; bool _hidden;
GrabComponent _grab_component;
vr::Texture_t _texture; vr::Texture_t _texture;
GLuint _gl_texture; GLuint _gl_texture;
}; };