deepstone
diff firstp/firstp.c @ 17:1e9f0b3616fa
fixed the matrix multiplication order
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 30 Nov 2011 00:04:16 +0200 |
parents | be61704c4cc8 |
children | c10f62b2bd56 |
line diff
1.1 --- a/firstp/firstp.c Tue Nov 29 07:23:57 2011 +0200 1.2 +++ b/firstp/firstp.c Wed Nov 30 00:04:16 2011 +0200 1.3 @@ -23,7 +23,7 @@ 1.4 static void sighandler(int s); 1.5 1.6 1.7 -static float cam_x, cam_y, cam_z = 10; 1.8 +static float cam_x, cam_y, cam_z; 1.9 static float cam_theta, cam_phi; 1.10 1.11 static float walk_speed = 0.1; 1.12 @@ -111,9 +111,10 @@ 1.13 1.14 mgl_matrix_mode(MGL_MODELVIEW); 1.15 mgl_load_identity(); 1.16 + mgl_rotate(cam_phi, 1, 0, 0); 1.17 mgl_rotate(cam_theta, 0, 1, 0); 1.18 - mgl_rotate(cam_phi, 1, 0, 0); 1.19 mgl_translate(-cam_x, -cam_y, -cam_z); 1.20 + mgl_translate(0, -2.0, 0); 1.21 1.22 mgl_light_intensity(0, 1.0); 1.23 mgl_light_position(0, 0, 5, 0, 1); 1.24 @@ -124,6 +125,14 @@ 1.25 copy_frame(fbuf); 1.26 } 1.27 1.28 +#define DEG_TO_RAD(x) (M_PI * (x) / 180.0) 1.29 +void cam_move(float dx, float dy) 1.30 +{ 1.31 + float angle = DEG_TO_RAD(cam_theta); 1.32 + cam_x += cos(angle) * dx + sin(angle) * dy; 1.33 + cam_z -= -sin(angle) * dx + cos(angle) * dy; 1.34 +} 1.35 + 1.36 static int proc_events(void) 1.37 { 1.38 static int prev_mx, prev_my, prev_bnmask; 1.39 @@ -150,35 +159,24 @@ 1.40 1.41 static int keyb(int key) 1.42 { 1.43 - float dir_x, dir_y, right_x, right_y; 1.44 - 1.45 - dir_x = sin(DEG2RAD(cam_theta)) * walk_speed; 1.46 - dir_y = cos(DEG2RAD(cam_theta)) * walk_speed; 1.47 - right_x = dir_y; 1.48 - right_y = -dir_x; 1.49 - 1.50 switch(key) { 1.51 case 27: 1.52 return 0; 1.53 1.54 case 'w': 1.55 - cam_x += dir_x; 1.56 - cam_z -= dir_y; 1.57 + cam_move(0, walk_speed); 1.58 break; 1.59 1.60 case 's': 1.61 - cam_x -= dir_x; 1.62 - cam_z += dir_y; 1.63 + cam_move(0, -walk_speed); 1.64 break; 1.65 1.66 case 'a': 1.67 - cam_x -= right_x; 1.68 - cam_z += right_y; 1.69 + cam_move(-walk_speed, 0); 1.70 break; 1.71 1.72 case 'd': 1.73 - cam_x += right_x; 1.74 - cam_z -= right_y; 1.75 + cam_move(walk_speed, 0); 1.76 break; 1.77 1.78 case '`':