3 #include "cgmath/cgmath.h"
11 static void draw_level(void);
16 int win_width, win_height;
22 float view_matrix[16], proj_matrix[16];
28 if(init_opengl() == -1) {
32 glEnable(GL_DEPTH_TEST);
33 glEnable(GL_CULL_FACE);
35 if(!(sdr_foo = create_program_load("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
38 glBindAttribLocation(sdr_foo, MESH_ATTR_VERTEX, "apos");
39 glBindAttribLocation(sdr_foo, MESH_ATTR_NORMAL, "anorm");
40 glBindAttribLocation(sdr_foo, MESH_ATTR_TANGENT, "atang");
41 glBindAttribLocation(sdr_foo, MESH_ATTR_TEXCOORD, "atex");
42 link_program(sdr_foo);
44 if(load_level(&lvl, "data/test.lvl") == -1) {
57 void game_shutdown(void)
60 free_program(sdr_foo);
63 #define STEP_INTERVAL 1000
67 static long prev_step;
68 int step[][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
70 cgm_vec3 vdir = {0, 0, -1};
72 upd_player_xform(&player);
73 cgm_vmul_m3v3(&vdir, player.view_xform);
75 player.dir = (int)(2.0f * (atan2(vdir.z, vdir.x) + M_PI) / M_PI + 0.5f);
78 if(time_msec - prev_step >= STEP_INTERVAL) {
85 void game_display(void)
88 static long prev_msec;
90 dt = (prev_msec - time_msec) / 1000.0f;
91 prev_msec = time_msec;
95 glClearColor(0.1, 0.1, 0.1, 1);
96 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
98 cgm_midentity(proj_matrix);
99 cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), win_aspect, 0.5, 500.0);
100 glMatrixMode(GL_PROJECTION);
101 glLoadMatrixf(proj_matrix);
103 glMatrixMode(GL_MODELVIEW);
104 glLoadMatrixf(player.view_xform);
109 assert(glGetError() == GL_NO_ERROR);
112 static void draw_level(void)
118 glUseProgram(sdr_foo);
121 for(i=0; i<lvl.height; i++) {
122 for(j=0; j<lvl.width; j++) {
123 cgm_mtranslation(xform, j * lvl.cell_size, 0, i * lvl.cell_size);
126 glMultMatrixf(xform);
128 for(k=0; k<cell->num_mgrp; k++) {
129 draw_meshgroup(cell->mgrp + k);
140 void game_reshape(int x, int y)
142 glViewport(0, 0, x, y);
145 win_aspect = (float)x / (float)y;
148 void game_keyboard(int key, int press)
150 if(press && key == 27) {
156 void game_mbutton(int bn, int press, int x, int y)
163 void game_mmotion(int x, int y)
165 int dx = x - mouse_x;
166 int dy = y - mouse_y;
170 if(!(dx | dy)) return;
173 player.theta -= cgm_deg_to_rad(dx * 0.5f);
174 player.phi -= cgm_deg_to_rad(dy * 0.5f);
175 if(player.phi < -M_PI/2) player.phi = -M_PI/2;
176 if(player.phi > M_PI/2) player.phi = M_PI/2;
179 cam_dist += dy * 0.1;
180 if(cam_dist < 0) cam_dist = 0;