ld33_umonster

view src/game.cc @ 2:35349df5392d

wtf?
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Aug 2015 23:55:21 +0300
parents 4a6683050e29
children 93ff21458a16
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"
10 #include "room.h"
12 static void draw_scene();
14 int win_width, win_height;
15 unsigned long cur_time;
16 bool dbg_wireframe;
17 int dbg_int;
19 unsigned int sdr_shadow, sdr_shadow_notex;
21 static float cam_theta, cam_phi = 25, cam_dist = 8;
22 static bool bnstate[8];
23 static int prev_x, prev_y;
25 static unsigned int modkeys;
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 float amb[] = {0.1, 0.1, 0.1, 1.0};
41 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
43 if(glcaps.sep_spec) {
44 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
45 }
46 glEnable(GL_MULTISAMPLE);
48 if(!init_shadow(4096)) {
49 fprintf(stderr, "failed to initialize shadowmaps\n");
50 return false;
51 }
52 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
53 return false;
54 }
55 set_uniform_int(sdr_shadow, "tex", 0);
56 set_uniform_int(sdr_shadow, "shadowmap", 1);
58 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
59 return false;
60 }
61 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
63 if(!init_room()) {
64 return false;
65 }
67 assert(glGetError() == GL_NO_ERROR);
68 return true;
69 }
71 void game_cleanup()
72 {
73 cleanup_room();
74 }
76 void game_update(unsigned long time_msec)
77 {
78 cur_time = time_msec;
79 }
81 void game_display()
82 {
83 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
85 glMatrixMode(GL_MODELVIEW);
86 glLoadIdentity();
87 glTranslatef(0, 0.1, -cam_dist);
88 glRotatef(cam_phi, 1, 0, 0);
89 glRotatef(cam_theta, 0, 1, 0);
91 float lpos[] = {-0, 0, 0, 1};
92 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
94 opt.shadows = false;
95 if(opt.shadows && sdr_shadow) {
96 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 5);
97 draw_scene();
98 end_shadow_pass();
100 glActiveTexture(GL_TEXTURE1);
101 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
103 glMatrixMode(GL_TEXTURE);
104 Matrix4x4 shadow_matrix = get_shadow_matrix();
105 glLoadTransposeMatrixf(shadow_matrix[0]);
107 glActiveTexture(GL_TEXTURE0);
108 glMatrixMode(GL_MODELVIEW);
110 override_shader(sdr_shadow_notex);
112 draw_scene();
114 glActiveTexture(GL_TEXTURE1);
115 glBindTexture(GL_TEXTURE_2D, 0);
116 glActiveTexture(GL_TEXTURE0);
117 glBindTexture(GL_TEXTURE_2D, 0);
118 } else {
119 override_shader(sdr_shadow_notex);
120 draw_scene();
121 }
122 }
124 static void glmaterial(float r, float g, float b, float spec, float shin)
125 {
126 float color[] = {r, g, b, 1};
127 float scolor[] = {spec, spec, spec, 1};
128 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
129 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
130 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
131 }
133 static void draw_scene()
134 {
135 draw_room();
137 glPushMatrix();
138 glTranslatef(0, 0.75, 0);
140 glmaterial(0.2, 0.3, 1.0, 0.8, 60.0);
141 draw_teapot();
143 glPopMatrix();
144 }
147 void game_reshape(int x, int y)
148 {
149 glMatrixMode(GL_PROJECTION);
150 glLoadIdentity();
151 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
153 glViewport(0, 0, x, y);
154 }
156 void game_keyboard(int bn, bool press)
157 {
158 if(press) {
159 switch(bn) {
160 case 27:
161 quit();
163 case 'w':
164 dbg_wireframe = !dbg_wireframe;
165 redisplay();
166 break;
168 case 's':
169 opt.shadows = !opt.shadows;
170 redisplay();
171 break;
172 }
173 }
174 }
176 void game_modifier_key(int key, bool press)
177 {
178 if(press) {
179 modkeys |= (1 << key);
180 } else {
181 modkeys &= ~(1 << key);
182 }
183 }
185 void game_mbutton(int bn, bool press, int x, int y)
186 {
187 bnstate[bn] = press;
188 prev_x = x;
189 prev_y = y;
191 if(modkeys) {
192 return;
193 }
195 if(bn == 0) {
196 }
197 }
199 void game_mmotion(int x, int y)
200 {
201 int dx = x - prev_x;
202 int dy = y - prev_y;
203 prev_x = x;
204 prev_y = y;
206 if(modkeys) {
207 if(bnstate[0]) {
208 cam_theta += dx * 0.5;
209 cam_phi += dy * 0.5;
211 if(cam_phi < -90) cam_phi = -90;
212 if(cam_phi > 90) cam_phi = 90;
213 }
214 if(bnstate[2]) {
215 cam_dist += dy * 0.1;
216 if(cam_dist < 0.0) cam_dist = 0.0;
217 }
218 }
219 }