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