X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fscr_game.c;fp=src%2Fscr_game.c;h=98f5d7631bb2d57d6a4192515f47bde0a96da318;hb=974e63caa567fb56d5fa08ead1ffd7282fb910fd;hp=d91e76570131e00e6edb5d394a574063c379fddc;hpb=ed53cdd30c05b7b3f8d0ba6ef2b0668f37d90119;p=deeprace diff --git a/src/scr_game.c b/src/scr_game.c index d91e765..98f5d76 100644 --- a/src/scr_game.c +++ b/src/scr_game.c @@ -6,6 +6,8 @@ #include "game.h" #include "util.h" #include "goat3d.h" +#include "input.h" +#include "cgmath/cgmath.h" static int ginit(void); static void gdestroy(void); @@ -17,6 +19,10 @@ static void gkeyb(int key, int press); static void gmouse(int bn, int press, int x, int y); static void gmotion(int x, int y); +static void set_light_dir(int idx, float x, float y, float z); +static void set_light_color(int idx, float r, float g, float b, float s); + + struct game_screen scr_game = { "game", ginit, gdestroy, @@ -25,8 +31,8 @@ struct game_screen scr_game = { gkeyb, gmouse, gmotion }; -static float cam_theta, cam_phi = 20, cam_dist = 10; -static float cam_pan[3]; +static float cam_theta, cam_phi = 20, cam_dist; +static cgm_vec3 cam_pan; static struct goat3d *gscn; static int dlist; @@ -97,6 +103,16 @@ static void gdestroy(void) static int gstart(void) { + float amb[] = {0.25, 0.25, 0.25, 1}; + + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); + + set_light_color(0, 1, 1, 1, 0.8); + glEnable(GL_LIGHT0); + set_light_color(1, 1, 0.6, 0.5, 0.2); + glEnable(GL_LIGHT1); + set_light_color(2, 0.5, 0.6, 1, 0.3); + glEnable(GL_LIGHT2); return 0; } @@ -104,14 +120,68 @@ static void gstop(void) { } +#define TSTEP (1.0f / 30.0f) + +static void gupdate(void) +{ + if(inpstate & INP_MOVE_BITS) { + cgm_vec3 fwd, right; + float theta = cam_theta * M_PI / 180.0f; + float phi = cam_phi * M_PI / 180.0f; + + float dx = 0, dy = 0; + + fwd.x = -sin(theta) * cos(phi); + fwd.y = sin(phi); + fwd.z = cos(theta) * cos(phi); + right.x = cos(theta); + right.y = 0; + right.z = sin(theta); + + if(inpstate & INP_FWD_BIT) { + dy += 0.1; + } + if(inpstate & INP_BACK_BIT) { + dy -= 0.1; + } + if(inpstate & INP_RIGHT_BIT) { + dx -= 0.1; + } + if(inpstate & INP_LEFT_BIT) { + dx += 0.1; + } + + cam_pan.x += right.x * dx + fwd.x * dy; + cam_pan.y += fwd.y * dy; + cam_pan.z += right.z * dx + fwd.z * dy; + } +} + static void gdisplay(void) { + static long prev_msec; + static float tm_acc; + long msec; + + msec = glutGet(GLUT_ELAPSED_TIME); + tm_acc += (float)(msec - prev_msec) / 1000.0f; + prev_msec = msec; + + while(tm_acc >= TSTEP) { + gupdate(); + tm_acc -= TSTEP; + } + glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0, 0, -cam_dist); glRotatef(cam_phi, 1, 0, 0); glRotatef(cam_theta, 0, 1, 0); - glTranslatef(cam_pan[0], cam_pan[1], cam_pan[2]); + glTranslatef(cam_pan.x, cam_pan.y, cam_pan.z); + + set_light_dir(0, -1, 1, 5); + set_light_dir(1, 5, 0, 3); + set_light_dir(2, -0.5, -2, -3); glCallList(dlist); } @@ -122,6 +192,18 @@ static void greshape(int x, int y) static void gkeyb(int key, int press) { + int i; + + for(i=0; i 90) cam_phi = 90; } + /* if(mouse_state[1]) { float up[3], right[3]; float theta = cam_theta * M_PI / 180.0f; @@ -157,8 +240,31 @@ static void gmotion(int x, int y) cam_pan[1] += up[1] * dy * 0.01; cam_pan[2] += (right[2] * dx + up[2] * dy) * 0.01; } + */ if(mouse_state[2]) { cam_dist += dy * 0.1; if(cam_dist < 0) cam_dist = 0; } } + +static void set_light_dir(int idx, float x, float y, float z) +{ + float pos[4]; + pos[0] = x; + pos[1] = y; + pos[2] = z; + pos[3] = 0; + glLightfv(GL_LIGHT0 + idx, GL_POSITION, pos); +} + +static void set_light_color(int idx, float r, float g, float b, float s) +{ + float color[4]; + color[0] = r * s; + color[1] = g * s; + color[2] = b * s; + color[3] = 1; + glLightfv(GL_LIGHT0 + idx, GL_DIFFUSE, color); + glLightfv(GL_LIGHT0 + idx, GL_SPECULAR, color); +} +