Skip to content

Instantly share code, notes, and snippets.

@ChristophHaag
Created November 8, 2019 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristophHaag/9a9584358e606826b88298c81fa1f27c to your computer and use it in GitHub Desktop.
Save ChristophHaag/9a9584358e606826b88298c81fa1f27c to your computer and use it in GitHub Desktop.
openvr overlay, dashboard pointer, actions
{
"default_bindings":[
{
"controller_type":"vive_controller",
"binding_url":"bindings_vive_controller.json"
},
{
"controller_type": "knuckles",
"binding_url": "bindings_vive_controller.json"
}
],
"actions":[
{
"name":"/actions/test/in/trigger",
"type":"boolean"
},
{
"name": "/actions/test/in/hand_pose",
"type": "pose"
}
],
"action_sets": [
{
"name": "/actions/test",
"usage": "leftright"
}
],
"localization":[
{
"language_tag":"en_US",
"/actions/test/in/trigger":"Trigger",
"/actions/test/in/hand_pose":"Hand Pose"
}
]
}
{
"bindings" : {
"/actions/test" : {
"sources" : [
{
"path": "/user/hand/right/input/trigger",
"mode": "button",
"inputs": {
"click": {
"output": "/actions/test/in/trigger"
}
}
},
{
"path": "/user/hand/left/input/trigger",
"mode": "button",
"inputs": {
"click": {
"output": "/actions/test/in/trigger"
}
}
}
],
"poses": [
{
"output": "/actions/test/in/hand_pose",
"path": "/user/hand/right/pose/raw"
},
{
"output": "/actions/test/in/hand_pose",
"path": "/user/hand/left/pose/raw"
}
]
}
},
"controller_type": "vive_controller",
"description" : "Test bindings",
"name" : "Vive Controller"
}
//g++ overlay-dashboard-pointer.cpp -o overlay-dashboard-pointer -lopenvr_api
#include <stdio.h>
#include <openvr/openvr.h>
#include <unistd.h>
using namespace vr;
void check_error(int line, EVRInitError error) { if (error != 0) printf("%d: error %s\n", line, VR_GetVRInitErrorAsSymbol(error)); }
void check_input_error(int line, EVRInputError error) {
if (error != EVRInputError::VRInputError_None) {
printf("Input error at %d: %d\n", line, error);
exit(1);
}
}
bool file_exists(const std::string& name) {
return ( access( name.c_str(), F_OK ) != -1 );
}
int main(int argc, char **argv) { (void) argc; (void) argv;
if (argc < 2) {
printf("Usage:\n\t%s /path/to/actions.json", argv[0]);
return 1;
}
const char *manifest_path = argv[1];
if (!file_exists ("/tmp/image.png"))
system ("wget -O /tmp/image.png https://upload.wikimedia.org/wikipedia/commons/c/ce/PNG_demo_Banana.png");
EVRInitError error;
VR_Init(&error, vr::VRApplication_Overlay);
check_error(__LINE__, error);
VROverlayHandle_t handle;
VROverlay()->CreateOverlay ("image", "image", &handle);
VROverlay()->SetOverlayFromFile(handle, "/tmp/image.png");
VROverlay()->SetOverlayWidthInMeters(handle, 1.5);
VROverlay()->ShowOverlay(handle);
vr::HmdMatrix34_t transform = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, -2.0f };
VROverlay()->SetOverlayTransformAbsolute(handle, TrackingUniverseStanding, &transform);
HmdVector2_t scale = { 420, 268 };
VROverlay()->SetOverlayMouseScale(handle, &scale);
EVRInputError inputerr = VRInput()->SetActionManifestPath(manifest_path);
if (inputerr != EVRInputError::VRInputError_None)
printf("Input error %d: %d\n", __LINE__, inputerr);
VRActionSetHandle_t test_handle = 0;
inputerr = VRInput()->GetActionSetHandle("/actions/test", &test_handle);
check_input_error(__LINE__, inputerr);
VRActionHandle_t trigger_handle = 0;
inputerr = VRInput()->GetActionHandle("/actions/test/in/trigger", &trigger_handle);
check_input_error(__LINE__, inputerr);
VRActionHandle_t pose_handle = 0;
inputerr = VRInput()->GetActionHandle("/actions/test/in/hand_pose", &pose_handle);
check_input_error(__LINE__, inputerr);
VRInputValueHandle_t right_hand_handle = 0;
inputerr = VRInput()->GetInputSourceHandle("/user/hand/right", &right_hand_handle);
check_input_error(__LINE__, inputerr);
struct VRActiveActionSet_t active_test_action_set = {};
active_test_action_set.ulActionSet = test_handle;
bool showTrigger = true;
bool showPose = true;
bool overlaysInteractiveIfVisible = false;
VROverlay()->SetOverlayInputMethod(handle, VROverlayInputMethod_Mouse);
VROverlay()->SetOverlayFlag(handle, VROverlayFlags_MakeOverlaysInteractiveIfVisible, overlaysInteractiveIfVisible);
for (int i = 0; i < 10000; i++) {
usleep (50000);
if (i % 100 == 0) {
printf("interactive: %d\n", overlaysInteractiveIfVisible);
VROverlay()->SetOverlayFlag(handle, VROverlayFlags_MakeOverlaysInteractiveIfVisible, overlaysInteractiveIfVisible);
overlaysInteractiveIfVisible = !overlaysInteractiveIfVisible;
}
if (VROverlay()->IsHoverTargetOverlay(handle))
printf("Hovering overlay!\n");
inputerr = VRInput()->UpdateActionState(&active_test_action_set, sizeof(active_test_action_set), 1);
check_input_error(__LINE__, inputerr);
VRInputValueHandle_t origin_handles[k_unMaxActionOriginCount];
EVRInputError err = VRInput()->GetActionOrigins(test_handle, trigger_handle, origin_handles, k_unMaxActionOriginCount);
if (err != VRInputError_None) {
printf("GetActionOrigins for test failed, retrying later...\n");
} else {
int origin_count = -1;
while (origin_handles[++origin_count] != k_ulInvalidInputValueHandle);
//printf ("Action test has %d origins\n", origin_count);
}
InputDigitalActionData_t data;
inputerr = VRInput()->GetDigitalActionData(trigger_handle, &data, sizeof(data), right_hand_handle);
check_input_error(__LINE__, inputerr);
//printf("Right Trigger State: %d, Changed: %d\n", data.bState, data.bChanged);
if (showTrigger) {
if (data.bActive)
printf("changed %d value %d\n", data.bChanged, data.bState);
else
printf("Trigger action not active\n");
}
InputPoseActionData_t pose_data;
inputerr = VRInput()->GetPoseActionDataRelativeToNow(pose_handle, TrackingUniverseStanding, 0, &pose_data, sizeof (pose_data), right_hand_handle);
check_input_error(__LINE__, inputerr);
if (showPose) {
if (pose_data.bActive)
printf("Pose valid %d %f %f %f\n",
pose_data.pose.bPoseIsValid,
pose_data.pose.mDeviceToAbsoluteTracking.m[0][3],
pose_data.pose.mDeviceToAbsoluteTracking.m[1][3],
pose_data.pose.mDeviceToAbsoluteTracking.m[2][3]
);
else
printf("Pose action not active\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment