dbf_amiga

view src/game.cc @ 0:87dfe0e10235

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 31 Aug 2015 07:38:37 +0300
parents
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"
10 #include "scene.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 = 23, cam_dist = 10;
22 static float cam_x, cam_y, cam_z;
23 static bool bnstate[8];
24 static int prev_x, prev_y;
26 static unsigned int modkeys;
28 static Scene *scn;
31 bool game_init()
32 {
33 if(init_opengl() == -1) {
34 return false;
35 }
37 glEnable(GL_DEPTH_TEST);
38 glEnable(GL_CULL_FACE);
39 glEnable(GL_NORMALIZE);
40 glEnable(GL_LIGHTING);
41 glEnable(GL_LIGHT0);
43 float amb[] = {0.1, 0.1, 0.1, 1.0};
44 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
46 if(glcaps.sep_spec) {
47 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
48 }
49 glEnable(GL_MULTISAMPLE);
51 if(!init_shadow(4096)) {
52 fprintf(stderr, "failed to initialize shadowmaps\n");
53 return false;
54 }
55 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
56 return false;
57 }
58 set_uniform_int(sdr_shadow, "tex", 0);
59 set_uniform_int(sdr_shadow, "shadowmap", 1);
61 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
62 return false;
63 }
64 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
66 scn = new Scene;
67 if(!scn->load("data/amiga.obj")) {
68 return false;
69 }
71 Mesh::use_custom_sdr_attr = false;
72 assert(glGetError() == GL_NO_ERROR);
73 return true;
74 }
76 void game_cleanup()
77 {
78 delete scn;
79 }
81 void game_update(unsigned long time_msec)
82 {
83 cur_time = time_msec;
84 }
86 void game_display()
87 {
88 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
90 glMatrixMode(GL_MODELVIEW);
91 glLoadIdentity();
92 glTranslatef(0, 0.1, -cam_dist);
93 glRotatef(cam_phi, 1, 0, 0);
94 glRotatef(cam_theta, 0, 1, 0);
95 glTranslatef(-cam_x, -cam_y, -cam_z);
97 float lpos[] = {-5, 10, 5, 1};
98 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
100 if(opt.shadows && sdr_shadow) {
101 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 45);
102 draw_scene();
103 end_shadow_pass();
105 glActiveTexture(GL_TEXTURE1);
106 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
108 glMatrixMode(GL_TEXTURE);
109 Matrix4x4 shadow_matrix = get_shadow_matrix();
110 glLoadTransposeMatrixf(shadow_matrix[0]);
112 glActiveTexture(GL_TEXTURE0);
113 glMatrixMode(GL_MODELVIEW);
115 override_shader(sdr_shadow_notex);
116 draw_scene();
117 override_shader(0);
119 glActiveTexture(GL_TEXTURE1);
120 glBindTexture(GL_TEXTURE_2D, 0);
121 glActiveTexture(GL_TEXTURE0);
122 glBindTexture(GL_TEXTURE_2D, 0);
123 } else {
124 draw_scene();
125 }
126 }
128 static void glmaterial(float r, float g, float b, float spec, float shin)
129 {
130 float color[] = {r, g, b, 1};
131 float scolor[] = {spec, spec, spec, 1};
132 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
133 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
134 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
135 }
137 static void draw_scene()
138 {
139 glmaterial(0.2, 0.2, 0.4, 0.0, 60.0);
140 glBegin(GL_QUADS);
141 glNormal3f(0, 1, 0);
142 glVertex3f(-5, 0, 5);
143 glVertex3f(5, 0, 5);
144 glVertex3f(5, 0, -5);
145 glVertex3f(-5, 0, -5);
146 glEnd();
148 glPushMatrix();
149 glTranslatef(0, 0.75, 0);
151 glmaterial(1.0, 0.4, 0.3, 0.8, 60.0);
152 draw_teapot();
154 glPopMatrix();
156 scn->draw();
157 }
160 void game_reshape(int x, int y)
161 {
162 glMatrixMode(GL_PROJECTION);
163 glLoadIdentity();
164 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
166 glViewport(0, 0, x, y);
167 }
169 void game_keyboard(int bn, bool press)
170 {
171 if(press) {
172 switch(bn) {
173 case 27:
174 quit();
176 case 'd':
177 scn->dump(stdout);
178 break;
180 case 'w':
181 dbg_wireframe = !dbg_wireframe;
182 redisplay();
183 break;
185 case 's':
186 opt.shadows = !opt.shadows;
187 redisplay();
188 break;
190 case 'p':
191 printf("camera pos(%g %g %g) rot(%g %g) d(%g)\n",
192 cam_x, cam_y, cam_z, cam_theta, cam_phi, cam_dist);
193 break;
194 }
195 }
196 }
198 void game_modifier_key(int key, bool press)
199 {
200 if(press) {
201 modkeys |= (1 << key);
202 } else {
203 modkeys &= ~(1 << key);
204 }
205 }
207 void game_mbutton(int bn, bool press, int x, int y)
208 {
209 bnstate[bn] = press;
210 prev_x = x;
211 prev_y = y;
213 if(modkeys) {
214 return;
215 }
217 if(bn == 0) {
218 }
219 }
221 void game_mmotion(int x, int y)
222 {
223 int dx = x - prev_x;
224 int dy = y - prev_y;
225 prev_x = x;
226 prev_y = y;
228 if(modkeys) {
229 if(bnstate[0]) {
230 cam_theta += dx * 0.5;
231 cam_phi += dy * 0.5;
233 if(cam_phi < -90) cam_phi = -90;
234 if(cam_phi > 90) cam_phi = 90;
235 }
236 if(bnstate[1]) {
237 float theta = DEG_TO_RAD(cam_theta);
239 float dxp = dx * 0.1;
240 float dyp = dy * 0.1;
242 cam_x += cos(theta) * dxp - sin(theta) * dyp;
243 if(modkeys & (1 << MOD_SHIFT)) {
244 cam_y -= sin(theta) * dxp + cos(theta) * dyp;
245 } else {
246 cam_z += sin(theta) * dxp + cos(theta) * dyp;
247 }
248 }
249 if(bnstate[2]) {
250 cam_dist += dy * 0.1;
251 if(cam_dist < 0.0) cam_dist = 0.0;
252 }
253 }
254 }