add scrolling

This commit is contained in:
Crispy 2023-04-29 19:38:36 +02:00
parent 858da78820
commit 035d9e7296
8 changed files with 95 additions and 30 deletions

View file

@ -1,4 +1,4 @@
VERSION=v0.2.0 VERSION=v0.2.1
# CC := g++ # CC := g++
CC := clang++ CC := clang++
LFLAGS := -lX11 -lXrandr -lXtst -lglfw -lGL LFLAGS := -lX11 -lXrandr -lXtst -lglfw -lGL

View file

@ -45,6 +45,16 @@
"name": "/actions/cursor/in/mouse_right", "name": "/actions/cursor/in/mouse_right",
"requirement": "suggested", "requirement": "suggested",
"type": "boolean" "type": "boolean"
},
{
"name": "/actions/cursor/in/mouse_middle",
"requirement": "optional",
"type": "boolean"
},
{
"name": "/actions/cursor/in/scroll",
"requirement": "suggested",
"type": "vector2"
} }
], ],
"action_sets": [ "action_sets": [
@ -70,8 +80,10 @@
"/actions/edit/in/grab": "grab panel", "/actions/edit/in/grab": "grab panel",
"/actions/edit/in/distance": "push/pull overlay", "/actions/edit/in/distance": "push/pull overlay",
"/actions/cursor/in/activate_cursor": "activate cursor", "/actions/cursor/in/activate_cursor": "activate cursor",
"/actions/cursor/in/mouse_left": "mouse left", "/actions/cursor/in/mouse_left": "left mouse button",
"/actions/cursor/in/mouse_right": "mouse right" "/actions/cursor/in/mouse_right": "right mouse button",
"/actions/cursor/in/mouse_middle": "middle mouse button",
"/actions/cursor/in/scroll": "scroll"
} }
] ]
} }

View file

@ -195,6 +195,24 @@
}, },
"mode" : "button", "mode" : "button",
"path" : "/user/hand/right/input/trackpad" "path" : "/user/hand/right/input/trackpad"
},
{
"inputs" : {
"position" : {
"output" : "/actions/cursor/in/scroll"
}
},
"mode" : "joystick",
"path" : "/user/hand/left/input/thumbstick"
},
{
"inputs" : {
"position" : {
"output" : "/actions/cursor/in/scroll"
}
},
"mode" : "joystick",
"path" : "/user/hand/right/input/thumbstick"
} }
] ]
}, },

View file

@ -9,6 +9,8 @@
const VRMat root_start_pose = {{{1, 0, 0, 0}, {0, 1, 0, 0.8f}, {0, 0, 1, 0}}}; // 0.8m above origin const VRMat root_start_pose = {{{1, 0, 0, 0}, {0, 1, 0, 0.8f}, {0, 0, 1, 0}}}; // 0.8m above origin
const int FRAME_INTERVAL = 4; // number of update loops until the frame buffer is updated
App::App() App::App()
{ {
_tracking_origin = vr::TrackingUniverseStanding; _tracking_origin = vr::TrackingUniverseStanding;
@ -69,6 +71,10 @@ App::App()
assert(action_err == 0); assert(action_err == 0);
action_err = vr_input->GetActionHandle("/actions/cursor/in/mouse_right", &_input_handles.cursor.mouse_right); action_err = vr_input->GetActionHandle("/actions/cursor/in/mouse_right", &_input_handles.cursor.mouse_right);
assert(action_err == 0); assert(action_err == 0);
action_err = vr_input->GetActionHandle("/actions/cursor/in/mouse_middle", &_input_handles.cursor.mouse_middle);
assert(action_err == 0);
action_err = vr_input->GetActionHandle("/actions/cursor/in/scroll", &_input_handles.cursor.scroll);
assert(action_err == 0);
action_err = vr_input->GetActionSetHandle("/actions/main", &_input_handles.main_set); action_err = vr_input->GetActionSetHandle("/actions/main", &_input_handles.main_set);
assert(action_err == 0); assert(action_err == 0);
action_err = vr_input->GetActionSetHandle("/actions/edit", &_input_handles.edit_set); action_err = vr_input->GetActionSetHandle("/actions/edit", &_input_handles.edit_set);
@ -138,9 +144,9 @@ void App::InitRootOverlay()
_root_overlay.SetTextureToColor(110, 30, 190); _root_overlay.SetTextureToColor(110, 30, 190);
} }
void App::Update() void App::Update(float dtime)
{ {
UpdateInput(); UpdateInput(dtime);
if (!_hidden) if (!_hidden)
{ {
_root_overlay.Update(); _root_overlay.Update();
@ -153,7 +159,7 @@ void App::Update()
_frames_since_framebuffer += 1; _frames_since_framebuffer += 1;
} }
void App::UpdateInput() void App::UpdateInput(float dtime)
{ {
vr::VRActiveActionSet_t active_sets[2]; vr::VRActiveActionSet_t active_sets[2];
active_sets[0].ulActionSet = _input_handles.main_set; active_sets[0].ulActionSet = _input_handles.main_set;
@ -206,8 +212,8 @@ void App::UpdateInput()
} }
} }
} }
_controllers[0]->Update(); _controllers[0]->Update(dtime);
_controllers[1]->Update(); _controllers[1]->Update(dtime);
} }
void App::UpdateUIVisibility() void App::UpdateUIVisibility()
@ -218,7 +224,7 @@ void App::UpdateUIVisibility()
void App::UpdateFramebuffer() void App::UpdateFramebuffer()
{ {
if (_frames_since_framebuffer < 2) if (_frames_since_framebuffer < FRAME_INTERVAL)
{ {
return; return;
} }

View file

@ -30,6 +30,8 @@ struct InputHandles
vr::VRActionHandle_t activate; vr::VRActionHandle_t activate;
vr::VRActionHandle_t mouse_left; vr::VRActionHandle_t mouse_left;
vr::VRActionHandle_t mouse_right; vr::VRActionHandle_t mouse_right;
vr::VRActionHandle_t mouse_middle;
vr::VRActionHandle_t scroll;
} cursor; } cursor;
vr::VRActionSetHandle_t cursor_set; vr::VRActionSetHandle_t cursor_set;
struct struct
@ -45,7 +47,7 @@ class App
public: public:
App(); App();
~App(); ~App();
void Update(); void Update(float dtime);
std::vector<TrackerID> GetControllers(); std::vector<TrackerID> GetControllers();
glm::mat4 GetTrackerPose(TrackerID tracker); glm::mat4 GetTrackerPose(TrackerID tracker);
@ -94,6 +96,6 @@ class App
void InitRootOverlay(); void InitRootOverlay();
void UpdateFramebuffer(); void UpdateFramebuffer();
void UpdateInput(); void UpdateInput(float dtime);
void UpdateUIVisibility(); void UpdateUIVisibility();
}; };

View file

@ -4,9 +4,10 @@
#include "util.h" #include "util.h"
#include <string> #include <string>
const float laser_width = 0.004f; const float LASER_WIDTH = 0.004f;
const Color edit_col{1, 0.6f, 1}; const Color EDIT_COLOR{1, 0.6f, 1};
const Color cursor_col{0.3f, 1, 1}; const Color CURSOR_COLOR{0.3f, 1, 1};
const float SCROLL_SPEED = 48.0f;
Controller::Controller(App *app, ControllerSide side) Controller::Controller(App *app, ControllerSide side)
{ {
@ -16,6 +17,7 @@ Controller::Controller(App *app, ControllerSide side)
_is_connected = false; _is_connected = false;
_side = side; _side = side;
_cursor_active = false; _cursor_active = false;
_last_sent_scroll = 0;
std::string laser_name = "controller_laser_"; std::string laser_name = "controller_laser_";
if (side == ControllerSide::Left) if (side == ControllerSide::Left)
@ -70,7 +72,7 @@ glm::vec3 Controller::GetLastRot()
return _last_rotation; return _last_rotation;
} }
void Controller::Update() void Controller::Update(float dtime)
{ {
UpdateStatus(); UpdateStatus();
if (!_is_connected) if (!_is_connected)
@ -80,7 +82,7 @@ void Controller::Update()
if (_app->_edit_mode) if (_app->_edit_mode)
{ {
_laser.SetColor(edit_col); _laser.SetColor(EDIT_COLOR);
if (_last_ray.overlay != nullptr) if (_last_ray.overlay != nullptr)
{ {
auto ray = _last_ray; auto ray = _last_ray;
@ -100,7 +102,7 @@ void Controller::Update()
if (_grabbed_overlay != nullptr) if (_grabbed_overlay != nullptr)
{ {
float move = _app->GetInputAnalog(_app->_input_handles.edit.distance, _input_handle).y * 0.1; // TODO use frame time float move = _app->GetInputAnalog(_app->_input_handles.edit.distance, _input_handle).y * dtime * 8;
if (move != 0.0f) if (move != 0.0f)
{ {
auto transform = _grabbed_overlay->GetTarget()->transform; auto transform = _grabbed_overlay->GetTarget()->transform;
@ -120,7 +122,7 @@ void Controller::Update()
} }
_cursor_active = !_cursor_active; _cursor_active = !_cursor_active;
_app->_active_cursor = this; _app->_active_cursor = this;
_laser.SetColor(cursor_col); _laser.SetColor(CURSOR_COLOR);
} }
if (_cursor_active) if (_cursor_active)
{ {
@ -139,19 +141,40 @@ void Controller::Update()
pos *= _last_ray.hit_panel->Width(); pos *= _last_ray.hit_panel->Width();
_last_ray.hit_panel->SetCursor(pos.x, pos.y); _last_ray.hit_panel->SetCursor(pos.x, pos.y);
} }
auto mouse_left = _app->GetInputDigital(_app->_input_handles.cursor.mouse_left, _input_handle); UpdateMouseButton(_app->_input_handles.cursor.mouse_left, 1);
if (mouse_left.bChanged) UpdateMouseButton(_app->_input_handles.cursor.mouse_middle, 2);
UpdateMouseButton(_app->_input_handles.cursor.mouse_right, 3);
auto scroll_state = _app->GetInputAnalog(_app->_input_handles.cursor.scroll, _input_handle);
if (scroll_state.y != 0)
{ {
_app->SendMouseInput(1, mouse_left.bState); _last_sent_scroll += dtime * glm::abs(scroll_state.y) * SCROLL_SPEED;
} if (_last_sent_scroll > 1)
auto mouse_right = _app->GetInputDigital(_app->_input_handles.cursor.mouse_right, _input_handle);
if (mouse_right.bChanged)
{ {
_app->SendMouseInput(3, mouse_right.bState); _last_sent_scroll = 0;
if (scroll_state.y > 0)
{
_app->SendMouseInput(4, true);
_app->SendMouseInput(4, false);
}
else if (scroll_state.y < 0)
{
_app->SendMouseInput(5, true);
_app->SendMouseInput(5, false);
} }
} }
} }
} }
}
}
void Controller::UpdateMouseButton(vr::VRActionHandle_t binding, unsigned int button)
{
auto state = _app->GetInputDigital(binding, _input_handle);
if (state.bChanged)
{
_app->SendMouseInput(button, state.bState);
}
}
void Controller::UpdateLaser() void Controller::UpdateLaser()
{ {
@ -170,7 +193,7 @@ void Controller::UpdateLaser()
hmd_local_pos.z = 0; hmd_local_pos.z = 0;
auto hmd_dir = glm::normalize(hmd_local_pos); auto hmd_dir = glm::normalize(hmd_local_pos);
VRMat transform = {{{laser_width * hmd_dir.y, 0, laser_width * hmd_dir.x, 0}, {laser_width * -hmd_dir.x, 0, laser_width * hmd_dir.y, 0}, {0, len, 0, len * -0.5f}}}; VRMat transform = {{{LASER_WIDTH * hmd_dir.y, 0, LASER_WIDTH * hmd_dir.x, 0}, {LASER_WIDTH * -hmd_dir.x, 0, LASER_WIDTH * hmd_dir.y, 0}, {0, len, 0, len * -0.5f}}};
_laser.SetTransformTracker(_device_index, &transform); _laser.SetTransformTracker(_device_index, &transform);
_laser.SetHidden(!_is_connected || _app->_hidden || (!_app->_edit_mode && !_cursor_active)); _laser.SetHidden(!_is_connected || _app->_hidden || (!_app->_edit_mode && !_cursor_active));
} }

