vr paint
This commit is contained in:
75
src/hmd.cpp
75
src/hmd.cpp
@@ -3,6 +3,10 @@
|
||||
#include "log.h"
|
||||
#include <array>
|
||||
|
||||
std::map<ViveController::kButton, ViveController::kButtonMask> ViveController::m_mask{
|
||||
{ ViveController::kButton::Trigger, ViveController::kButtonMask::TriggerBit },
|
||||
};
|
||||
|
||||
bool Vive::Initialize()
|
||||
{
|
||||
vr::EVRInitError error;
|
||||
@@ -79,18 +83,78 @@ void Vive::Update()
|
||||
auto mat_proj = glm::make_mat4(data_eye_proj);
|
||||
auto mat_pose = glm::inverse(glm::make_mat4(data_hmd_pose));
|
||||
auto mat_eye = glm::inverse(glm::make_mat4(data_eye_pose));
|
||||
glm::vec3 head_position(h.m[0][3], h.m[1][3], h.m[2][3]);
|
||||
|
||||
m_proj[eye] = mat_proj;
|
||||
m_view[eye] = mat_eye * mat_pose;
|
||||
m_pose = mat_pose;
|
||||
|
||||
// invalidate controller state
|
||||
std::for_each(m_controllers, m_controllers + 1, [&](ViveController& c) { c.m_valid = false; });
|
||||
int controller_index = 0;
|
||||
auto pose_inv = glm::inverse(m_pose);
|
||||
|
||||
for (int id = 0; id < vr::k_unMaxTrackedDeviceCount; id++)
|
||||
{
|
||||
// device not tracking
|
||||
if (!poses[id].bPoseIsValid)
|
||||
continue;
|
||||
|
||||
switch (m_hmd->GetTrackedDeviceClass(id))
|
||||
{
|
||||
case vr::TrackedDeviceClass_HMD:
|
||||
m_active = (m_hmd->GetTrackedDeviceActivityLevel(id) == vr::k_EDeviceActivityLevel_UserInteraction);
|
||||
break;
|
||||
case vr::TrackedDeviceClass_Controller:
|
||||
{
|
||||
m_controllers[controller_index].m_valid = true;
|
||||
m_hmd->GetControllerState(id, &m_controllers[controller_index].m_state, sizeof(vr::VRControllerState_t));
|
||||
m_controllers[controller_index].m_mat = glm::translate(-head_position) * Pose2Mat(poses[id].mDeviceToAbsoluteTracking);
|
||||
|
||||
std::array<ViveController::kButton, 4> button_ids{
|
||||
ViveController::kButton::Trigger,
|
||||
//ViveController::kButton::Pad, // unused
|
||||
//ViveController::kButton::Menu, // unused
|
||||
//ViveController::kButton::Grip, // unused
|
||||
};
|
||||
|
||||
for (auto b : button_ids)
|
||||
{
|
||||
glm::vec2 force = { m_controllers[controller_index].m_state.rAxis[(int)b].x,
|
||||
m_controllers[controller_index].m_state.rAxis[(int)b].y };
|
||||
if (glm::compMax(glm::abs(force)) > 0.f && !m_controllers[controller_index].m_analog_buttons[(uint8_t)b])
|
||||
{
|
||||
m_controllers[controller_index].m_analog_buttons[(uint8_t)b] = true;
|
||||
if (on_analog_button)
|
||||
on_analog_button(m_controllers[controller_index], b, ViveController::kAction::Press);
|
||||
}
|
||||
if (glm::compMax(glm::abs(force)) == 0.f && m_controllers[controller_index].m_analog_buttons[(uint64_t)b])
|
||||
{
|
||||
m_controllers[controller_index].m_analog_buttons[(uint8_t)b] = false;
|
||||
if (on_analog_button)
|
||||
on_analog_button(m_controllers[controller_index], b, ViveController::kAction::Release);
|
||||
}
|
||||
}
|
||||
|
||||
auto pressed_mask = m_controllers[controller_index].m_state.ulButtonPressed;
|
||||
for (auto b : button_ids)
|
||||
{
|
||||
bool b_pressed = pressed_mask & (uint64_t)m_controllers[controller_index].m_mask[b];
|
||||
if (b_pressed == true && !m_controllers[controller_index].m_buttons[(uint64_t)b])
|
||||
{
|
||||
m_controllers[controller_index].m_buttons[(uint64_t)b] = true;
|
||||
if (on_button)
|
||||
on_button(m_controllers[controller_index], b, ViveController::kAction::Press);
|
||||
}
|
||||
if (b_pressed == false && m_controllers[controller_index].m_buttons[(uint64_t)b])
|
||||
{
|
||||
m_controllers[controller_index].m_buttons[(uint64_t)b] = false;
|
||||
if (on_button)
|
||||
on_button(m_controllers[controller_index], b, ViveController::kAction::Release);
|
||||
}
|
||||
}
|
||||
controller_index++;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -138,3 +202,14 @@ bool Vive::Valid()
|
||||
{
|
||||
return m_hmd && m_comp && comp_attempts < 10;
|
||||
}
|
||||
|
||||
glm::mat4 Vive::Pose2Mat(vr::HmdMatrix34_t const& m)
|
||||
{
|
||||
float data[16] = {
|
||||
m.m[0][0], m.m[1][0], m.m[2][0], 0,
|
||||
m.m[0][1], m.m[1][1], m.m[2][1], 0,
|
||||
m.m[0][2], m.m[1][2], m.m[2][2], 0,
|
||||
m.m[0][3], m.m[1][3], m.m[2][3], 1,
|
||||
};
|
||||
return glm::make_mat4(data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user