cgv
Loading...
Searching...
No Matches
vr_emulator.h
Go to the documentation of this file.
1#pragma once
2
3#include <cgv/base/base.h>
4#include <cgv/render/drawable.h>
5#include <cgv/math/quaternion.h>
6#include <cgv/gui/provider.h>
7#include <cgv/gui/event_handler.h>
8#include <cgv/gui/key_event.h>
9#include <vr/vr_kit.h>
10#include <vr/vr_driver.h>
11
12#include "lib_begin.h"
13
16
20class CGV_API vr_emulator;
21
22enum TrackerAttachment
23{
24 TA_WORLD,
25 TA_HEAD,
26 TA_LEFT_HAND,
27 TA_RIGHT_HAND
28};
29
30class CGV_API vr_emulated_kit : public vr::vr_kit
31{
32protected:
33 friend class vr_emulator;
34 vr::vr_kit_state state;
36 float body_direction;
37 float body_height;
38 float hip_parameter;
39 float yaw_parameter;
40 float fovy;
41 cgv::vec3 body_position;
42
43 cgv::vec3 hand_position[2];
44 quat hand_orientation[2];
45
46 cgv::vec3 tracker_positions[4];
47 quat tracker_orientations[4];
48 bool tracker_enabled[4];
49 TrackerAttachment tracker_attachments[4];
50
52 cgv::mat3x4 construct_pos_matrix(const quat& orientation, const cgv::vec3& position);
53 cgv::mat4 construct_homogeneous_matrix(const quat& orientation, const cgv::vec3& position);
54 void set_pose_matrix(const cgv::mat4& H, float* pose) const;
55 void compute_state_poses();
56 bool query_state_impl(vr::vr_kit_state& state, int pose_query = 2);
57public:
58 vr_emulated_kit(float _body_direction, const cgv::vec3& _body_position, float _body_height, unsigned _width, unsigned _height, vr::vr_driver* _driver, void* _handle, const std::string& _name, int _nr_trackers);
59 cgv::vec3 get_body_direction() const;
60 const std::vector<std::pair<int, int> >& get_controller_throttles_and_sticks(int controller_index) const;
61 const std::vector<std::pair<float, float> >& get_controller_throttles_and_sticks_deadzone_and_precision(int controller_index) const;
62 bool set_vibration(unsigned controller_index, float low_frequency_strength, float high_frequency_strength);
63 void put_eye_to_head_matrix(int eye, float* pose_matrix) const;
64 void put_projection_matrix(int eye, float z_near, float z_far, float* projection_matrix, const float* hmd_pose) const;
65 void submit_frame();
66};
67
68enum InteractionMode {
69 IM_BODY,
70 IM_LEFT_HAND,
71 IM_RIGHT_HAND,
72 IM_TRACKER_1,
73 IM_TRACKER_2,
74 IM_TRACKER_3,
75 IM_TRACKER_4,
76 IM_BASE_1,
77 IM_BASE_2,
78 IM_BASE_3,
79 IM_BASE_4
80};
81
82class CGV_API vr_emulator :
85{
86public:
87 std::vector<vr_emulated_kit*> kits;
88 static bool gamepad_connected;
89 unsigned screen_width, screen_height;
90 unsigned counter;
91 int nr_trackers = 2;
92 cgv::vec3 body_position;
93 float body_direction;
94 float body_height;
95 InteractionMode interaction_mode;
96 cgv::quat hand_orientation[2];
97
98 // coordinate transform used to emulate a displacement between tracking and world system
99 cgv::quat coordinate_rotation;
100 cgv::vec3 coordinate_displacement;
101 // information used to define reference states
102 std::vector<cgv::quat> base_orientations;
103 std::vector<cgv::vec3> base_positions;
104 std::vector<std::string> base_serials;
106 void update_reference_states(int i = -1);
107
108protected:
109 mutable std::map<std::string, vr::vr_trackable_state> transformed_reference_states;
110
111 // emulation
112 bool installed;
113 float body_speed;
114
115 bool left_ctrl, right_ctrl, up_ctrl, down_ctrl;
116 bool home_ctrl, end_ctrl, pgup_ctrl, pgdn_ctrl;
117 bool is_alt;
118
119 void create_tracker_gui(vr_emulated_kit* kit, int i);
120 void create_trackable_gui(const std::string& name, vr::vr_trackable_state& ts);
121 void create_controller_gui(int i, vr::vr_controller_state& cs);
122
123 int current_kit_index;
124
125 void add_new_kit();
126 void timer_event(double t, double dt);
127public:
129 vr_emulator();
131 void on_set(void* member_ptr);
133 std::string get_driver_name() const;
135 bool is_installed() const;
137 std::vector<void*> scan_vr_kits();
139 vr::vr_kit* replace_by_index(int& index, vr::vr_kit* new_kit_ptr);
141 bool replace_by_pointer(vr::vr_kit* old_kit_ptr, vr::vr_kit* new_kit_ptr);
143 void put_up_direction(float* up_dir) const;
145 float get_floor_level() const;
147 float get_action_zone_height() const;
149 void put_action_zone_bounary(std::vector<float>& boundary) const;
151 bool check_for_button_toggle(cgv::gui::key_event& ke, int controller_index, vr::VRButtonStateFlags button, float touch_x = 0.0f, float touch_y = 0.0f);
153 bool handle_ctrl_key(cgv::gui::key_event& ke, bool& fst_ctrl, bool* snd_ctrl_ptr = 0);
155 bool handle(cgv::gui::event& e);
157 void stream_help(std::ostream& os);
159 std::string get_type_name() const;
161 void stream_stats(std::ostream&);
163 bool init(cgv::render::context& ctx);
165 void init_frame(cgv::render::context&);
167 void draw(cgv::render::context&);
169 void finish_frame(cgv::render::context&);
171 bool self_reflect(cgv::reflect::reflection_handler& srh);
173 void create_gui();
174};
176
177#include <cgv/config/lib_end.h>
The node class keeps a pointer to its parent.
Definition node.h:19
interface for all classes that want to receive events
class to represent all possible keyboard events with the EID_KEY
Definition key_event.h:23
derive from this class to provide a gui to the current viewer
Definition provider.h:64
implements a quaternion.
Definition quaternion.h:21
the self reflection handler is passed to the virtual self_reflect() method of cgv::base::base.
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
base class for all drawables, which is independent of the used rendering API.
Definition drawable.h:15
interface class for vr drivers.
Definition vr_driver.h:57
a vr kit is composed of headset, two controllers, and two trackers, where all devices can be attached...
Definition vr_kit.h:69
virtual bool set_vibration(unsigned controller_index, float low_frequency_strength, float high_frequency_strength)=0
set the vibration strength between 0 and 1 of low and high frequency motors, return false if device i...
void * handle
handle for internal use
Definition vr_kit.h:74
virtual bool query_state_impl(vr_kit_state &state, int pose_query)=0
derived kits implement this without caring about calibration; vr_kit::query_state() will apply driver...
virtual void put_eye_to_head_matrix(int eye, float *pose_matrix) const =0
access to 3x4 matrix in column major format for transformation from eye (0..left, 1....
virtual void submit_frame()=0
submit the rendered stereo frame to the hmd
virtual void put_projection_matrix(int eye, float z_near, float z_far, float *projection_matrix, const float *hmd_pose=0) const =0
access to 4x4 matrix in column major format for perspective transformation from eye (0....
VRButtonStateFlags
one flag for each vr controller button
Definition vr_state.h:62
std::vector< void * > scan_vr_kits()
iterate all registered vr drivers to scan for vr kits and return vector of vr kit handles
bool replace_by_pointer(vr_kit *old_kit_ptr, vr_kit *new_kit_ptr)
scan vr kits and if one with given pointer exists, replace it by passed kit; return whether replaceme...
vr_kit * replace_by_index(int vr_kit_index, vr_kit *new_kit_ptr)
scan vr kits and if one of given index exists replace it by passed kit and return index to replaced k...
Extends the trackable state by information on the buttons, input axes and vibration strengths.
Definition vr_state.h:118
structure that stores all information describing the state of a VR kit
Definition vr_state.h:139
a trackable knows whether it is tracked and its 6d pose stored as 3x4 matrix in column major format
Definition vr_state.h:94
defines the class vr::vr_driver class and gives access to the driver registry with the functions vr::...