cgv
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vr_render_helpers.cxx
1#include <cgv/base/base.h>
2#include "vr_render_helpers.h"
3#include <cgv/math/ftransform.h>
4#include <cgv/base/import.h>
5
7namespace vr {
8 bool& ref_vrmesh_outofdate(VRMeshId id)
9 {
10 static std::vector<int> outofdates;
11 if ((int)outofdates.size() <= id)
12 outofdates.resize(id + 1, true);
13 return reinterpret_cast<bool&>(outofdates[id]);
14 }
15 std::string& ref_vrmesh_file_name(VRMeshId id)
16 {
17 static std::vector<std::string> file_names;
18 if ((int)file_names.size() <= id)
19 file_names.resize(id + 1);
20 return file_names[id];
21 }
22 const std::string& get_vrmesh_file_name(VRMeshId id)
23 {
24 return ref_vrmesh_file_name(id);
25 }
26 void set_vrmesh_file_name(VRMeshId id, const std::string& file_name)
27 {
28 ref_vrmesh_file_name(id) = file_name;
29 ref_vrmesh_outofdate(id) = true;
30 }
32 {
33 static std::vector<cgv::render::mesh_render_info*> mesh_infos;
34 if ((int)mesh_infos.size() <= id)
35 mesh_infos.resize(id + 1, 0);
36 // in case file name changed, delete previous mesh render info
37 if (ref_vrmesh_outofdate(id) && !get_vrmesh_file_name(id).empty() && mesh_infos[id] != 0) {
38 mesh_infos[id]->destruct(ctx);
39 delete mesh_infos[id];
40 mesh_infos[id] = 0;
41 }
42 // check whether mesh needs to be read from file
43 if (mesh_infos[id] == 0 && !get_vrmesh_file_name(id).empty()) {
46 mesh_infos[id] = new cgv::render::mesh_render_info();
47 mesh_infos[id]->construct(ctx, M);
48 mesh_infos[id]->bind(ctx, ctx.ref_surface_shader_program(true), true);
49 }
50 ref_vrmesh_outofdate(id) = false;
51 }
52 return mesh_infos[id];
53 }
54
56 cgv::mat4 get_mat4_from_pose(const float pose_matrix[12])
57 {
58 cgv::mat4 M;
59
60 M.set_col(0, cgv::vec4(reinterpret_cast<const cgv::vec3&>(pose_matrix[0]), 0));
61 M.set_col(1, cgv::vec4(reinterpret_cast<const cgv::vec3&>(pose_matrix[3]), 0));
62 M.set_col(2, cgv::vec4(reinterpret_cast<const cgv::vec3&>(pose_matrix[6]), 0));
63 M.set_col(3, cgv::vec4(reinterpret_cast<const cgv::vec3&>(pose_matrix[9]), 1));
64 return M;
65 }
67 cgv::mat4 get_world_to_eye_transform(const vr_kit* vr_kit_ptr, const vr_kit_state& state, int eye)
68 {
69 cgv::mat4 T;
70 vr_kit_ptr->put_world_to_eye_transform(eye, state.hmd.pose, T.data());
71 return T;
72 /*
73 float eye_to_head[12];
74 vr_kit_ptr->put_eye_to_head_matrix(eye, eye_to_head);
75 return inv(get_mat4_from_pose(state.hmd.pose) * get_mat4_from_pose(eye_to_head));
76 */
77 }
79 cgv::mat4 get_world_to_camera_transform(const vr_kit* vr_kit_ptr, const vr_kit_state& state, int eye)
80 {
81 float camera_to_head[12];
82 const vr_camera* camera_ptr = vr_kit_ptr->get_camera();
83 if (!camera_ptr)
84 return cgv::math::identity4<float>();
85 camera_ptr->put_camera_to_head_matrix(eye, camera_to_head);
86 return inv(get_mat4_from_pose(state.hmd.pose) * get_mat4_from_pose(camera_to_head));
87 }
89 cgv::mat4 get_eye_projection_transform(const vr_kit* vr_kit_ptr, const vr_kit_state& state, float z_near, float z_far, int eye)
90 {
91 cgv::mat4 P;
92 vr_kit_ptr->put_projection_matrix(eye, z_near, z_far, &P(0, 0), state.hmd.pose);
93 return P;
94 }
96 cgv::mat4 get_camera_projection_transform(const vr_kit* vr_kit_ptr, float z_near, float z_far, int eye, bool undistorted)
97 {
98 const vr_camera* camera_ptr = vr_kit_ptr->get_camera();
99 if (!camera_ptr)
100 return cgv::math::identity4<float>();
101 cgv::mat4 TM;
102 camera_ptr->put_projection_matrix(eye, undistorted, z_near, z_far, &TM(0, 0));
103 return TM;
104 }
106 cgv::mat4 get_texture_transform(const vr_kit* vr_kit_ptr, const vr_kit_state& state, float z_near, float z_far, int eye, bool undistorted)
107 {
108 return get_camera_projection_transform(vr_kit_ptr, z_near, z_far, eye, undistorted) * get_world_to_camera_transform(vr_kit_ptr, state, eye);
109 }
110 bool configure_seethrough_shader_program(cgv::render::context& ctx, cgv::render::shader_program& prog, uint32_t frame_width, uint32_t frame_height, const vr_kit* vr_kit_ptr, const vr_kit_state& state, float z_near, float z_far, int eye, bool undistorted)
111 {
112 vr::vr_camera* camera_ptr = vr_kit_ptr->get_camera();
113 if (!camera_ptr)
114 return false;
115
116 int nr_cameras = camera_ptr->get_nr_cameras();
117 int frame_split = camera_ptr->get_frame_split();
118 cgv::vec2 focal_lengths;
119 cgv::vec2 camera_center;
120 camera_ptr->put_camera_intrinsics(eye, true, &focal_lengths(0), &camera_center(0));
121 camera_center(0) /= frame_width;
122 camera_center(1) /= frame_height;
123 cgv::vec2 extent_texcrd(0.5f);
124
125 cgv::mat4 TM = vr::get_texture_transform(vr_kit_ptr, state, z_near, z_far, eye, undistorted);
126
127 return
128 prog.set_uniform(ctx, "texture_matrix", TM) &&
129 prog.set_uniform(ctx, "extent_texcrd", extent_texcrd) &&
130 prog.set_uniform(ctx, "frame_split", frame_split) &&
131 prog.set_uniform(ctx, "center_left", camera_center) &&
132 prog.set_uniform(ctx, "center_right", camera_center) &&
133 prog.set_uniform(ctx, "eye", eye);
134 }
135}
void set_col(unsigned j, const fvec< T, N > &v)
set column j of the matrix to vector v
Definition fmat.h:217
T * data()
cast into array. This allows calls like glVertex<N><T>v(p.data()) instead of glVertex<N><T,...
Definition fvec.h:181
the simple_mesh class is templated over the coordinate type that defaults to float
bool read(const std::string &file_name)
read simple mesh from file (currently only obj and stl are supported)
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
virtual shader_program & ref_surface_shader_program(bool texture_support=false)=0
return a reference to the default shader program used to render surfaces
the mesh_render_info structure manages vertex buffer objects for attribute and element buffers as wel...
void destruct(cgv::render::context &ctx)
destruct render mesh info and free vertex buffer objects
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
interface for mono or stereo cameras in VR headsets
Definition vr_camera.h:39
virtual bool put_projection_matrix(int camera_index, bool undistorted, float z_near, float z_far, float *projection_matrix) const =0
access to 4x4 matrix in column major format for perspective transformation of camera (0....
virtual bool put_camera_intrinsics(int camera_index, bool undistorted, float *focal_length_2d_ptr, float *center_2d_ptr) const =0
write the focal lengths in x- and y-direction to access to focal_length_2d_ptr[0|1] and the texture c...
CameraFrameSplit get_frame_split() const
query stereo frame layout
Definition vr_camera.cxx:89
virtual bool put_camera_to_head_matrix(int camera_index, float *pose_matrix) const =0
access to 3x4 matrix in column major format for transformation from camera (0 .. left,...
uint8_t get_nr_cameras() const
return number of cameras in the headset (1 for mono and 2 for stereo)
Definition vr_camera.cxx:99
a vr kit is composed of headset, two controllers, and two trackers, where all devices can be attached...
Definition vr_kit.h:69
virtual void put_world_to_eye_transform(int eye, const float *hmd_pose, float *modelview_matrix) const
access to 4x4 modelview transformation matrix of given eye in column major format,...
Definition vr_kit.cxx:92
vr_camera * get_camera() const
return camera or nullptr if not available
Definition vr_kit.cxx:52
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....
std::string find_data_file(const std::string &file_name, const std::string &strategy, const std::string &sub_directory, const std::string &master_path)
Find a file with the given strategy and return the file name extended by the necessary path.
Definition import.cxx:59
the vr namespace for virtual reality support
void set_vrmesh_file_name(VRMeshId id, const std::string &file_name)
set the file name for the given vrmesh type
cgv::mat4 get_eye_projection_transform(const vr_kit *vr_kit_ptr, const vr_kit_state &state, float z_near, float z_far, int eye)
query projection matrix for a given eye (0 ... left, 1 ... right)
bool configure_seethrough_shader_program(cgv::render::context &ctx, cgv::render::shader_program &prog, uint32_t frame_width, uint32_t frame_height, const vr_kit *vr_kit_ptr, const vr_kit_state &state, float z_near, float z_far, int eye, bool undistorted)
set all uniforms of seethrough shader program for a given camera (0 ... left or mono,...
VRMeshId
enumerate for different mesh types
cgv::mat4 get_mat4_from_pose(const float pose_matrix[12])
convert pose to mat4
cgv::render::mesh_render_info * get_vrmesh_render_info(cgv::render::context &ctx, VRMeshId id)
return a pointer to a mesh info structure for the given mesh type (read and construct if necessary); ...
cgv::mat4 get_world_to_eye_transform(const vr_kit *vr_kit_ptr, const vr_kit_state &state, int eye)
compute lookat matrix for a given eye (0 ... left, 1 ... right)
cgv::mat4 get_camera_projection_transform(const vr_kit *vr_kit_ptr, float z_near, float z_far, int eye, bool undistorted)
query projection matrix for a given camera (0 ... left or mono, 1 ... right only for stereo cameras)
cgv::mat4 get_texture_transform(const vr_kit *vr_kit_ptr, const vr_kit_state &state, float z_near, float z_far, int eye, bool undistorted)
query the texture matrix needed for projective texture mapping for a given camera (0 ....
cgv::mat4 get_world_to_camera_transform(const vr_kit *vr_kit_ptr, const vr_kit_state &state, int eye)
compute lookat matrix for a given camera (0 ... left, 1 ... right)
const std::string & get_vrmesh_file_name(VRMeshId id)
return the file name for the given vrmesh type
structure that stores all information describing the state of a VR kit
Definition vr_state.h:139
vr_trackable_state hmd
status and pose of hmd
Definition vr_state.h:141
float pose[12]
pose as 3x4 matrix in column major format, where each column is a vector in world coordinates
Definition vr_state.h:104