metadata, walk polygons, stuff...
[laserbrain_demo] / src / app.cc
1 #include <stdio.h>
2 #include <assert.h>
3 #include "app.h"
4 #include "opengl.h"
5 #include "sdr.h"
6 #include "texture.h"
7 #include "mesh.h"
8 #include "meshgen.h"
9 #include "scene.h"
10 #include "metascene.h"
11 #include "datamap.h"
12
13 static void draw_scene();
14
15 long time_msec;
16 int win_width, win_height;
17 bool opt_gear_wireframe;
18 bool show_walk_mesh;
19
20 static float cam_dist = 0.0;
21 static float cam_theta, cam_phi = 20;
22 static Vec3 cam_pos;
23 static int prev_mx, prev_my;
24 static bool bnstate[8];
25 static bool keystate[256];
26
27 static Mat4 view_matrix;
28 static TextureSet texman;
29 static Scene *scn;
30 static unsigned int sdr;
31
32 static long prev_msec;
33
34
35 bool app_init()
36 {
37         glEnable(GL_FRAMEBUFFER_SRGB);
38         glEnable(GL_MULTISAMPLE);
39         glEnable(GL_DEPTH_TEST);
40         glEnable(GL_CULL_FACE);
41         glEnable(GL_LIGHTING);
42         glEnable(GL_NORMALIZE);
43
44         Mesh::use_custom_sdr_attr = false;
45
46         float ambient[] = {0.0, 0.0, 0.0, 0.0};
47         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
48
49         scn = new Scene(&texman);
50         if(!load_scene(scn, "data/museum.scene")) {
51                 return false;
52         }
53
54         if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
55                 fprintf(stderr, "failed to load test shaders\n");
56                 return false;
57         }
58         set_uniform_int(sdr, "texmap", 0);
59         set_uniform_int(sdr, "lightmap", 1);
60
61         glUseProgram(0);
62         return true;
63 }
64
65 void app_cleanup()
66 {
67         texman.clear();
68 }
69
70 static void update(float dt)
71 {
72         texman.update();
73
74         scn->update(dt);
75
76         float walk_speed = 2000.0 * dt;
77         Vec3 dir;
78
79         if(keystate[(int)'w']) {
80                 dir.z -= walk_speed;
81         }
82         if(keystate[(int)'s']) {
83                 dir.z += walk_speed;
84         }
85         if(keystate[(int)'d']) {
86                 dir.x += walk_speed;
87         }
88         if(keystate[(int)'a']) {
89                 dir.x -= walk_speed;
90         }
91         if(keystate[(int)'q']) {
92                 cam_pos.y += walk_speed;
93         }
94         if(keystate[(int)'z']) {
95                 cam_pos.y -= walk_speed;
96         }
97
98         float theta = M_PI * cam_theta / 180.0f;
99         cam_pos.x += cos(theta) * dir.x - sin(theta) * dir.z;
100         cam_pos.z += sin(theta) * dir.x + cos(theta) * dir.z;
101 }
102
103 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
104 {
105         unsigned int lt = GL_LIGHT0 + idx;
106         float posv[] = { pos.x, pos.y, pos.z, 1 };
107         float colv[] = { color.x, color.y, color.z, 1 };
108
109         glEnable(lt);
110         glLightfv(lt, GL_POSITION, posv);
111         glLightfv(lt, GL_DIFFUSE, colv);
112         glLightfv(lt, GL_SPECULAR, colv);
113 }
114
115 void app_display()
116 {
117         float dt = (float)(time_msec - prev_msec) / 1000.0f;
118         prev_msec = time_msec;
119
120         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
121
122         view_matrix = Mat4::identity;
123         view_matrix.pre_translate(0, 0, -cam_dist);
124         view_matrix.pre_rotate(deg_to_rad(cam_phi), 1, 0, 0);
125         view_matrix.pre_rotate(deg_to_rad(cam_theta), 0, 1, 0);
126         view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
127
128         glMatrixMode(GL_MODELVIEW);
129         glLoadMatrixf(view_matrix[0]);
130
131         static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
132         set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
133         set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
134         set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
135
136         update(dt);
137
138         draw_scene();
139
140         app_swap_buffers();
141         assert(glGetError() == GL_NO_ERROR);
142 }
143
144
145 static void draw_scene()
146 {
147         /*
148         glBegin(GL_QUADS);
149         glNormal3f(0, 1, 0);
150         glVertex3f(-30, -10, 30);
151         glVertex3f(30, -10, 30);
152         glVertex3f(30, -10, -30);
153         glVertex3f(-30, -10, -30);
154         glEnd();
155         */
156
157         glUseProgram(sdr);
158         scn->draw();
159         glUseProgram(0);
160
161         if(show_walk_mesh && scn->walk_mesh) {
162                 glPushAttrib(GL_ENABLE_BIT);
163                 glEnable(GL_BLEND);
164                 glBlendFunc(GL_ONE, GL_ONE);
165                 glDisable(GL_LIGHTING);
166
167                 glColor3f(0.5, 0.4, 0.05);
168                 scn->walk_mesh->draw();
169
170                 glPopAttrib();
171         }
172 }
173
174
175 void app_reshape(int x, int y)
176 {
177         glViewport(0, 0, x, y);
178
179         glMatrixMode(GL_PROJECTION);
180         glLoadIdentity();
181         gluPerspective(50.0, (float)x / (float)y, 1.0, 10000.0);
182 }
183
184 void app_keyboard(int key, bool pressed)
185 {
186         if(pressed) {
187                 printf("key: %d (mod: %x)\n", key, app_get_modifiers());
188                 switch(key) {
189                 case 27:
190                         app_quit();
191                         break;
192
193                 case 'w':
194                         if(app_get_modifiers() & MOD_CTRL) {
195                                 show_walk_mesh = !show_walk_mesh;
196                         }
197                         break;
198                 }
199         }
200
201         keystate[key] = pressed;
202 }
203
204 void app_mouse_button(int bn, bool pressed, int x, int y)
205 {
206         prev_mx = x;
207         prev_my = y;
208         bnstate[bn] = pressed;
209 }
210
211 void app_mouse_motion(int x, int y)
212 {
213         int dx = x - prev_mx;
214         int dy = y - prev_my;
215         prev_mx = x;
216         prev_my = y;
217
218         if(!dx && !dy) return;
219
220         if(bnstate[0]) {
221                 cam_theta += dx * 0.5;
222                 cam_phi += dy * 0.5;
223
224                 if(cam_phi < -90) cam_phi = -90;
225                 if(cam_phi > 90) cam_phi = 90;
226         }
227         if(bnstate[2]) {
228                 cam_dist += dy * 0.1;
229                 if(cam_dist < 0.0) cam_dist = 0.0;
230         }
231 }