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