tavli

view src/game.cc @ 18:986c0b76513f

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