ld33_umonster

view src/game.cc @ 10:1b30bd381667

sweep curve mesh gen and dragon horns
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 27 Aug 2015 05:25:04 +0300
parents 3b4460b34d43
children
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 = new Dragon;
75 dragon->set_position(Vector3(0, 5, 20));
76 dragon->set_direction(Vector3(0, 0, -1));
77 dragon->set_target(Vector3(0, 3, 0));
78 dragon->move_head(Vector3(0, 6, 10));
79 dragon->set_head_limits(-ROOM_WIDTH * 0.2, ROOM_WIDTH * 0.2, 1, ROOM_HEIGHT - 3);
81 Mesh::use_custom_sdr_attr = false;
83 assert(glGetError() == GL_NO_ERROR);
84 return true;
85 }
87 void game_cleanup()
88 {
89 delete dragon;
90 cleanup_room();
91 }
93 void game_update(unsigned long time_msec)
94 {
95 cur_time = time_msec;
97 dragon->update();
98 }
100 void game_display()
101 {
102 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104 glMatrixMode(GL_MODELVIEW);
105 glLoadIdentity();
106 glTranslatef(0, 0.1, -cam_dist);
107 glRotatef(cam_phi, 1, 0, 0);
108 glRotatef(cam_theta, 0, 1, 0);
109 glTranslatef(-cam_x, -cam_y, -cam_z);
111 float lpos[] = {0, 10, 24, 1};
112 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
114 if(opt.shadows && sdr_shadow) {
115 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 45);
116 draw_scene();
117 end_shadow_pass();
119 glActiveTexture(GL_TEXTURE1);
120 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
122 glMatrixMode(GL_TEXTURE);
123 Matrix4x4 shadow_matrix = get_shadow_matrix();
124 glLoadTransposeMatrixf(shadow_matrix[0]);
126 glActiveTexture(GL_TEXTURE0);
127 glMatrixMode(GL_MODELVIEW);
129 override_shader(sdr_shadow_notex);
131 draw_scene();
133 glActiveTexture(GL_TEXTURE1);
134 glBindTexture(GL_TEXTURE_2D, 0);
135 glActiveTexture(GL_TEXTURE0);
136 glBindTexture(GL_TEXTURE_2D, 0);
137 } else {
138 draw_scene();
139 }
140 }
142 static void glmaterial(float r, float g, float b, float spec, float shin)
143 {
144 float color[] = {r, g, b, 1};
145 float scolor[] = {spec, spec, spec, 1};
146 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
147 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
148 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
149 }
151 static void draw_scene()
152 {
153 draw_room();
155 glPushMatrix();
156 glTranslatef(0, 0.75, 0);
158 glmaterial(1.0, 0.4, 0.3, 0.8, 60.0);
159 draw_teapot();
161 glPopMatrix();
163 dragon->draw();
164 }
167 void game_reshape(int x, int y)
168 {
169 glMatrixMode(GL_PROJECTION);
170 glLoadIdentity();
171 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
173 glViewport(0, 0, x, y);
174 }
176 void game_keyboard(int bn, bool press)
177 {
178 if(press) {
179 switch(bn) {
180 case 27:
181 quit();
183 case ' ':
184 gamectl = !gamectl;
185 break;
187 case 'w':
188 dbg_wireframe = !dbg_wireframe;
189 redisplay();
190 break;
192 case 's':
193 opt.shadows = !opt.shadows;
194 redisplay();
195 break;
197 case 'p':
198 printf("camera pos(%g %g %g) rot(%g %g) d(%g)\n",
199 cam_x, cam_y, cam_z, cam_theta, cam_phi, cam_dist);
200 break;
201 }
202 }
203 }
205 void game_modifier_key(int key, bool press)
206 {
207 if(press) {
208 modkeys |= (1 << key);
209 } else {
210 modkeys &= ~(1 << key);
211 }
212 }
214 void game_mbutton(int bn, bool press, int x, int y)
215 {
216 bnstate[bn] = press;
217 prev_x = x;
218 prev_y = y;
220 if(modkeys) {
221 return;
222 }
224 if(bn == 0) {
225 }
226 }
228 void game_mmotion(int x, int y)
229 {
230 int dx = x - prev_x;
231 int dy = y - prev_y;
232 prev_x = x;
233 prev_y = y;
235 if(gamectl) {
236 dragon->move_head(dx * 0.1, -dy * 0.1);
237 }
239 if(modkeys) {
240 if(bnstate[0]) {
241 cam_theta += dx * 0.5;
242 cam_phi += dy * 0.5;
244 if(cam_phi < -90) cam_phi = -90;
245 if(cam_phi > 90) cam_phi = 90;
246 }
247 if(bnstate[1]) {
248 float theta = DEG_TO_RAD(cam_theta);
250 float dxp = dx * 0.1;
251 float dyp = dy * 0.1;
253 cam_x += cos(theta) * dxp - sin(theta) * dyp;
254 if(modkeys & (1 << MOD_SHIFT)) {
255 cam_y -= sin(theta) * dxp + cos(theta) * dyp;
256 } else {
257 cam_z += sin(theta) * dxp + cos(theta) * dyp;
258 }
259 }
260 if(bnstate[2]) {
261 cam_dist += dy * 0.1;
262 if(cam_dist < 0.0) cam_dist = 0.0;
263 }
264 }
265 }