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