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