moving functionality into the avatar class, and drawing hands
authorJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 21 Sep 2017 19:01:39 +0000 (22:01 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 21 Sep 2017 19:01:39 +0000 (22:01 +0300)
src/app.cc
src/avatar.cc [new file with mode: 0644]
src/avatar.h
src/vrinput.cc
src/vrinput.h

index 17ab2fd..dc87024 100644 (file)
@@ -14,6 +14,7 @@
 #include "opt.h"
 #include "post.h"
 #include "renderer.h"
 #include "opt.h"
 #include "post.h"
 #include "renderer.h"
+#include "avatar.h"
 #include "vrinput.h"
 #include "exman.h"
 #include "blob_exhibit.h"
 #include "vrinput.h"
 #include "exman.h"
 #include "blob_exhibit.h"
@@ -36,9 +37,9 @@ SceneSet sceneman;
 
 unsigned int sdr_ltmap, sdr_ltmap_notex;
 
 
 unsigned int sdr_ltmap, sdr_ltmap_notex;
 
+static Avatar avatar;
+
 static float cam_dist = 0.0;
 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;
 
 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;
        }
 
                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;
        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")) {
 
        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;
                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
        }
 
        // keyboard move
@@ -251,55 +252,56 @@ static void update(float dt)
                dir.x -= speed;
        }
        if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
                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]) {
        }
        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;
        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) {
 
        if(noclip) {
-               cam_pos = newpos;
+               avatar.pos = newpos;
        } else {
        } else {
-               if(!constrain_walk_mesh(newpos, &cam_pos)) {
+               if(!constrain_walk_mesh(newpos, &avatar.pos)) {
                        float dtheta = M_PI / 32.0;
                        float theta = dtheta;
                        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);
 
                        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);
                                        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;
                        }
                }
                                        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) {
        // 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 hand-tracking
        if(have_handtracking) {
-               update_vrhands();
+               update_vrhands(&avatar);
        }
 }
 
        }
 }
 
@@ -364,6 +366,7 @@ void app_display()
                glLoadMatrixf(view_matrix[0]);
 
                draw_scene();
                glLoadMatrixf(view_matrix[0]);
 
                draw_scene();
+               draw_vrhands(); // XXX
 
                if(!fb_srgb && sdr_post_gamma) {
                        slow_post(sdr_post_gamma);
 
                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;
 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)
 }
 
 static void mouse_zoom(float dx, float dy)
diff --git a/src/avatar.cc b/src/avatar.cc
new file mode 100644 (file)
index 0000000..0986d8a
--- /dev/null
@@ -0,0 +1,7 @@
+#include "avatar.h"
+
+Avatar::Avatar()
+{
+       body_rot = 0;
+       head_alt = 0;
+}
index 6c3521e..ca3ea8c 100644 (file)
@@ -13,12 +13,9 @@ public:
        Vec3 pos;
        float body_rot;
        Quat head_rot;          // used when head-tracking
        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();
-       ~Avatar();
-
-       void draw() const;
 };
 
 #endif // AVATAR_H_
 };
 
 #endif // AVATAR_H_
index 2a25f53..d5692b8 100644 (file)
@@ -2,6 +2,7 @@
 #include <goatvr.h>
 #include "vrinput.h"
 #include "scene.h"
 #include <goatvr.h>
 #include "vrinput.h"
 #include "scene.h"
+#include "shader.h"
 
 VRHand vrhand[2];
 
 
 VRHand vrhand[2];
 
@@ -13,6 +14,9 @@ bool init_vrhands()
        if(!(scn->load("data/vrhands.obj"))) {
                return false;
        }
        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;
 }
 
        return true;
 }
 
@@ -22,7 +26,7 @@ void destroy_vrhands()
        scn = 0;
 }
 
        scn = 0;
 }
 
-void update_vrhands()
+void update_vrhands(const Avatar *avatar)
 {
        for(int i=0; i<2; i++) {
                if(goatvr_hand_active(i)) {
 {
        for(int i=0; i<2; i++) {
                if(goatvr_hand_active(i)) {
@@ -33,8 +37,12 @@ void update_vrhands()
                        vrhand[i].valid = false;
                }
        }
                        vrhand[i].valid = false;
                }
        }
+
+       scn->update(0);
 }
 
 void draw_vrhands()
 {
 }
 
 void draw_vrhands()
 {
+       bind_shader(0);
+       scn->draw();
 }
 }
index 48ac1e3..67b2640 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <gmath/gmath.h>
 #include <goatvr.h>
 
 #include <gmath/gmath.h>
 #include <goatvr.h>
+#include "avatar.h"
 
 struct VRHand {
        bool valid;
 
 struct VRHand {
        bool valid;
@@ -16,7 +17,7 @@ extern VRHand vrhand[2];
 bool init_vrhands();
 void destroy_vrhands();
 
 bool init_vrhands();
 void destroy_vrhands();
 
-void update_vrhands();
+void update_vrhands(const Avatar *avatar);
 void draw_vrhands();
 
 #endif /* VRINPUT_H_ */
 void draw_vrhands();
 
 #endif /* VRINPUT_H_ */