proper gamepad control
[laserbrain_demo] / src / app.cc
index 9245f06..e9a4781 100644 (file)
@@ -25,6 +25,8 @@ float win_aspect;
 bool fb_srgb;
 bool opt_gear_wireframe;
 
+unsigned int sdr_ltmap, sdr_ltmap_notex;
+
 static float cam_dist = 0.0;
 static float cam_theta, cam_phi = 20;
 static Vec3 cam_pos;
@@ -40,11 +42,13 @@ static bool have_headtracking, should_swap;
 static int prev_mx, prev_my;
 static bool bnstate[8];
 static bool keystate[256];
+static Vec2 joy_move, joy_look;
+static float joy_deadzone = 0.01;
 
 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
 static TextureSet texman;
 static Scene *scn;
-static unsigned int sdr, sdr_post_gamma;
+static unsigned int sdr_post_gamma;
 
 static long prev_msec;
 
@@ -92,7 +96,7 @@ bool app_init(int argc, char **argv)
        glClearColor(0.2, 0.2, 0.2, 1.0);
 
        scn = new Scene(&texman);
-       if(!load_scene(scn, "data/museum.scene")) {
+       if(!load_scene(scn, opt.scenefile ? opt.scenefile : "data/museum.scene")) {
                return false;
        }
 
@@ -106,12 +110,15 @@ bool app_init(int argc, char **argv)
                cam_pos = bcent + Vec3(0, user_eye_height, 0);
        }
 
-       if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
-               fprintf(stderr, "failed to load test shaders\n");
+       if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
+               return false;
+       }
+
+       if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
                return false;
        }
-       set_uniform_int(sdr, "texmap", 0);
-       set_uniform_int(sdr, "lightmap", 1);
+       set_uniform_int(sdr_ltmap, "texmap", 0);
+       set_uniform_int(sdr_ltmap, "lightmap", 1);
 
        if(!fb_srgb) {
                sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
@@ -161,6 +168,31 @@ static void update(float dt)
        float speed = walk_speed * dt;
        Vec3 dir;
 
+       // joystick
+       float jdeadsq = joy_deadzone * joy_deadzone;
+       float jmove_lensq = length_sq(joy_move);
+       float jlook_lensq = length_sq(joy_look);
+
+       if(jmove_lensq > jdeadsq) {
+               float len = sqrt(jmove_lensq);
+               jmove_lensq -= jdeadsq;
+
+               float mag = len * len;
+               dir.x += mag * joy_move.x / len * 2.0 * speed;
+               dir.z += mag * joy_move.y / len * 2.0 * speed;
+       }
+       if(jlook_lensq > jdeadsq) {
+               float len = sqrt(jlook_lensq);
+               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;
+       }
+
+       // keyboard move
        if(keystate[(int)'w']) {
                dir.z -= speed;
        }
@@ -295,9 +327,7 @@ static void draw_scene()
        set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
        set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
 
-       glUseProgram(sdr);
        scn->draw();
-       glUseProgram(0);
 
        if(show_walk_mesh && scn->walk_mesh) {
                glPushAttrib(GL_ENABLE_BIT);
@@ -306,6 +336,8 @@ static void draw_scene()
                glDisable(GL_LIGHTING);
                glEnable(GL_POLYGON_OFFSET_FILL);
 
+               glUseProgram(0);
+
                glPolygonOffset(-1, 1);
                glDepthMask(0);
 
@@ -337,8 +369,11 @@ void app_keyboard(int key, bool pressed)
                        app_quit();
                        break;
 
-               case 'f':
-                       app_toggle_fullscreen();
+               case '\n':
+               case '\r':
+                       if(mod & MOD_ALT) {
+                               app_toggle_fullscreen();
+                       }
                        break;
 
                case '`':
@@ -360,6 +395,23 @@ void app_keyboard(int key, bool pressed)
                        }
                        break;
 
+               case 'f':
+                       {
+                               static float prev_walk_speed = -1.0;
+                               if(prev_walk_speed < 0.0) {
+                                       noclip = true;
+                                       prev_walk_speed = walk_speed;
+                                       walk_speed = 1000.0;
+                                       show_message("fly mode\n");
+                               } else {
+                                       noclip = false;
+                                       walk_speed = prev_walk_speed;
+                                       prev_walk_speed = -1.0;
+                                       show_message("walk mode\n");
+                               }
+                       }
+                       break;
+
                case 'p':
                        if(mod & MOD_CTRL) {
                                fb_srgb = !fb_srgb;
@@ -442,3 +494,26 @@ void app_mouse_delta(int dx, int dy)
                mouse_look(dx * mouse_speed, dy * mouse_speed);
        }
 }
+
+void app_gamepad_axis(int axis, float val)
+{
+       switch(axis) {
+       case 0:
+               joy_move.x = val;
+               break;
+       case 1:
+               joy_move.y = val;
+               break;
+
+       case 2:
+               joy_look.x = val;
+               break;
+       case 3:
+               joy_look.y = val;
+               break;
+       }
+}
+
+void app_gamepad_button(int bn, bool pressed)
+{
+}