Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic implementation of tracker roles #97

Open
wants to merge 1 commit into
base: Godot-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified demo/addons/godot-openvr/bin/win64/libgodot_openvr.dll
Binary file not shown.
64 changes: 54 additions & 10 deletions src/open_vr/openvr_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@

using namespace godot;

static const char *kWellKnownInputSourcePaths[] = {
"/unrestricted", //Any
"/user/hand/left", // LeftHand,
"/user/hand/right", // RightHand,
"/user/knee/left", // LeftKnee,
"/user/knee/right", // RightKnee,
"/user/elbow/left", // LeftElbow,
"/user/elbow/right", // RightElbow,
"/user/foot/left", // LeftFoot,
"/user/foot/right", // RightFoot,
"/user/shoulder/left", // LeftShoulder,
"/user/shoulder/right", // RightShoulder,
"/user/waist", // Waist,
"/user/chest", // Chest,
"/user/head", // Head,
"/user/gamepad", // Gamepad,
"/user/camera", // Camera,
"/user/keyboard", // Keyboard,
"/user/treadmill"
};

openvr_data *openvr_data::singleton = NULL;

openvr_data::openvr_data() {
Expand Down Expand Up @@ -206,11 +227,21 @@ bool openvr_data::initialise() {
}
}

size_t knowncount = (sizeof(kWellKnownInputSourcePaths) / sizeof(kWellKnownInputSourcePaths[0]));
handles_by_well_known_path.resize(knowncount);
for (size_t i = 0; i < knowncount; i++) {
handles_by_well_known_path[i] = vr::k_ulInvalidInputValueHandle;
}

if (success) {
for (size_t i = 0; i < knowncount; i++) {
vr::VRInput()->GetInputSourceHandle(kWellKnownInputSourcePaths[i], &handles_by_well_known_path[i]);
}
/* reset some stuff */
for (int i = 0; i < vr::k_unMaxTrackedDeviceCount; i++) {
tracked_devices[i].tracker_id = 0;
tracked_devices[i].last_rumble_update = 0;
tracked_devices[i].well_known_device_path = "";
}

device_hands_are_available = false;
Expand Down Expand Up @@ -623,6 +654,7 @@ bool openvr_data::get_custom_pose_data(int p_action_idx, vr::InputPoseActionData
source_handle = tracked_devices[right_hand_device].source_handle;
}

// virtual EVRInputError GetPoseActionDataForNextFrame(VRActionHandle_t action, ETrackingUniverseOrigin eOrigin, InputPoseActionData_t * pActionData, uint32_t unActionDataSize, VRInputValueHandle_t ulRestrictToDevice) = 0;
// let's retrieve it
vr::EVRInputError err = vr::VRInput()->GetPoseActionDataForNextFrame(custom_actions[p_action_idx].handle, vr::TrackingUniverseStanding, p_data, sizeof(vr::InputPoseActionData_t), source_handle);
if (err != vr::VRInputError_None) {
Expand Down Expand Up @@ -736,24 +768,23 @@ void openvr_data::attach_device(uint32_t p_device_index) {
if (p_device_index == vr::k_unTrackedDeviceIndexInvalid) {
// really?!
} else if (device->tracker_id == 0) {
char device_name[256];
strcpy(device_name, get_device_name(p_device_index, 255));
std::string device_name = get_device_name(p_device_index, 255);

vr::TrackedDeviceClass device_class = get_tracked_device_class(p_device_index);
if (device_class == vr::TrackedDeviceClass_TrackingReference) {
// ignore base stations and cameras for now
Godot::print(
godot::String("Found base station ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name) + godot::String(")"));
godot::String("Found base station ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name.c_str()) + godot::String(")"));
} else if (device_class == vr::TrackedDeviceClass_HMD) {
// ignore any HMD
Godot::print(
godot::String("Found HMD ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name) + godot::String(")"));
godot::String("Found HMD ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name.c_str()) + godot::String(")"));
} else {
godot_int hand = 0;

if (device_class == vr::TrackedDeviceClass_Controller) {
Godot::print(
godot::String("Found controller ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name) + godot::String(")"));
godot::String("Found controller ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name.c_str()) + godot::String(")"));

// If this is a controller than get our controller role
int32_t controllerRole = get_controller_role(p_device_index);
Expand All @@ -773,23 +804,36 @@ void openvr_data::attach_device(uint32_t p_device_index) {
}
} else {
Godot::print(
godot::String("Found tracker ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name) + godot::String(")"));
godot::String("Found tracker ") + String::num_int64(p_device_index) + godot::String("(") + godot::String(device_name.c_str()) + godot::String(")"));
}

sprintf(&device_name[strlen(device_name)], "_%i", p_device_index);
device->tracker_id = godot::arvr_api->godot_arvr_add_controller(device_name, hand, true, true);

// remember our primary left and right hand devices
if ((hand == 1) && (left_hand_device == vr::k_unTrackedDeviceIndexInvalid)) {
vr::VRInput()->GetInputSourceHandle("/user/hand/left", &device->source_handle);
device->well_known_device_path = "/user/hand/left";
left_hand_device = p_device_index;
} else if ((hand == 2) && (right_hand_device == vr::k_unTrackedDeviceIndexInvalid)) {
vr::VRInput()->GetInputSourceHandle("/user/hand/right", &device->source_handle);
device->well_known_device_path = "/user/hand/right";
right_hand_device = p_device_index;
} else {
// other devices don't have source handles...
// other devices might not have source handles...
device->source_handle = vr::k_ulInvalidInputValueHandle;
device->well_known_device_path = "";
for (size_t i = 0; i < handles_by_well_known_path.size(); i++) {
vr::VRInputValueHandle_t inputHandle = handles_by_well_known_path[i];
vr::InputOriginInfo_t data;
vr::VRInput()->GetOriginTrackedDeviceInfo(inputHandle, &data, sizeof(data));
if (data.trackedDeviceIndex == p_device_index) {
device->source_handle = data.devicePath;
device->well_known_device_path = kWellKnownInputSourcePaths[i];
}
}
}
char buf[64];
snprintf(buf, sizeof(buf), "_%i", p_device_index);
device_name = std::string(device->well_known_device_path) + "_" + device_name + buf;
device->tracker_id = godot::arvr_api->godot_arvr_add_controller(const_cast<char*>(device_name.c_str()), hand, true, true);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/open_vr/openvr_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class openvr_data {
struct tracked_device {
godot_int tracker_id;
uint64_t last_rumble_update;
const char* well_known_device_path;

/* add our controller source */
vr::VRInputValueHandle_t source_handle;
Expand All @@ -114,6 +115,8 @@ class openvr_data {

tracked_device tracked_devices[vr::k_unMaxTrackedDeviceCount];

std::vector<vr::VRInputValueHandle_t> handles_by_well_known_path;

void attach_device(uint32_t p_device_index);
void detach_device(uint32_t p_device_index);
void process_device_actions(tracked_device *p_device, uint64_t p_msec);
Expand Down