ld33_umonster

view src/game.cc @ 0:4a6683050e29

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Aug 2015 07:15:00 +0300
parents
children 35349df5392d
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 static void draw_scene();
12 int win_width, win_height;
13 unsigned long cur_time;
14 bool dbg_wireframe;
15 int dbg_int;
17 unsigned int sdr_shadow, sdr_shadow_notex;
19 static float cam_theta, cam_phi = 25, cam_dist = 8;
20 static bool bnstate[8];
21 static int prev_x, prev_y;
23 static unsigned int modkeys;
26 bool game_init()
27 {
28 if(init_opengl() == -1) {
29 return false;
30 }
32 glEnable(GL_DEPTH_TEST);
33 glEnable(GL_CULL_FACE);
34 glEnable(GL_NORMALIZE);
35 glEnable(GL_LIGHTING);
36 glEnable(GL_LIGHT0);
38 float amb[] = {0.3, 0.3, 0.3, 1.0};
39 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
41 if(glcaps.sep_spec) {
42 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
43 }
44 glEnable(GL_MULTISAMPLE);
46 if(!init_shadow(4096)) {
47 fprintf(stderr, "failed to initialize shadowmaps\n");
48 return false;
49 }
50 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
51 return false;
52 }
53 set_uniform_int(sdr_shadow, "tex", 0);
54 set_uniform_int(sdr_shadow, "shadowmap", 1);
56 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
57 return false;
58 }
59 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
62 assert(glGetError() == GL_NO_ERROR);
63 return true;
64 }
66 void game_cleanup()
67 {
68 }
70 void game_update(unsigned long time_msec)
71 {
72 cur_time = time_msec;
73 }
75 void game_display()
76 {
77 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
79 glMatrixMode(GL_MODELVIEW);
80 glLoadIdentity();
81 glTranslatef(0, 0.1, -cam_dist);
82 glRotatef(cam_phi, 1, 0, 0);
83 glRotatef(cam_theta, 0, 1, 0);
85 float lpos[] = {-10, 20, 10, 1};
86 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
88 if(opt.shadows && sdr_shadow) {
89 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 5);
90 draw_scene();
91 end_shadow_pass();
93 glActiveTexture(GL_TEXTURE1);
94 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
96 glMatrixMode(GL_TEXTURE);
97 Matrix4x4 shadow_matrix = get_shadow_matrix();
98 glLoadTransposeMatrixf(shadow_matrix[0]);
100 glActiveTexture(GL_TEXTURE0);
101 glMatrixMode(GL_MODELVIEW);
103 set_shader(sdr_shadow_notex);
105 draw_scene();
107 glActiveTexture(GL_TEXTURE1);
108 glBindTexture(GL_TEXTURE_2D, 0);
109 glActiveTexture(GL_TEXTURE0);
110 glBindTexture(GL_TEXTURE_2D, 0);
111 } else {
112 draw_scene();
113 }
114 }
116 static void glmaterial(float r, float g, float b, float spec, float shin)
117 {
118 float color[] = {r, g, b, 1};
119 float scolor[] = {spec, spec, spec, 1};
120 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
121 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
122 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
123 }
125 static void draw_scene()
126 {
127 glPushMatrix();
128 glTranslatef(0, -1, 0);
130 glmaterial(0.5, 0.5, 0.5, 0.8, 40.0);
132 glBegin(GL_QUADS);
133 glNormal3f(0, 1, 0);
134 glVertex3f(-10, 0, 10);
135 glVertex3f(10, 0, 10);
136 glVertex3f(10, 0, -10);
137 glVertex3f(-10, 0, -10);
138 glEnd();
140 glPopMatrix();
142 glmaterial(0.2, 0.3, 1.0, 0.8, 60.0);
143 draw_teapot();
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 }