View file

@ -26,7 +26,7 @@ class Controller
void ReleaseOverlay(); void ReleaseOverlay();
void Update(); void Update(float dtime);
bool _cursor_active; bool _cursor_active;
@ -34,6 +34,8 @@ class Controller
void UpdateStatus(); void UpdateStatus();
void UpdateLaser(); void UpdateLaser();
void UpdateMouseButton(vr::VRActionHandle_t binding, unsigned int button);
App *_app; App *_app;
Overlay _laser; Overlay _laser;
ControllerSide _side; ControllerSide _side;
@ -47,4 +49,6 @@ class Controller
Ray _last_ray; Ray _last_ray;
glm::vec3 _last_rotation; glm::vec3 _last_rotation;
glm::vec3 _last_pos; glm::vec3 _last_pos;
float _last_sent_scroll;
}; };

View file

@ -1,7 +1,7 @@
#include "app.h" #include "app.h"
#include <signal.h> #include <signal.h>
#define UPDATE_RATE 60 #define UPDATE_RATE 120
bool should_exit = false; bool should_exit = false;
@ -18,8 +18,8 @@ int main()
while (!should_exit) while (!should_exit)
{ {
app.Update();
usleep(1000000 / UPDATE_RATE); usleep(1000000 / UPDATE_RATE);
app.Update(1.0 / UPDATE_RATE);
} }
printf("\nShutting down\n"); printf("\nShutting down\n");
return 0; return 0;