ld33_umonster
view src/game.cc @ 6:3b4460b34d43
progress
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 23 Aug 2015 05:37:09 +0300 |
parents | 93ff21458a16 |
children | 92d662deb66e |
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include "opengl.h"
4 #include "game.h"
5 #include "sdr.h"
6 #include "shader.h"
7 #include "shadow.h"
8 #include "opt.h"
9 #include "mesh.h"
11 #include "room.h"
12 #include "dragon.h"
14 static void draw_scene();
16 int win_width, win_height;
17 unsigned long cur_time;
18 bool dbg_wireframe;
19 int dbg_int;
21 unsigned int sdr_shadow, sdr_shadow_notex;
23 static float cam_theta, cam_phi = 23, cam_dist = 26;
24 static float cam_x, cam_y, cam_z = 9;
25 static bool bnstate[8];
26 static int prev_x, prev_y;
27 static bool gamectl = false;
29 static unsigned int modkeys;
32 static Dragon dragon;
35 bool game_init()
36 {
37 if(init_opengl() == -1) {
38 return false;
39 }
41 glEnable(GL_DEPTH_TEST);
42 glEnable(GL_CULL_FACE);
43 glEnable(GL_NORMALIZE);
44 glEnable(GL_LIGHTING);
45 glEnable(GL_LIGHT0);
47 float amb[] = {0.1, 0.1, 0.1, 1.0};
48 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
50 if(glcaps.sep_spec) {
51 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
52 }
53 glEnable(GL_MULTISAMPLE);
55 if(!init_shadow(4096)) {
56 fprintf(stderr, "failed to initialize shadowmaps\n");
57 return false;
58 }
59 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
60 return false;
61 }
62 set_uniform_int(sdr_shadow, "tex", 0);
63 set_uniform_int(sdr_shadow, "shadowmap", 1);
65 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
66 return false;
67 }
68 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
70 if(!init_room()) {
71 return false;
72 }
74 dragon.set_position(Vector3(0, 5, 20));
75 dragon.set_direction(Vector3(0, 0, -1));
76 dragon.set_target(Vector3(0, 3, 0));
77 dragon.move_head(Vector3(0, 6, 10));
78 dragon.set_head_limits(-ROOM_WIDTH * 0.2, ROOM_WIDTH * 0.2, 1, ROOM_HEIGHT - 3);
80 Mesh::use_custom_sdr_attr = false;
82 assert(glGetError() == GL_NO_ERROR);
83 return true;
84 }
86 void game_cleanup()
87 {
88 cleanup_room();
89 }
91 void game_update(unsigned long time_msec)
92 {
93 cur_time = time_msec;
95 dragon.update();
96 }
98 void game_display()
99 {
100 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();
104 glTranslatef(0, 0.1, -cam_dist);
105 glRotatef(cam_phi, 1, 0, 0);
106 glRotatef(cam_theta, 0, 1, 0);
107 glTranslatef(-cam_x, -cam_y, -cam_z);
109 float lpos[] = {0, 10, 24, 1};
110 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
112 if(opt.shadows && sdr_shadow) {
113 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 45);
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 glLoadTransposeMatrixf(shadow_matrix[0]);
124 glActiveTexture(GL_TEXTURE0);
125 glMatrixMode(GL_MODELVIEW);
127 override_shader(sdr_shadow_notex);
129 draw_scene();
131 glActiveTexture(GL_TEXTURE1);
132 glBindTexture(GL_TEXTURE_2D, 0);
133 glActiveTexture(GL_TEXTURE0);
134 glBindTexture(GL_TEXTURE_2D, 0);
135 } else {
136 draw_scene();
137 }
138 }
140 static void glmaterial(float r, float g, float b, float spec, float shin)
141 {
142 float color[] = {r, g, b, 1};
143 float scolor[] = {spec, spec, spec, 1};
144 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
145 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
146 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
147 }
149 static void draw_scene()
150 {
151 draw_room();
153 glPushMatrix();
154 glTranslatef(0, 0.75, 0);
156 glmaterial(1.0, 0.4, 0.3, 0.8, 60.0);
157 draw_teapot();
159 glPopMatrix();
161 dragon.draw();
162 }
165 void game_reshape(int x, int y)
166 {
167 glMatrixMode(GL_PROJECTION);
168 glLoadIdentity();
169 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
171 glViewport(0, 0, x, y);
172 }
174 void game_keyboard(int bn, bool press)
175 {
176 if(press) {
177 switch(bn) {
178 case 27:
179 quit();
181 case ' ':
182 gamectl = !gamectl;
183 break;
185 case 'w':
186 dbg_wireframe = !dbg_wireframe;
187 redisplay();
188 break;
190 case 's':
191 opt.shadows = !opt.shadows;
192 redisplay();
193 break;
195 case 'p':
196 printf("camera pos(%g %g %g) rot(%g %g) d(%g)\n",
197 cam_x, cam_y, cam_z, cam_theta, cam_phi, cam_dist);
198 break;
199 }
200 }
201 }
203 void game_modifier_key(int key, bool press)
204 {
205 if(press) {
206 modkeys |= (1 << key);
207 } else {
208 modkeys &= ~(1 << key);
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;
218 if(modkeys) {
219 return;
220 }
222 if(bn == 0) {
223 }
224 }
226 void game_mmotion(int x, int y)
227 {
228 int dx = x - prev_x;
229 int dy = y - prev_y;
230 prev_x = x;
231 prev_y = y;
233 if(gamectl) {
234 dragon.move_head(dx * 0.1, -dy * 0.1);
235 }
237 if(modkeys) {
238 if(bnstate[0]) {
239 cam_theta += dx * 0.5;
240 cam_phi += dy * 0.5;
242 if(cam_phi < -90) cam_phi = -90;
243 if(cam_phi > 90) cam_phi = 90;
244 }
245 if(bnstate[1]) {
246 float theta = DEG_TO_RAD(cam_theta);
248 float dxp = dx * 0.1;
249 float dyp = dy * 0.1;
251 cam_x += cos(theta) * dxp - sin(theta) * dyp;
252 if(modkeys & (1 << MOD_SHIFT)) {
253 cam_y -= sin(theta) * dxp + cos(theta) * dyp;
254 } else {
255 cam_z += sin(theta) * dxp + cos(theta) * dyp;
256 }
257 }
258 if(bnstate[2]) {
259 cam_dist += dy * 0.1;
260 if(cam_dist < 0.0) cam_dist = 0.0;
261 }
262 }
263 }