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