tavli

view src/game.cc @ 20:111c1a9bca29

moved the camera slightly higher
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jun 2015 21:55:36 +0300
parents 37dead56f01e
children c3fbf9616dbd
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;
21 static Board board;
23 static float cam_theta, cam_phi = 45, cam_dist = 3;
24 static bool bnstate[8];
25 static int prev_x, prev_y;
27 static bool dbg_busyloop, dbg_show_shadowmap;
29 bool game_init()
30 {
31 if(init_opengl() == -1) {
32 return false;
33 }
35 glEnable(GL_DEPTH_TEST);
36 glEnable(GL_CULL_FACE);
37 glEnable(GL_NORMALIZE);
38 glEnable(GL_LIGHTING);
39 glEnable(GL_LIGHT0);
41 float amb[] = {0.3, 0.3, 0.3, 1.0};
42 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
44 if(glcaps.sep_spec) {
45 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
46 }
48 if(glcaps.fsaa) {
49 glEnable(GL_MULTISAMPLE);
50 }
52 if(glcaps.shaders) {
53 Mesh::use_custom_sdr_attr = false;
54 if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
55 return false;
56 }
57 if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
58 return false;
59 }
61 if(glcaps.fbo) {
62 init_shadow(2048);
64 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
65 return false;
66 }
67 set_uniform_int(sdr_shadow, "tex", 0);
68 set_uniform_int(sdr_shadow, "shadowmap", 1);
70 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
71 return false;
72 }
73 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
74 }
75 }
77 if(!board.init()) {
78 return false;
79 }
80 board.setup();
82 if(!init_scenery()) {
83 return false;
84 }
86 assert(glGetError() == GL_NO_ERROR);
87 return true;
88 }
90 void game_cleanup()
91 {
92 board.destroy();
93 destroy_scenery();
94 destroy_shadow();
95 }
97 void game_update(unsigned long time_msec)
98 {
99 cur_time = time_msec;
100 }
102 void game_display()
103 {
104 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106 glMatrixMode(GL_MODELVIEW);
107 glLoadIdentity();
108 glTranslatef(0, 0.1, -cam_dist);
109 glRotatef(cam_phi, 1, 0, 0);
110 glRotatef(cam_theta, 0, 1, 0);
112 float lpos[] = {-10, 20, 10, 1};
113 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
115 if(opt.shadows && sdr_shadow) {
116 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 4.5);
117 draw_scene();
118 end_shadow_pass();
120 glActiveTexture(GL_TEXTURE1);
121 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
123 glMatrixMode(GL_TEXTURE);
124 Matrix4x4 shadow_matrix = get_shadow_matrix();
125 glLoadTransposeMatrixf(shadow_matrix[0]);
127 glActiveTexture(GL_TEXTURE0);
128 glMatrixMode(GL_MODELVIEW);
130 draw_scene();
132 glActiveTexture(GL_TEXTURE1);
133 glBindTexture(GL_TEXTURE_2D, 0);
134 glActiveTexture(GL_TEXTURE0);
135 glBindTexture(GL_TEXTURE_2D, 0);
136 } else {
137 draw_scene();
138 }
140 if(dbg_show_shadowmap) {
141 glMatrixMode(GL_PROJECTION);
142 glPushMatrix();
143 glLoadIdentity();
144 glMatrixMode(GL_MODELVIEW);
145 glPushMatrix();
146 glLoadIdentity();
147 glMatrixMode(GL_TEXTURE);
148 glLoadIdentity();
150 glPushAttrib(GL_ENABLE_BIT);
151 glUseProgram(0);
152 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
154 glEnable(GL_TEXTURE_2D);
155 glDisable(GL_DEPTH_TEST);
156 glDisable(GL_LIGHTING);
157 glDisable(GL_BLEND);
159 glBegin(GL_QUADS);
160 glColor4f(1, 1, 1, 1);
161 glTexCoord2f(0, 0); glVertex2f(-1, -1);
162 glTexCoord2f(1, 0); glVertex2f(1, -1);
163 glTexCoord2f(1, 1); glVertex2f(1, 1);
164 glTexCoord2f(0, 1); glVertex2f(-1, 1);
165 glEnd();
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
168 glBindTexture(GL_TEXTURE_2D, 0);
170 glPopAttrib();
172 glMatrixMode(GL_PROJECTION);
173 glPopMatrix();
174 glMatrixMode(GL_MODELVIEW);
175 glPopMatrix();
176 }
178 if(dbg_busyloop) {
179 redisplay();
180 }
181 }
183 static void draw_scene()
184 {
185 draw_backdrop();
186 draw_scenery();
187 board.draw();
188 }
190 static void draw_backdrop()
191 {
192 glPushAttrib(GL_ENABLE_BIT);
193 glDisable(GL_LIGHTING);
194 glDisable(GL_DEPTH_TEST);
196 glMatrixMode(GL_PROJECTION);
197 glPushMatrix();
198 glLoadIdentity();
199 glMatrixMode(GL_MODELVIEW);
200 glPushMatrix();
201 glLoadIdentity();
203 glBegin(GL_QUADS);
204 glColor3f(0.9, 0.8, 0.6);
205 glVertex2f(-1, -1);
206 glVertex2f(1, -1);
207 glColor3f(0.4, 0.5, 0.8);
208 glVertex2f(1, 1);
209 glVertex2f(-1, 1);
210 glEnd();
212 glMatrixMode(GL_PROJECTION);
213 glPopMatrix();
214 glMatrixMode(GL_MODELVIEW);
215 glPopMatrix();
217 glPopAttrib();
218 }
220 void game_reshape(int x, int y)
221 {
222 glMatrixMode(GL_PROJECTION);
223 glLoadIdentity();
224 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
226 glViewport(0, 0, x, y);
227 }
229 void game_keyboard(int bn, bool press)
230 {
231 if(press) {
232 switch(bn) {
233 case 27:
234 quit();
236 case 'w':
237 wireframe = !wireframe;
238 redisplay();
239 break;
241 case 'd':
242 dbg_busyloop = !dbg_busyloop;
243 redisplay();
244 break;
246 case 'D':
247 dbg_show_shadowmap = !dbg_show_shadowmap;
248 redisplay();
249 break;
251 case 's':
252 opt.shadows = !opt.shadows;
253 redisplay();
254 break;
255 }
256 }
257 }
259 void game_mbutton(int bn, bool press, int x, int y)
260 {
261 bnstate[bn] = press;
262 prev_x = x;
263 prev_y = y;
264 }
266 void game_mmotion(int x, int y)
267 {
268 int dx = x - prev_x;
269 int dy = y - prev_y;
270 prev_x = x;
271 prev_y = y;
273 if(bnstate[0]) {
274 cam_theta += dx * 0.5;
275 cam_phi += dy * 0.5;
277 if(cam_phi < -90) cam_phi = -90;
278 if(cam_phi > 90) cam_phi = 90;
280 redisplay();
281 }
282 if(bnstate[2]) {
283 cam_dist += dy * 0.1;
284 if(cam_dist < 0.0) cam_dist = 0.0;
286 redisplay();
287 }
288 }