From 2c648dbdf80ad77015e3283beb028cd389c2d2fa Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 21 Sep 2017 22:01:39 +0300 Subject: [PATCH] moving functionality into the avatar class, and drawing hands --- src/app.cc | 63 +++++++++++++++++++++++++++++--------------------------- src/avatar.cc | 7 +++++++ src/avatar.h | 5 +---- src/vrinput.cc | 10 ++++++++- src/vrinput.h | 3 ++- 5 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 src/avatar.cc diff --git a/src/app.cc b/src/app.cc index 17ab2fd..dc87024 100644 --- a/src/app.cc +++ b/src/app.cc @@ -14,6 +14,7 @@ #include "opt.h" #include "post.h" #include "renderer.h" +#include "avatar.h" #include "vrinput.h" #include "exman.h" #include "blob_exhibit.h" @@ -36,9 +37,9 @@ SceneSet sceneman; unsigned int sdr_ltmap, sdr_ltmap_notex; +static Avatar avatar; + static float cam_dist = 0.0; -static float cam_theta, cam_phi; -static Vec3 cam_pos; static float floor_y; // last floor height static float user_eye_height = 165; @@ -122,10 +123,10 @@ bool app_init(int argc, char **argv) return false; } - cam_pos = mscn->start_pos; + avatar.pos = mscn->start_pos; Vec3 dir = rotate(Vec3(0, 0, 1), mscn->start_rot); dir.y = 0; - cam_theta = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1)))); + avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1)))); exman = new ExhibitManager; if(!exman->load(mscn, "data/exhibits")) { @@ -231,10 +232,10 @@ static void update(float dt) jlook_lensq -= jdeadsq; float mag = len * len; - cam_theta += mag * joy_look.x / len * 200.0 * dt; - cam_phi += mag * joy_look.y / len * 100.0 * dt; - if(cam_phi < -90.0f) cam_phi = -90.0f; - if(cam_phi > 90.0f) cam_phi = 90.0f; + avatar.body_rot += mag * joy_look.x / len * 200.0 * dt; + avatar.head_alt += mag * joy_look.y / len * 100.0 * dt; + if(avatar.head_alt < -90.0f) avatar.head_alt = -90.0f; + if(avatar.head_alt > 90.0f) avatar.head_alt = 90.0f; } // keyboard move @@ -251,55 +252,56 @@ static void update(float dt) dir.x -= speed; } if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) { - cam_pos.y += speed; + avatar.pos.y += speed; } if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) { - cam_pos.y -= speed; + avatar.pos.y -= speed; } - float theta = M_PI * cam_theta / 180.0f; + float theta = M_PI * avatar.body_rot / 180.0f; Vec3 newpos; - newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z; - newpos.y = cam_pos.y; - newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z; + newpos.x = avatar.pos.x + cos(theta) * dir.x - sin(theta) * dir.z; + newpos.y = avatar.pos.y; + newpos.z = avatar.pos.z + sin(theta) * dir.x + cos(theta) * dir.z; if(noclip) { - cam_pos = newpos; + avatar.pos = newpos; } else { - if(!constrain_walk_mesh(newpos, &cam_pos)) { + if(!constrain_walk_mesh(newpos, &avatar.pos)) { float dtheta = M_PI / 32.0; float theta = dtheta; - Vec2 dir2d = newpos.xz() - cam_pos.xz(); + Vec2 dir2d = newpos.xz() - avatar.pos.xz(); for(int i=0; i<16; i++) { Vec2 dvec = rotate(dir2d, theta); - Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y); - if(constrain_walk_mesh(pos, &cam_pos)) { + Vec3 pos = avatar.pos + Vec3(dvec.x, 0, dvec.y); + if(constrain_walk_mesh(pos, &avatar.pos)) { break; } dvec = rotate(dir2d, -theta); - pos = cam_pos + Vec3(dvec.x, 0, dvec.y); - if(constrain_walk_mesh(pos, &cam_pos)) { + pos = avatar.pos + Vec3(dvec.x, 0, dvec.y); + if(constrain_walk_mesh(pos, &avatar.pos)) { break; } theta += dtheta; } } - floor_y = cam_pos.y - user_eye_height; + floor_y = avatar.pos.y - user_eye_height; } + // TODO move to avatar // calculate mouselook view matrix mouse_view_matrix = Mat4::identity; mouse_view_matrix.pre_translate(0, 0, -cam_dist); if(!have_headtracking) { - mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi)); + mouse_view_matrix.pre_rotate_x(deg_to_rad(avatar.head_alt)); } - mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta)); - mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z); + mouse_view_matrix.pre_rotate_y(deg_to_rad(avatar.body_rot)); + mouse_view_matrix.pre_translate(-avatar.pos.x, -avatar.pos.y, -avatar.pos.z); // update hand-tracking if(have_handtracking) { - update_vrhands(); + update_vrhands(&avatar); } } @@ -364,6 +366,7 @@ void app_display() glLoadMatrixf(view_matrix[0]); draw_scene(); + draw_vrhands(); // XXX if(!fb_srgb && sdr_post_gamma) { slow_post(sdr_post_gamma); @@ -547,11 +550,11 @@ void app_mouse_button(int bn, bool pressed, int x, int y) static inline void mouse_look(float dx, float dy) { float scrsz = (float)win_height; - cam_theta += dx * 512.0 / scrsz; - cam_phi += dy * 512.0 / scrsz; + avatar.body_rot += dx * 512.0 / scrsz; + avatar.head_alt += dy * 512.0 / scrsz; - if(cam_phi < -90) cam_phi = -90; - if(cam_phi > 90) cam_phi = 90; + if(avatar.head_alt < -90) avatar.head_alt = -90; + if(avatar.head_alt > 90) avatar.head_alt = 90; } static void mouse_zoom(float dx, float dy) diff --git a/src/avatar.cc b/src/avatar.cc new file mode 100644 index 0000000..0986d8a --- /dev/null +++ b/src/avatar.cc @@ -0,0 +1,7 @@ +#include "avatar.h" + +Avatar::Avatar() +{ + body_rot = 0; + head_alt = 0; +} diff --git a/src/avatar.h b/src/avatar.h index 6c3521e..ca3ea8c 100644 --- a/src/avatar.h +++ b/src/avatar.h @@ -13,12 +13,9 @@ public: Vec3 pos; float body_rot; Quat head_rot; // used when head-tracking - float head_tilt; // used for mouselook + float head_alt; // used for mouselook Avatar(); - ~Avatar(); - - void draw() const; }; #endif // AVATAR_H_ diff --git a/src/vrinput.cc b/src/vrinput.cc index 2a25f53..d5692b8 100644 --- a/src/vrinput.cc +++ b/src/vrinput.cc @@ -2,6 +2,7 @@ #include #include "vrinput.h" #include "scene.h" +#include "shader.h" VRHand vrhand[2]; @@ -13,6 +14,9 @@ bool init_vrhands() if(!(scn->load("data/vrhands.obj"))) { return false; } + scn->objects[0]->node->set_position(Vec3(0, 150, 0)); + scn->objects[1]->node->set_position(Vec3(0, 250, 0)); + scn->update(0); return true; } @@ -22,7 +26,7 @@ void destroy_vrhands() scn = 0; } -void update_vrhands() +void update_vrhands(const Avatar *avatar) { for(int i=0; i<2; i++) { if(goatvr_hand_active(i)) { @@ -33,8 +37,12 @@ void update_vrhands() vrhand[i].valid = false; } } + + scn->update(0); } void draw_vrhands() { + bind_shader(0); + scn->draw(); } diff --git a/src/vrinput.h b/src/vrinput.h index 48ac1e3..67b2640 100644 --- a/src/vrinput.h +++ b/src/vrinput.h @@ -3,6 +3,7 @@ #include #include +#include "avatar.h" struct VRHand { bool valid; @@ -16,7 +17,7 @@ extern VRHand vrhand[2]; bool init_vrhands(); void destroy_vrhands(); -void update_vrhands(); +void update_vrhands(const Avatar *avatar); void draw_vrhands(); #endif /* VRINPUT_H_ */ -- 1.7.10.4