ld33_umonster

view src/game.cc @ 3:93ff21458a16

fixed stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 23 Aug 2015 02:51:39 +0300
parents 35349df5392d
children 3b4460b34d43
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"
13 static void draw_scene();
15 int win_width, win_height;
16 unsigned long cur_time;
17 bool dbg_wireframe;
18 int dbg_int;
20 unsigned int sdr_shadow, sdr_shadow_notex;
22 static float cam_theta, cam_phi = 25, cam_dist = 8;
23 static bool bnstate[8];
24 static int prev_x, prev_y;
26 static unsigned int modkeys;
29 bool game_init()
30 {
31 if(init_opengl() == -1) {
32 return false;
33 }
35 glEnable(GL_DEPTH_TEST);
36 glEnable(GL_CULL_FACE);
37 glEnable(GL_NORMALIZE);
38 glEnable(GL_LIGHTING);
39 glEnable(GL_LIGHT0);
41 float amb[] = {0.1, 0.1, 0.1, 1.0};
42 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
44 if(glcaps.sep_spec) {
45 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
46 }
47 glEnable(GL_MULTISAMPLE);
49 if(!init_shadow(4096)) {
50 fprintf(stderr, "failed to initialize shadowmaps\n");
51 return false;
52 }
53 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
54 return false;
55 }
56 set_uniform_int(sdr_shadow, "tex", 0);
57 set_uniform_int(sdr_shadow, "shadowmap", 1);
59 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
60 return false;
61 }
62 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
64 if(!init_room()) {
65 return false;
66 }
68 Mesh::use_custom_sdr_attr = false;
70 assert(glGetError() == GL_NO_ERROR);
71 return true;
72 }
74 void game_cleanup()
75 {
76 cleanup_room();
77 }
79 void game_update(unsigned long time_msec)
80 {
81 cur_time = time_msec;
82 }
84 void game_display()
85 {
86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
88 glMatrixMode(GL_MODELVIEW);
89 glLoadIdentity();
90 glTranslatef(0, 0.1, -cam_dist);
91 glRotatef(cam_phi, 1, 0, 0);
92 glRotatef(cam_theta, 0, 1, 0);
94 float lpos[] = {-5, 15, 10, 1};
95 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
97 if(opt.shadows && sdr_shadow) {
98 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 10);
99 draw_scene();
100 end_shadow_pass();
102 glActiveTexture(GL_TEXTURE1);
103 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
105 glMatrixMode(GL_TEXTURE);
106 Matrix4x4 shadow_matrix = get_shadow_matrix();
107 glLoadTransposeMatrixf(shadow_matrix[0]);
109 glActiveTexture(GL_TEXTURE0);
110 glMatrixMode(GL_MODELVIEW);
112 override_shader(sdr_shadow_notex);
114 draw_scene();
116 glActiveTexture(GL_TEXTURE1);
117 glBindTexture(GL_TEXTURE_2D, 0);
118 glActiveTexture(GL_TEXTURE0);
119 glBindTexture(GL_TEXTURE_2D, 0);
120 } else {
121 draw_scene();
122 }
123 }
125 static void glmaterial(float r, float g, float b, float spec, float shin)
126 {
127 float color[] = {r, g, b, 1};
128 float scolor[] = {spec, spec, spec, 1};
129 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
130 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
131 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
132 }
134 static void draw_scene()
135 {
136 draw_room();
138 glPushMatrix();
139 glTranslatef(0, 0.75, 0);
141 glmaterial(1.0, 0.4, 0.3, 0.8, 60.0);
142 draw_teapot();
144 glPopMatrix();
145 }
148 void game_reshape(int x, int y)
149 {
150 glMatrixMode(GL_PROJECTION);
151 glLoadIdentity();
152 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
154 glViewport(0, 0, x, y);
155 }
157 void game_keyboard(int bn, bool press)
158 {
159 if(press) {
160 switch(bn) {
161 case 27:
162 quit();
164 case 'w':
165 dbg_wireframe = !dbg_wireframe;
166 redisplay();
167 break;
169 case 's':
170 opt.shadows = !opt.shadows;
171 redisplay();
172 break;
173 }
174 }
175 }
177 void game_modifier_key(int key, bool press)
178 {
179 if(press) {
180 modkeys |= (1 << key);
181 } else {
182 modkeys &= ~(1 << key);
183 }
184 }
186 void game_mbutton(int bn, bool press, int x, int y)
187 {
188 bnstate[bn] = press;
189 prev_x = x;
190 prev_y = y;
192 if(modkeys) {
193 return;
194 }
196 if(bn == 0) {
197 }
198 }
200 void game_mmotion(int x, int y)
201 {
202 int dx = x - prev_x;
203 int dy = y - prev_y;
204 prev_x = x;
205 prev_y = y;
207 if(modkeys) {
208 if(bnstate[0]) {
209 cam_theta += dx * 0.5;
210 cam_phi += dy * 0.5;
212 if(cam_phi < -90) cam_phi = -90;
213 if(cam_phi > 90) cam_phi = 90;
214 }
215 if(bnstate[2]) {
216 cam_dist += dy * 0.1;
217 if(cam_dist < 0.0) cam_dist = 0.0;
218 }
219 }
220 }