Skip to content

Commit

Permalink
added support for data path and vr mesh rendering of controller and hmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sgumhold committed Mar 24, 2019
1 parent a63ada1 commit 427a49f
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 106 deletions.
1 change: 0 additions & 1 deletion cgv/base/import.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ std::string find_data_file_1(const std::string& base_path, const std::string& su

std::string find_data_file(const std::string& file_name, const std::string& strategy, const std::string& sub_directory, const std::string& master_path)
{
// std::cout << "find " << file_name << " in " << std::endl;
for (unsigned i=0; i<strategy.size(); ++i) {
switch (strategy[i]) {
case 'r' :
Expand Down
29 changes: 29 additions & 0 deletions cgv/math/ftransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ namespace cgv {
template <typename T> fmat<T, 4, 4>
rotate4(const T& A, const T& ax, const T& ay, const T& az) { return rotate4(A, fvec<T, 3>(ax, ay, az)); }

/// construct 4x4 pose matrix from a 3x4 matrix
template <typename T> fmat<T, 4, 4>
pose4(const fmat<T,3,4>& M) {
fmat<T, 4, 4> M4;
M4.set_col(0, fvec<T, 4>(M.col(0), 0));
M4.set_col(1, fvec<T, 4>(M.col(1), 0));
M4.set_col(2, fvec<T, 4>(M.col(2), 0));
M4.set_col(3, fvec<T, 4>(M.col(3), 1));
return M4;
}
/// construct 4x4 pose matrix from a 3x3 rotation matrix and translation vector
template <typename T> fmat<T, 4, 4>
pose4(const fmat<T, 3, 3>& R, const fvec<T, 3>& t) {
fmat<T, 4, 4> M4;
M4.set_col(0, fvec<T, 4>(R.col(0), 0));
M4.set_col(1, fvec<T, 4>(R.col(1), 0));
M4.set_col(2, fvec<T, 4>(R.col(2), 0));
M4.set_col(3, fvec<T, 4>(t, 1));
return M4;
}

/// construct 4x4 pose matrix from quaternion and translation vector
template <typename T> fmat<T, 4, 4>
pose4(const quaternion<T>& q, const fvec<T, 3>& t) {
fmat<T, 3, 3> R;
q.put_matrix(R);
return pose4<T>(R, t);
}

/// return rigid body transformation that performs look at transformation
template <typename T> fmat<T, 4, 4>
look_at4(const fvec<T, 3>& eye, const fvec<T, 3>& focus, const fvec<T, 3>& view_up_dir) {
Expand Down
7 changes: 5 additions & 2 deletions cgv/media/illum/textured_surface_material.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ textured_surface_material::textured_surface_material(
normal_index = -1;
bump_index = -1;
specular_index = -1;
sRGBA_textures = true;
sRGBA_textures = false;
}

/// set whether textures are interpreted in sRGB format
Expand All @@ -44,6 +44,7 @@ void textured_surface_material::set_sRGBA_textures(bool do_set)
/// convert obj material
textured_surface_material::textured_surface_material(const obj_material& obj_mat)
{
sRGBA_textures = false;
diffuse_index = -1;
roughness_index = -1;
metalness_index = -1;
Expand All @@ -57,7 +58,9 @@ textured_surface_material::textured_surface_material(const obj_material& obj_mat
set_name(obj_mat.get_name());
set_brdf_type(BrdfType(BT_LAMBERTIAN + BT_PHONG));
obj_material::color_type a = obj_mat.get_ambient(), d = obj_mat.get_diffuse();
set_ambient_occlusion(sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]) / sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]));
float la = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
float ld = sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
set_ambient_occlusion(la >= ld ? la : la/ld);
set_diffuse_reflectance(obj_mat.get_diffuse());
set_specular_reflectance(obj_mat.get_specular());
set_emission(obj_mat.get_emission());
Expand Down
1 change: 0 additions & 1 deletion cgv/media/image/bmp_reader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ bool bmp_reader::open(const std::string& file_name, data_format& df, std::vector
palette.clear();

fp = cgv::base::open_data_file(file_name.c_str(), "rb");
// fp = fopen(file_name.c_str(), "rb");
if (!fp) {
last_error = "could not open file: ";
last_error += file_name;
Expand Down
3 changes: 2 additions & 1 deletion cgv/media/mesh/obj_reader.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "obj_reader.h"
#include <cgv/utils/file.h>
#include <cgv/base/import.h>
#include <cgv/type/standard_types.h>
#include <cgv/utils/advanced_scan.h>
#include <cgv/utils/tokenizer.h>
Expand Down Expand Up @@ -177,7 +178,7 @@ template <typename T>
bool obj_reader_generic<T>::read_obj(const std::string& file_name)
{
std::string content;
if (!file::read(file_name, content, true))
if (!cgv::base::read_data_file(file_name, content, true))
return false;

path_name = file::get_path(file_name);
Expand Down
7 changes: 6 additions & 1 deletion cgv/render/clipped_view.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ void clipped_view::compute_clipping_planes(const cgv::render::view& view, double
if (z_min > z_near)
z_near_derived = z_min;
if (z_max > z_near)
z_far_derived = 1.2*z_max;
z_far_derived = z_max;

if (clip_relative_to_extent) {
z_near_derived *= z_near;
z_far_derived *= z_far;
}
}
else if (clip_relative_to_extent) {
z_near_derived = y_extent_at_focus*z_near;
Expand Down
32 changes: 32 additions & 0 deletions define_data_dir.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@echo off
setlocal EnableDelayedExpansion
%~d0
cd %~p0
set my_path=%~dp0
set cgvdir=%my_path:~0,-1%
set cgvdata=%cgvdir%\data
if [%1] == [] (
set param=""
) else (
set param=%1
)
set param=%param:"=%
if NOT [%1] == [] (
if EXIST "%param%\..\%~n1" (
call set cgvdata=%param%
) else (
echo only directories valid as data path argument
echo "%param%\..\%~n1" does not exist
pause
goto:eof
)
) else (
echo no cgv data directory dragged onto batch script
pause
goto:eof
)
call reg ADD HKEY_CURRENT_USER\Environment /v CGV_DATA /t REG_SZ /d "%cgvdata%" /f > nul 2> nul
bin\setx CGV_DUMMY ""
call reg DELETE HKEY_CURRENT_USER\Environment /v CGV_DUMMY /f >nul 2> nul
echo set CGV_DATA to %cgvdata%
pause
2 changes: 1 addition & 1 deletion define_support_dir.bat
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if NOT [%1] == [] (
goto:eof
)
) else (
echo no cgv project directory dragged onto batch script
echo no cgv support directory dragged onto batch script
pause
goto:eof
)
Expand Down
2 changes: 2 additions & 0 deletions libs/cgv_gl/gl/mesh_render_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace cgv {
mesh_render_info();
///
void destruct(cgv::render::context& ctx);
/// check whether info is constructed
bool is_constructed() const { return vbo.is_created(); }
///
void construct_base(cgv::render::context& c, const cgv::media::mesh::simple_mesh_base& mesh,
std::vector<idx_type>& vertex_indices, std::vector<vec3i>& unique_triples,
Expand Down
9 changes: 4 additions & 5 deletions libs/cgv_gl/glsl/textured_surface.glfs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ in vec3 position_eye;
in vec4 color_fs;
in vec2 texcoord_fs;

out vec4 frag_color;

//***** begin interface of fragment.glfs ***********************************
uniform float gamma = 2.2;
void finish_fragment(vec4 color);
//***** end interface of fragment.glfs ***********************************

//***** begin interface of surface.glsl ***********************************
struct Material {
Expand Down Expand Up @@ -43,7 +44,5 @@ void main()
if (!keep_this_side(position_eye, normal, side))
discard;

frag_color = compute_reflected_appearance_texture(position_eye, normal, texcoord_fs, color_fs, side);
// perform inverse gamma correction
frag_color.rgb = pow(frag_color.rgb, vec3(gamma));
finish_fragment(compute_reflected_appearance_texture(position_eye, normal, texcoord_fs, color_fs, side));
}
7 changes: 4 additions & 3 deletions libs/cgv_gl/glsl/textured_surface.glpr
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
files:textured_surface
fragment_file:lights.glsl
fragment_file:brdf.glsl
fragment_file:bump_map.glfs
vertex_file:brdf.glsl
vertex_file:view.glsl
fragment_file:fragment.glfs
fragment_file:side.glsl
fragment_file:surface.glsl
fragment_file:lights.glsl
fragment_file:brdf.glsl
fragment_file:bump_map.glfs
121 changes: 82 additions & 39 deletions plugins/crg_vr_view/vr_view_interactor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cgv/gui/trigger.h>
#include <cgv/signal/rebind.h>
#include <cgv/math/ftransform.h>
#include <cgv/base/import.h>
#include <cgv/math/inv.h>
#include <iostream>
#include <sstream>
Expand All @@ -26,6 +27,9 @@ vr_view_interactor::vr_view_interactor(const char* name) : stereo_view_interacto
fence_frequency = 5;
fence_line_width = 3;
show_vr_kits = true;
show_vr_kits_as_spheres = false;
show_vr_kits_as_meshes = true;

show_action_zone = false;
current_vr_handle = 0;
current_vr_handle_index = 0;
Expand Down Expand Up @@ -171,6 +175,21 @@ void vr_view_interactor::stream_stats(std::ostream& os)
bool vr_view_interactor::init(cgv::render::context& ctx)
{
cgv::render::ref_sphere_renderer(ctx, 1);
cgv::media::mesh::simple_mesh<float> M;
if (M.read(cgv::base::find_data_file("vr_controller_vive_1_5.obj", "D")))
MI_controller.construct(ctx, M);
cgv::media::mesh::simple_mesh<float> M2;
if (M2.read(cgv::base::find_data_file("generic_hmd.obj", "D")))
MI_hmd.construct(ctx, M2);

if (!MI_hmd.is_constructed() && !MI_controller.is_constructed()) {
show_vr_kits_as_meshes = false;
on_set(&show_vr_kits_as_meshes);
if (!show_vr_kits_as_spheres) {
show_vr_kits_as_spheres = true;
on_set(&show_vr_kits_as_spheres);
}
}
return stereo_view_interactor::init(ctx);
}

Expand Down Expand Up @@ -434,53 +453,75 @@ void vr_view_interactor::draw(cgv::render::context& ctx)
vr::vr_kit_state state;
vr::vr_kit_state* state_ptr = &state;
if (handle == current_vr_handle)
state_ptr = &kit_states[current_vr_handle_index-1];
state_ptr = &kit_states[current_vr_handle_index - 1];
else if (!kit_ptr->query_state(state, 1))
continue;
float left_eye_to_head[12];
float right_eye_to_head[12];
kit_ptr->put_eye_to_head_matrix(0, left_eye_to_head);
kit_ptr->put_eye_to_head_matrix(1, right_eye_to_head);
const mat3& R_w_h = reinterpret_cast<const mat3&>(state_ptr->hmd.pose[0]);
const vec3& p_w_h = reinterpret_cast<const vec3&>(state_ptr->hmd.pose[9]);

if (show_vr_kits_as_meshes && MI_hmd.is_constructed()) {
if (kit_ptr != rendered_kit_ptr) {
ctx.push_modelview_matrix();
ctx.mul_modelview_matrix(
cgv::math::pose4<float>(reinterpret_cast<const mat34&>(state_ptr->hmd.pose[0]))*
cgv::math::translate4<float>(0, 0.1f, -0.1f)
);
MI_hmd.render_mesh(ctx);
ctx.pop_modelview_matrix();
}
}
if (show_vr_kits_as_spheres || !MI_hmd.is_constructed()) {
float left_eye_to_head[12];
float right_eye_to_head[12];
kit_ptr->put_eye_to_head_matrix(0, left_eye_to_head);
kit_ptr->put_eye_to_head_matrix(1, right_eye_to_head);
const mat3& R_w_h = reinterpret_cast<const mat3&>(state_ptr->hmd.pose[0]);
const vec3& p_w_h = reinterpret_cast<const vec3&>(state_ptr->hmd.pose[9]);

const mat3& R_h_l = reinterpret_cast<const mat3&>(left_eye_to_head[0]);
const vec3& p_h_l = reinterpret_cast<const vec3&>(left_eye_to_head[9]);

const mat3& R_h_r = reinterpret_cast<const mat3&>(right_eye_to_head[0]);
const vec3& p_h_r = reinterpret_cast<const vec3&>(right_eye_to_head[9]);
const mat3& R_h_l = reinterpret_cast<const mat3&>(left_eye_to_head[0]);
const vec3& p_h_l = reinterpret_cast<const vec3&>(left_eye_to_head[9]);

vec4 s_l(0, 0, 0, 0.012f);
vec4 s_r = s_l;
reinterpret_cast<vec3&>(s_l) = R_w_h * p_h_l + p_w_h;
reinterpret_cast<vec3&>(s_r) = R_w_h * p_h_r + p_w_h;
const mat3& R_h_r = reinterpret_cast<const mat3&>(right_eye_to_head[0]);
const vec3& p_h_r = reinterpret_cast<const vec3&>(right_eye_to_head[9]);

if (kit_ptr != rendered_kit_ptr || rendered_eye != 0) {
spheres.push_back(s_l);
sphere_colors.push_back(rgb(1, 0, 0));
}
if (kit_ptr != rendered_kit_ptr || rendered_eye != 1) {
spheres.push_back(s_r);
sphere_colors.push_back(rgb(0, 0, 1));
vec4 s_l(0, 0, 0, 0.012f);
vec4 s_r = s_l;
reinterpret_cast<vec3&>(s_l) = R_w_h * p_h_l + p_w_h;
reinterpret_cast<vec3&>(s_r) = R_w_h * p_h_r + p_w_h;

if (kit_ptr != rendered_kit_ptr || rendered_eye != 0) {
spheres.push_back(s_l);
sphere_colors.push_back(rgb(1, 0, 0));
}
if (kit_ptr != rendered_kit_ptr || rendered_eye != 1) {
spheres.push_back(s_r);
sphere_colors.push_back(rgb(0, 0, 1));
}
}
for (unsigned i = 0; i < 2; ++i) {
const mat3& R_ci = reinterpret_cast<const mat3&>(state_ptr->controller[i].pose[0]);
const vec3& p_ci = reinterpret_cast<const vec3&>(state_ptr->controller[i].pose[9]);
spheres.push_back(vec4(p_ci, 0.04f));
spheres.push_back(vec4(p_ci + 0.05f*R_ci.col(0), 0.01f));
spheres.push_back(vec4(p_ci - 0.05f*R_ci.col(0), 0.01f));
spheres.push_back(vec4(p_ci + 0.05f*R_ci.col(1), 0.01f));
spheres.push_back(vec4(p_ci - 0.05f*R_ci.col(1), 0.01f));
spheres.push_back(vec4(p_ci + 0.05f*R_ci.col(2), 0.01f));
spheres.push_back(vec4(p_ci - 0.05f*R_ci.col(2), 0.01f));
sphere_colors.push_back(rgb(0.5f + (1 - i)*0.5f, 0.5f, 0.5f + 0.5f*i));
sphere_colors.push_back(rgb(1, 0, 0));
sphere_colors.push_back(rgb(1, 0.5f, 0.5f));
sphere_colors.push_back(rgb(0, 1, 0));
sphere_colors.push_back(rgb(0.5f, 1, 0.5f));
sphere_colors.push_back(rgb(0, 0, 1));
sphere_colors.push_back(rgb(0.5f, 0.5f, 1));
if (show_vr_kits_as_meshes && MI_controller.is_constructed()) {
ctx.push_modelview_matrix();
ctx.mul_modelview_matrix(cgv::math::pose4<float>(reinterpret_cast<const mat34&>(state_ptr->controller[i].pose[0])));
MI_controller.render_mesh(ctx);
ctx.pop_modelview_matrix();
}
if (show_vr_kits_as_spheres || !MI_controller.is_constructed()) {
const mat3& R_ci = reinterpret_cast<const mat3&>(state_ptr->controller[i].pose[0]);
const vec3& p_ci = reinterpret_cast<const vec3&>(state_ptr->controller[i].pose[9]);
spheres.push_back(vec4(p_ci, 0.04f));
spheres.push_back(vec4(p_ci + 0.05f*R_ci.col(0), 0.01f));
spheres.push_back(vec4(p_ci - 0.05f*R_ci.col(0), 0.01f));
spheres.push_back(vec4(p_ci + 0.05f*R_ci.col(1), 0.01f));
spheres.push_back(vec4(p_ci - 0.05f*R_ci.col(1), 0.01f));
spheres.push_back(vec4(p_ci + 0.05f*R_ci.col(2), 0.01f));
spheres.push_back(vec4(p_ci - 0.05f*R_ci.col(2), 0.01f));
sphere_colors.push_back(rgb(0.5f + (1 - i)*0.5f, 0.5f, 0.5f + 0.5f*i));
sphere_colors.push_back(rgb(1, 0, 0));
sphere_colors.push_back(rgb(1, 0.5f, 0.5f));
sphere_colors.push_back(rgb(0, 1, 0));
sphere_colors.push_back(rgb(0.5f, 1, 0.5f));
sphere_colors.push_back(rgb(0, 0, 1));
sphere_colors.push_back(rgb(0.5f, 0.5f, 1));
}
}
}
if (!spheres.empty()) {
Expand Down Expand Up @@ -558,7 +599,9 @@ void vr_view_interactor::create_gui()
align("\b");
end_tree_node(fence_color1);
}
add_member_control(this, "show_vr_kits", show_vr_kits, "check");
add_member_control(this, "show kits", show_vr_kits, "toggle", "w=62", " ");
add_member_control(this, "as meshes", show_vr_kits_as_meshes, "toggle", "w=61", " ");
add_member_control(this, "as spheres", show_vr_kits_as_spheres, "toggle", "w=61");
if (begin_tree_node("sphere styles", srs, false, "level=3")) {
align("\a");
add_gui("sphere style", srs);
Expand Down
7 changes: 7 additions & 0 deletions plugins/crg_vr_view/vr_view_interactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cg_vr/vr_server.h>
#include <cgv_gl/box_renderer.h>
#include <cgv_gl/sphere_renderer.h>
#include <cgv_gl/gl/mesh_render_info.h>
#include <stereo_view_interactor.h>

#include "lib_begin.h"
Expand Down Expand Up @@ -54,6 +55,8 @@ bool your_class::init(cgv::render::context& ctx)
class CGV_API vr_view_interactor :
public stereo_view_interactor
{
public:
typedef cgv::math::fmat<float,3,4> mat34;
protected:
/// whether the window shows a separate view onto the scene or the one of the current vr kit
bool separate_view;
Expand All @@ -65,6 +68,8 @@ class CGV_API vr_view_interactor :
int rendered_eye;
vr::vr_kit* rendered_kit_ptr;
int rendered_kit_index;
cgv::render::mesh_render_info MI_controller, MI_hmd;

cgv::gui::VREventTypeFlags event_flags;
static dmat4 hmat_from_pose(float pose_matrix[12]);

Expand All @@ -73,6 +78,8 @@ class CGV_API vr_view_interactor :

// visualization of kits and action zone
bool show_vr_kits;
bool show_vr_kits_as_spheres;
bool show_vr_kits_as_meshes;
bool show_action_zone;
rgb fence_color1, fence_color2;
float fence_frequency;
Expand Down
Loading

0 comments on commit 427a49f

Please sign in to comment.