refactoring vr code
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "pch.h"
|
||||
#include "app.h"
|
||||
#include "util.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
bool win32_vr_start();
|
||||
@@ -40,6 +41,72 @@ void App::vr_draw_ui()
|
||||
uirtt.unbindFramebuffer();
|
||||
}
|
||||
|
||||
bool trigger_down = false;
|
||||
cbuffer<glm::vec3> controller_points(10);
|
||||
glm::vec3 controller_last_point;
|
||||
void App::vr_update(float dt)
|
||||
{
|
||||
glm::vec3 hit;
|
||||
glm::vec2 hit_fb_pos;
|
||||
float t;
|
||||
if (ray_intersect({ 0, 0, 0 }, glm::normalize(vr_controller_pos), { 0, 0, -1 }, { 0, 0, 1 }, { 1, 0, 0 }, hit, t))
|
||||
{
|
||||
glm::mat4 plane_camera = glm::lookAt(glm::vec3(0, 0, -1), { 0, 0, 1 }, { 0, 1, 0 });
|
||||
glm::vec4 plane_local = plane_camera * glm::vec4(hit, 1);
|
||||
hit_fb_pos.x = -(plane_local.x * 0.5f - 0.5f) * width;
|
||||
hit_fb_pos.y = (plane_local.y * 0.5f + 0.5f) * height;
|
||||
LOG("cursor %f %f", hit_fb_pos.x, hit_fb_pos.y);
|
||||
mouse_move(hit_fb_pos.x, height - hit_fb_pos.y - 1, 1.f, kEventSource::Mouse, false);
|
||||
}
|
||||
if (trigger_down)
|
||||
{
|
||||
controller_points.add(App::I.vr_controller_pos * 800.f);
|
||||
auto p = controller_points.average();
|
||||
if (glm::distance(p, controller_last_point) > 10)
|
||||
{
|
||||
async_start();
|
||||
Canvas::I->stroke_update(p, 1.f);
|
||||
async_end();
|
||||
controller_last_point = p;
|
||||
App::I.redraw = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void App::vr_analog(const VRController& c, VRController::kButton b, VRController::kAction a, glm::vec2 force)
|
||||
{
|
||||
if (b == VRController::kButton::Trigger)
|
||||
{
|
||||
if (a == VRController::kAction::Press)
|
||||
{
|
||||
glm::vec3 pos = vr_controller_pos * 800.f;
|
||||
async_start();
|
||||
Canvas::I->stroke_start(pos, force.x);
|
||||
async_end();
|
||||
controller_last_point = pos;
|
||||
controller_points.clear();
|
||||
trigger_down = true;
|
||||
}
|
||||
if (a == VRController::kAction::Release)
|
||||
{
|
||||
trigger_down = false;
|
||||
async_start();
|
||||
Canvas::I->stroke_end();
|
||||
async_end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void App::vr_digital(const VRController& c, VRController::kButton b, VRController::kAction a, glm::vec2 axis)
|
||||
{
|
||||
if (b == VRController::kButton::Pad && a == VRController::kAction::Press)
|
||||
{
|
||||
if (glm::length(axis) < 0.5f)
|
||||
App::I.toggle_ui();
|
||||
}
|
||||
}
|
||||
|
||||
void App::vr_draw(const glm::mat4& proj, const glm::mat4& camera, const glm::mat4& pose)
|
||||
{
|
||||
//glm::mat4 ortho_proj = glm::ortho(0.f, box.z, 0.f, box.w, -1000.f, 1000.f);
|
||||
@@ -220,7 +287,7 @@ void App::vr_draw(const glm::mat4& proj, const glm::mat4& camera, const glm::mat
|
||||
if (ui_visible && mode)
|
||||
{
|
||||
auto pos = mode->m_resizing ? mode->m_size_pos_start : mode->m_cur_pos;
|
||||
if (App::I.keys[(int)kKey::KeyAlt] && !mode->m_resizing)
|
||||
if (keys[(int)kKey::KeyAlt] && !mode->m_resizing)
|
||||
pos.x = pos.x - canvas->m_canvas->m_current_brush->m_tip_size * 500;
|
||||
auto cur = (glm::vec2(pos.x / width, 1.f - pos.y / height) - 0.5f) * 2.f;
|
||||
|
||||
@@ -235,7 +302,7 @@ void App::vr_draw(const glm::mat4& proj, const glm::mat4& camera, const glm::mat
|
||||
glm::scale(glm::vec3(100)) *
|
||||
glm::transpose(canvas->m_canvas->m_cam_rot) *
|
||||
glm::translate(glm::vec3(cur * glm::vec2(aspect * tan_fov), -1)) *
|
||||
glm::scale(glm::vec3(canvas->m_canvas->m_current_brush->m_tip_size / App::I.height)) *
|
||||
glm::scale(glm::vec3(canvas->m_canvas->m_current_brush->m_tip_size / height)) *
|
||||
glm::eulerAngleZ(canvas->m_canvas->m_current_brush->m_tip_angle * (float)(M_PI * 2.0))
|
||||
);
|
||||
glEnable(GL_BLEND);
|
||||
@@ -305,7 +372,7 @@ void App::vr_draw(const glm::mat4& proj, const glm::mat4& camera, const glm::mat
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP,
|
||||
proj * camera * pos *
|
||||
glm::inverse(glm::lookAt({ 0, 0, 0 }, vr_controller_pos, { 0, 1, 0 })) *
|
||||
glm::scale(glm::vec3(canvas->m_canvas->m_current_brush->m_tip_size * 100.f / App::I.height)) *
|
||||
glm::scale(glm::vec3(canvas->m_canvas->m_current_brush->m_tip_size * 100.f / height)) *
|
||||
glm::eulerAngleZ(canvas->m_canvas->m_current_brush->m_tip_angle * (float)(M_PI * 2.0))
|
||||
);
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
Reference in New Issue
Block a user