implement motion controllers and vr drawing with brush preview

This commit is contained in:
2018-11-02 13:49:15 +01:00
parent 3b73e84fe9
commit 3ee10bb88d
13 changed files with 202 additions and 46 deletions

View File

@@ -5,6 +5,7 @@
#include "texture.h"
#include "image.h"
#include "app.h"
#include "canvas.h"
#include "keymap.h"
#include "hmd.h"
#include "../resource.h"
@@ -715,6 +716,32 @@ int main(int argc, char** argv)
BT_SetTerminate();
LOG("start hmd render thread");
App::I.has_vr = true;
bool trigger_down = false;
cbuffer<glm::vec3, 10> controller_points;
glm::vec3 controller_last_point;
vive->on_button_axis = [&](const Controller& c, Controller::ButtonAxis b, Controller::Action a) {
if (b == Controller::ButtonAxis::Trigger)
{
if (a == Controller::Action::Press)
{
async_lock();
ui::Canvas::I->stroke_start(vive->controllers[0].pos * 800.f, vive->controllers[0].axis[1].x, ui::Canvas::I->m_current_brush);
async_unlock();
controller_last_point = vive->controllers[0].pos * 800.f;
controller_points.clear();
trigger_down = true;
}
if (a == Controller::Action::Release)
{
trigger_down = false;
async_lock();
ui::Canvas::I->stroke_end();
async_unlock();
}
}
};
const float target_tick_rate = 90;
unsigned long t0 = GetTickCount();
while (running && vive->Valid())
@@ -723,14 +750,37 @@ int main(int argc, char** argv)
unsigned long t1 = GetTickCount();
float dt = (float)(t1 - t0) / 1000.0f;
async_lock();
vive->Draw();
async_unlock();
vive->Update();
App::I.vr_active = vive->active;
App::I.vr_controller = vive->controllers[0].xform;
App::I.vr_controller_pos = vive->controllers[0].pos;
if (trigger_down)
{
controller_points.add(vive->controllers[0].pos * 800.f);
auto p = controller_points.average();
if (glm::distance(p, controller_last_point) > 1)
{
async_lock();
ui::Canvas::I->stroke_update(p, vive->controllers[0].axis[1].x);
async_unlock();
controller_last_point = p;
App::I.redraw = true;
}
}
if (vive->active)
{
async_lock();
vive->Draw();
async_unlock();
}
const int framerate = (1.f / target_tick_rate) * 1000;
const int diff = framerate - (t1 - t0);
hmd_render_cv.wait_for(lock, std::chrono::milliseconds(diff));
}
App::I.vr_active = false;
App::I.has_vr = false;
async_lock();
vive->Terminate();