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