moving functionality into the avatar class, and drawing hands
[laserbrain_demo] / src / app.cc
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)