cgv
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 
20 class CGV_API vr_emulator;
21 
22 enum TrackerAttachment
23 {
24  TA_WORLD,
25  TA_HEAD,
26  TA_LEFT_HAND,
27  TA_RIGHT_HAND
28 };
29 
30 class CGV_API vr_emulated_kit : public vr::vr_kit, public cgv::render::render_types
31 {
32 public:
33  typedef cgv::math::fmat<float, 3, 4> mat3x4;
34 protected:
35  friend class vr_emulator;
36  vr::vr_kit_state state;
37  typedef cgv::math::quaternion<float> quat;
38  float body_direction;
39  float body_height;
40  float hip_parameter;
41  float yaw_parameter;
42  float fovy;
43  vec3 body_position;
44 
45  vec3 hand_position[2];
46  quat hand_orientation[2];
47 
48  vec3 tracker_positions[4];
49  quat tracker_orientations[4];
50  bool tracker_enabled[4];
51  TrackerAttachment tracker_attachments[4];
52 
54  mat3x4 construct_pos_matrix(const quat& orientation, const vec3& position);
55  mat4 construct_homogeneous_matrix(const quat& orientation, const vec3& position);
56  void set_pose_matrix(const mat4& H, float* pose) const;
57  void compute_state_poses();
58  bool query_state_impl(vr::vr_kit_state& state, int pose_query = 2);
59 public:
60  vr_emulated_kit(float _body_direction, const vec3& _body_position, float _body_height, unsigned _width, unsigned _height, vr::vr_driver* _driver, void* _handle, const std::string& _name);
61  vec3 get_body_direction() const;
62  const std::vector<std::pair<int, int> >& get_controller_throttles_and_sticks(int controller_index) const;
63  const std::vector<std::pair<float, float> >& get_controller_throttles_and_sticks_deadzone_and_precision(int controller_index) const;
64  bool set_vibration(unsigned controller_index, float low_frequency_strength, float high_frequency_strength);
65  void put_eye_to_head_matrix(int eye, float* pose_matrix) const;
66  void put_projection_matrix(int eye, float z_near, float z_far, float* projection_matrix, const float* hmd_pose) const;
67  void submit_frame();
68 };
69 
70 enum InteractionMode {
71  IM_BODY,
72  IM_LEFT_HAND,
73  IM_RIGHT_HAND,
74  IM_TRACKER_1,
75  IM_TRACKER_2,
76  IM_TRACKER_3,
77  IM_TRACKER_4,
78  IM_BASE_1,
79  IM_BASE_2,
80  IM_BASE_3,
81  IM_BASE_4
82 };
83 
84 class CGV_API vr_emulator :
87 {
88 public:
89  typedef cgv::math::quaternion<float> quat;
90  std::vector<vr_emulated_kit*> kits;
91  static bool gamepad_connected;
92  unsigned screen_width, screen_height;
93  unsigned counter;
94  vec3 body_position;
95  float body_direction;
96  float body_height;
97  InteractionMode interaction_mode;
98  quat hand_orientation[2];
99 
100  // coordinate transform used to emulate a displacement between tracking and world system
101  quat coordinate_rotation;
102  vec3 coordinate_displacement;
103  // information used to define reference states
104  std::vector<quat> base_orientations;
105  std::vector<vec3> base_positions;
106  std::vector<std::string> base_serials;
108  void update_reference_states(int i = -1);
109 
110 protected:
111  mutable std::map<std::string, vr::vr_trackable_state> transformed_reference_states;
112 
113  // emulation
114  bool installed;
115  float body_speed;
116 
117  bool left_ctrl, right_ctrl, up_ctrl, down_ctrl;
118  bool home_ctrl, end_ctrl, pgup_ctrl, pgdn_ctrl;
119  bool is_alt;
120 
121  void create_tracker_gui(vr_emulated_kit* kit, int i);
122  void create_trackable_gui(const std::string& name, vr::vr_trackable_state& ts);
123  void create_controller_gui(int i, vr::vr_controller_state& cs);
124 
125  int current_kit_index;
126 
127  void add_new_kit();
128  void timer_event(double t, double dt);
129 public:
131  vr_emulator();
133  void on_set(void* member_ptr);
135  std::string get_driver_name() const;
137  bool is_installed() const;
139  std::vector<void*> scan_vr_kits();
141  vr::vr_kit* replace_by_index(int& index, vr::vr_kit* new_kit_ptr);
143  bool replace_by_pointer(vr::vr_kit* old_kit_ptr, vr::vr_kit* new_kit_ptr);
145  void put_up_direction(float* up_dir) const;
147  float get_floor_level() const;
149  float get_action_zone_height() const;
151  void put_action_zone_bounary(std::vector<float>& boundary) const;
153  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);
155  bool handle_ctrl_key(cgv::gui::key_event& ke, bool& fst_ctrl, bool* snd_ctrl_ptr = 0);
157  bool handle(cgv::gui::event& e);
159  void stream_help(std::ostream& os);
161  std::string get_type_name() const;
163  void stream_stats(std::ostream&);
165  bool init(cgv::render::context& ctx);
169  void draw(cgv::render::context&);
175  void create_gui();
176 };
178 
179 #include <cgv/config/lib_end.h>
vr::vr_driver::get_floor_level
virtual float get_floor_level() const =0
return the floor level relativ to the world origin
cgv::math::fmat< float, 3, 4 >
cgv::base::node::get_type_name
std::string get_type_name() const
overload to return the type name of this object
Definition: node.cxx:47
cgv::render::drawable::finish_frame
virtual void finish_frame(context &)
this method is called in one pass over all drawables after drawing
Definition: drawable.cxx:119
cgv::gui::event_handler
interface for all classes that want to receive events
Definition: event_handler.h:15
cgv::gui::key_event
class to represent all possible keyboard events with the EID_KEY
Definition: key_event.h:23
cgv::base::base::stream_stats
virtual void stream_stats(std::ostream &)
overload to show the content of this object
Definition: base.cxx:25
vr::VRButtonStateFlags
VRButtonStateFlags
one flag for each vr controller button
Definition: vr_state.h:62
cgv::math::quaternion< float >
vr_driver.h
vr::vr_kit
a vr kit is composed of headset, two controllers, and two trackers, where all devices can be attached...
Definition: vr_kit.h:69
cgv::gui::create_gui
bool create_gui(provider *p, const std::string &label, void *value_ptr, const std::string &value_type, const std::string &gui_type, const std::string &options, bool *toggles)
create the gui for a composed structure
Definition: gui_creator.cxx:21
cgv::render::drawable::init
virtual bool init(context &)
this method is called after creation or recreation of the context, return whether all necessary funct...
Definition: drawable.cxx:98
vr::vr_kit::put_eye_to_head_matrix
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....
vr::vr_driver
interface class for vr drivers.
Definition: vr_driver.h:57
vr::vr_driver::replace_by_index
virtual vr_kit * replace_by_index(int &index, vr_kit *new_kit_ptr)=0
scan all connected vr kits and return a vector with their ids
cgv::base::base::self_reflect
virtual bool self_reflect(cgv::reflect::reflection_handler &)
used for simple self reflection
Definition: base.cxx:161
cgv::gui::provider
derive from this class to provide a gui to the current viewer
Definition: provider.h:64
cgv::render::drawable::init_frame
virtual void init_frame(context &)
this method is called in one pass over all drawables before the draw method
Definition: drawable.cxx:107
vr::vr_driver::is_installed
virtual bool is_installed() const =0
return whether driver is installed
cgv::reflect::reflection_handler
Definition: reflection_handler.h:63
vr::vr_kit::query_state_impl
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...
vr::vr_driver::get_driver_name
virtual std::string get_driver_name() const =0
return name of driver
vr::vr_trackable_state
a trackable knows whether it is tracked and its 6d pose stored as 3x4 matrix in column major format
Definition: vr_state.h:94
cgv::gui::event_handler::stream_help
virtual void stream_help(std::ostream &os)=0
overload to stream help information to the given output stream
cgv::gui::event
Definition: event.h:59
vr::vr_kit::set_vibration
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...
cgv::render::drawable::draw
virtual void draw(context &)
overload to draw the content of this drawable
Definition: drawable.cxx:111
cgv::gui::event_handler::handle
virtual bool handle(event &e)=0
overload and implement this method to handle events
vr::vr_driver::get_action_zone_height
virtual float get_action_zone_height() const =0
return height of action zone in meters
vr::vr_controller_state
Extends the trackable state by information on the buttons, input axes and vibration strengths.
Definition: vr_state.h:118
vr::vr_driver::put_up_direction
virtual void put_up_direction(float *up_dir) const =0
put a 3d up direction into passed array
vr::vr_driver::replace_by_pointer
virtual bool replace_by_pointer(vr_kit *old_kit_ptr, vr_kit *new_kit_ptr)=0
scan all connected vr kits and return a vector with their ids
vr::vr_kit::put_projection_matrix
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....
cgv::base::base::on_set
virtual void on_set(void *member_ptr)
this callback is called when the set_void method has changed a member and can be overloaded in derive...
Definition: base.cxx:210
vr::vr_kit::submit_frame
virtual void submit_frame()=0
submit the rendered stereo frame to the hmd
cgv::render::drawable
Definition: drawable.h:15
vr::vr_kit_state
structure that stores all information describing the state of a VR kit
Definition: vr_state.h:139
vr::vr_driver::put_action_zone_bounary
virtual void put_action_zone_bounary(std::vector< float > &boundary) const =0
return a vector of floor points defining the action zone boundary as a closed polygon
vr::vr_driver::scan_vr_kits
virtual std::vector< void * > scan_vr_kits()=0
scan all connected vr kits and return a vector with their ids
cgv::base::node
Definition: node.h:17
cgv::render::context
Definition: context.h:525