2 #include "cgmath/cgmath.h"
9 static void draw_level(void);
13 int win_width, win_height;
18 float cam_theta, cam_phi, cam_dist = 10;
19 float view_matrix[16], proj_matrix[16];
25 if(init_opengl() == -1) {
29 glEnable(GL_DEPTH_TEST);
30 glEnable(GL_CULL_FACE);
32 if(!(sdr_foo = create_program_load("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
35 glBindAttribLocation(sdr_foo, MESH_ATTR_VERTEX, "apos");
36 glBindAttribLocation(sdr_foo, MESH_ATTR_NORMAL, "anorm");
37 glBindAttribLocation(sdr_foo, MESH_ATTR_TANGENT, "atang");
38 glBindAttribLocation(sdr_foo, MESH_ATTR_TEXCOORD, "atex");
39 link_program(sdr_foo);
41 if(load_level(&lvl, "data/test.lvl") == -1) {
48 void game_shutdown(void)
51 free_program(sdr_foo);
54 void game_display(void)
56 glClearColor(0.1, 0.1, 0.1, 1);
57 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59 cgm_midentity(proj_matrix);
60 cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), win_aspect, 0.5, 500.0);
61 glMatrixMode(GL_PROJECTION);
62 glLoadMatrixf(proj_matrix);
64 cgm_midentity(view_matrix);
65 cgm_mpretranslate(view_matrix, 0, -1.7, -cam_dist);
66 cgm_mprerotate(view_matrix, cam_phi, 1, 0, 0);
67 cgm_mprerotate(view_matrix, cam_theta, 0, 1, 0);
68 glMatrixMode(GL_MODELVIEW);
69 glLoadMatrixf(view_matrix);
74 assert(glGetError() == GL_NO_ERROR);
77 static void draw_level(void)
82 glUseProgram(sdr_foo);
85 for(i=0; i<lvl.height; i++) {
86 for(j=0; j<lvl.width; j++) {
87 for(k=0; k<cell->num_mgrp; k++) {
88 draw_meshgroup(cell->mgrp + k);
97 void game_reshape(int x, int y)
99 glViewport(0, 0, x, y);
102 win_aspect = (float)x / (float)y;
105 void game_keyboard(int key, int press)
107 if(press && key == 27) {
113 void game_mbutton(int bn, int press, int x, int y)
120 void game_mmotion(int x, int y)
122 int dx = x - mouse_x;
123 int dy = y - mouse_y;
127 if(!(dx | dy)) return;
130 cam_theta += cgm_deg_to_rad(dx * 0.5f);
131 cam_phi += cgm_deg_to_rad(dy * 0.5f);
132 if(cam_phi < -M_PI/2) cam_phi = -M_PI/2;
133 if(cam_phi > M_PI/2) cam_phi = M_PI/2;
136 cam_dist += dy * 0.1;
137 if(cam_dist < 0) cam_dist = 0;