tavli
view src/game.cc @ 24:0aadb519b5ee
correct highlighting
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 08 Jul 2015 15:11:58 +0300 |
parents | c3fbf9616dbd |
children |
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include "opengl.h"
4 #include "game.h"
5 #include "board.h"
6 #include "scenery.h"
7 #include "sdr.h"
8 #include "shadow.h"
9 #include "opt.h"
11 static void draw_scene();
12 static void draw_backdrop();
14 int win_width, win_height;
15 unsigned long cur_time;
16 unsigned int sdr_phong, sdr_phong_notex;
17 unsigned int sdr_shadow, sdr_shadow_notex;
18 unsigned int sdr_unlit;
19 bool wireframe;
20 int dbg_int;
22 Ray pick_ray;
24 static Board board;
26 static float cam_theta, cam_phi = 45, cam_dist = 3;
27 static bool bnstate[8];
28 static int prev_x, prev_y;
30 static bool dbg_busyloop, dbg_show_shadowmap;
32 bool game_init()
33 {
34 if(init_opengl() == -1) {
35 return false;
36 }
38 glEnable(GL_DEPTH_TEST);
39 glEnable(GL_CULL_FACE);
40 glEnable(GL_NORMALIZE);
41 glEnable(GL_LIGHTING);
42 glEnable(GL_LIGHT0);
44 float amb[] = {0.3, 0.3, 0.3, 1.0};
45 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
47 if(glcaps.sep_spec) {
48 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
49 }
51 if(glcaps.fsaa) {
52 glEnable(GL_MULTISAMPLE);
53 }
55 if(glcaps.shaders) {
56 Mesh::use_custom_sdr_attr = false;
57 if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
58 return false;
59 }
60 if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
61 return false;
62 }
64 if(glcaps.fbo) {
65 init_shadow(2048);
67 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
68 return false;
69 }
70 set_uniform_int(sdr_shadow, "tex", 0);
71 set_uniform_int(sdr_shadow, "shadowmap", 1);
73 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
74 return false;
75 }
76 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
77 }
78 }
80 if(!board.init()) {
81 return false;
82 }
83 board.setup();
85 if(!init_scenery()) {
86 return false;
87 }
89 assert(glGetError() == GL_NO_ERROR);
90 return true;
91 }
93 void game_cleanup()
94 {
95 board.destroy();
96 destroy_scenery();
97 destroy_shadow();
98 }
100 void game_update(unsigned long time_msec)
101 {
102 cur_time = time_msec;
103 }
105 void game_display()
106 {
107 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109 glMatrixMode(GL_MODELVIEW);
110 glLoadIdentity();
111 glTranslatef(0, 0.1, -cam_dist);
112 glRotatef(cam_phi, 1, 0, 0);
113 glRotatef(cam_theta, 0, 1, 0);
115 float lpos[] = {-10, 20, 10, 1};
116 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
118 if(opt.shadows && sdr_shadow) {
119 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 4.5);
120 draw_scene();
121 end_shadow_pass();
123 glActiveTexture(GL_TEXTURE1);
124 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
126 glMatrixMode(GL_TEXTURE);
127 Matrix4x4 shadow_matrix = get_shadow_matrix();
128 glLoadTransposeMatrixf(shadow_matrix[0]);
130 glActiveTexture(GL_TEXTURE0);
131 glMatrixMode(GL_MODELVIEW);
133 draw_scene();
135 glActiveTexture(GL_TEXTURE1);
136 glBindTexture(GL_TEXTURE_2D, 0);
137 glActiveTexture(GL_TEXTURE0);
138 glBindTexture(GL_TEXTURE_2D, 0);
139 } else {
140 draw_scene();
141 }
143 if(dbg_show_shadowmap) {
144 glMatrixMode(GL_PROJECTION);
145 glPushMatrix();
146 glLoadIdentity();
147 glMatrixMode(GL_MODELVIEW);
148 glPushMatrix();
149 glLoadIdentity();
150 glMatrixMode(GL_TEXTURE);
151 glLoadIdentity();
153 glPushAttrib(GL_ENABLE_BIT);
154 glUseProgram(0);
155 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
157 glEnable(GL_TEXTURE_2D);
158 glDisable(GL_DEPTH_TEST);
159 glDisable(GL_LIGHTING);
160 glDisable(GL_BLEND);
162 glBegin(GL_QUADS);
163 glColor4f(1, 1, 1, 1);
164 glTexCoord2f(0, 0); glVertex2f(-1, -1);
165 glTexCoord2f(1, 0); glVertex2f(1, -1);
166 glTexCoord2f(1, 1); glVertex2f(1, 1);
167 glTexCoord2f(0, 1); glVertex2f(-1, 1);
168 glEnd();
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
171 glBindTexture(GL_TEXTURE_2D, 0);
173 glPopAttrib();
175 glMatrixMode(GL_PROJECTION);
176 glPopMatrix();
177 glMatrixMode(GL_MODELVIEW);
178 glPopMatrix();
179 }
181 if(dbg_busyloop) {
182 redisplay();
183 }
184 }
186 static void draw_scene()
187 {
188 draw_backdrop();
189 draw_scenery();
190 board.draw();
191 }
193 static void draw_backdrop()
194 {
195 glPushAttrib(GL_ENABLE_BIT);
196 glDisable(GL_LIGHTING);
197 glDisable(GL_DEPTH_TEST);
199 glMatrixMode(GL_PROJECTION);
200 glPushMatrix();
201 glLoadIdentity();
202 glMatrixMode(GL_MODELVIEW);
203 glPushMatrix();
204 glLoadIdentity();
206 glBegin(GL_QUADS);
207 glColor3f(0.9, 0.8, 0.6);
208 glVertex2f(-1, -1);
209 glVertex2f(1, -1);
210 glColor3f(0.4, 0.5, 0.8);
211 glVertex2f(1, 1);
212 glVertex2f(-1, 1);
213 glEnd();
215 glMatrixMode(GL_PROJECTION);
216 glPopMatrix();
217 glMatrixMode(GL_MODELVIEW);
218 glPopMatrix();
220 glPopAttrib();
221 }
223 void game_reshape(int x, int y)
224 {
225 glMatrixMode(GL_PROJECTION);
226 glLoadIdentity();
227 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
229 glViewport(0, 0, x, y);
230 }
232 void game_keyboard(int bn, bool press)
233 {
234 if(press) {
235 switch(bn) {
236 case 27:
237 quit();
239 case 'w':
240 wireframe = !wireframe;
241 redisplay();
242 break;
244 case 'd':
245 dbg_busyloop = !dbg_busyloop;
246 redisplay();
247 break;
249 case 'D':
250 dbg_show_shadowmap = !dbg_show_shadowmap;
251 redisplay();
252 break;
254 case 's':
255 opt.shadows = !opt.shadows;
256 redisplay();
257 break;
259 case '=':
260 dbg_int++;
261 printf("dbg_int: %d\n", dbg_int);
262 redisplay();
263 break;
265 case '-':
266 dbg_int--;
267 printf("dbg_int: %d\n", dbg_int);
268 redisplay();
269 break;
270 }
271 }
272 }
274 void game_mbutton(int bn, bool press, int x, int y)
275 {
276 bnstate[bn] = press;
277 prev_x = x;
278 prev_y = y;
279 }
281 void game_mmotion(int x, int y)
282 {
283 int dx = x - prev_x;
284 int dy = y - prev_y;
285 prev_x = x;
286 prev_y = y;
288 if(bnstate[0]) {
289 cam_theta += dx * 0.5;
290 cam_phi += dy * 0.5;
292 if(cam_phi < -90) cam_phi = -90;
293 if(cam_phi > 90) cam_phi = 90;
294 }
295 if(bnstate[2]) {
296 cam_dist += dy * 0.1;
297 if(cam_dist < 0.0) cam_dist = 0.0;
298 }
300 // in all cases construct the global pick ray
301 double viewmat[16], projmat[16];
302 int vp[4], ysz;
303 double px, py, pz;
305 glGetIntegerv(GL_VIEWPORT, vp);
306 glGetDoublev(GL_MODELVIEW_MATRIX, viewmat);
307 glGetDoublev(GL_PROJECTION_MATRIX, projmat);
309 ysz = vp[3] - vp[1];
311 gluUnProject(x, ysz - y, 0, viewmat, projmat, vp, &px, &py, &pz);
312 pick_ray.origin = Vector3(px, py, pz);
313 gluUnProject(x, ysz - y, 1, viewmat, projmat, vp, &px, &py, &pz);
314 pick_ray.dir = Vector3(px, py, pz) - pick_ray.origin;
316 board.select_slot(board.slot_hit(pick_ray));
318 redisplay();
319 }