affeff79119f4c4bbf866ea3295c13cf36c9918b
[demo] / src / main.cc
1 #include <GL/glew.h>
2 #include <GLFW/glfw3.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <vector>
8
9 #include <gmath/gmath.h>
10
11 #include "gfxapi.h"
12 #include "global.h"
13
14 /* TODO: fix those */
15 #include "camera.h"
16 #include "mesh.h"
17 #include "morph_renderer.h"
18 #include "object.h"
19 #include "scene.h"
20 #include "terrain.h"
21 #include "texture.h"
22
23 #include "opengl/opengl.h"
24 #include "vulkan/vk.h"
25
26 /* static functions */
27
28 static bool init(Gfx_API api);
29 static void cleanup();
30 static void display();
31 static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius);
32
33 /* glfw callbacks */
34
35 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
36 static void clbk_motion(GLFWwindow *win, double x, double y);
37 static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
38 static void clbk_reshape(GLFWwindow *win, int width, int height);
39
40 /* global variables */
41
42 Mat4 mprojection;
43
44 GLFWwindow *win;
45 int win_w = 800;
46 int win_h = 600;
47
48 ShaderManager *sdr_man;
49
50 double time_sec;
51
52 /* variables */
53 static bool move_camera;
54
55 static float cam_phi = 25;
56 static float cam_theta = 0;
57 static float cam_dist = 16;
58 static Vec3 cam_pos;
59
60 static float aspect;
61 static OrbitCamera *camera;
62
63 static float fog_density;
64
65 static int num_cows = 1;
66 static float cow_gap = 4;
67 static Scene *cow_scene;
68 static MorphRenderer *cow_rend;
69
70 static Terrain terrain;
71 static TerrainParams p;
72 static Texture *skybox_tex;
73 static Texture *irradiance_tex;
74 static Texture *terrain_tex;
75 static Material terrain_mat;
76 static Renderer *terrain_rend;
77
78 /* *** */
79
80 int main(int argc, char **argv)
81 {
82         Gfx_API api;
83
84         for(int i=1; i<argc; i++) {
85                 if(strcmp(argv[i], "-opengl") == 0) {
86                         api = GFX_GL;
87                         printf("Backend: OpenGL.\n");
88                 }
89                 else if(strcmp(argv[i], "-vulkan") == 0) {
90                         api = GFX_VK;
91                         printf("Backend: Vulkan.\n");
92                 }
93                 else {
94                         api = GFX_GL;
95                         printf("No backend specified. Using OpenGL.\n");
96                 }
97         }
98
99         if(!init(api)) {
100                 fprintf(stderr, "Failed to initialize program.\n");
101                 return 1;
102         }
103
104         //TODO
105         //return 0;
106
107         glfwSetKeyCallback(win, clbk_key);
108         glfwSetCursorPosCallback(win, clbk_motion);
109         glfwSetMouseButtonCallback(win, clbk_mouse);
110         glfwSetWindowSizeCallback(win, clbk_reshape);
111
112         glfwGetWindowSize(win, &win_w, &win_h);
113         clbk_reshape(win, win_w, win_h);
114
115         while(!glfwWindowShouldClose(win)) {
116                 display();
117
118                 gfx_swapbuffers();
119                 glfwPollEvents();
120         }
121
122         cleanup();
123         return 0;
124 }
125
126 static bool init(Gfx_API api)
127 {
128         if(!gfx_init(api))
129                 return false;
130
131         fog_density = 0.0037;
132
133         sdr_man = new ShaderManager;
134
135         camera = new OrbitCamera;
136
137         terrain_tex = gfx_create_texture();
138         if(!terrain_tex->load("data/grass.jpeg")) {
139                 fprintf(stderr, "Failed to load ground texture.\n");
140                 return false;
141         }
142
143         Image ter_hmap;
144         if(!ter_hmap.load("data/terhmap.png")) {
145                 fprintf(stderr, "Failed to load terrain heightmap.\n");
146                 return false;
147         }
148
149         p.xsz = 20;//200;
150         p.ysz = 20; //200;
151         p.max_height = 3;//30;
152         p.xtiles = 4;//40;
153         p.ytiles = 4;//40;
154         p.tile_usub = 10;
155         p.tile_vsub = 10;
156         p.num_octaves = 3;
157         p.noise_freq = 5;
158         p.coarse_heightmap = ter_hmap;
159
160         terrain.init();
161         terrain.generate(p);
162
163         terrain_mat.diffuse = Vec3(1, 1, 1);
164         terrain_mat.specular = Vec3(0, 0, 0);
165         terrain_mat.shininess = 40;
166         terrain_mat.dtex = terrain_tex;
167         terrain_mat.name = "tt";
168
169         terrain.material = terrain_mat;
170
171         terrain_rend = new Renderer;
172         terrain_rend->camera = camera;
173         terrain_rend->scene = terrain.get_visible(camera);
174
175         skybox_tex = gfx_create_texture();
176         skybox_tex->load("data/cubemap/cubemap.hdr");
177         terrain_rend->set_sky_tex(skybox_tex);
178
179         irradiance_tex = gfx_create_texture();
180         irradiance_tex->load("data/cubemap/irradiance.hdr");
181         terrain_rend->set_diffuse_sky_tex(irradiance_tex);
182
183         if(!terrain_rend->create()) {
184                 fprintf(stderr, "terrain fail\n");
185                 return false;
186         }
187         terrain_rend->fog_density = fog_density;
188
189         cow_scene = new Scene;
190         if(!cow_scene->load("data/spot/spot.obj")) {
191                 fprintf(stderr, "Failed to load scene: spot.obj.\n");
192                 return false;
193         }
194
195         cow_rend = new MorphRenderer;
196         cow_rend->camera = camera;
197         cow_rend->scene = cow_scene;
198         cow_rend->fog_density = fog_density;
199
200         if(!cow_rend->create()) {
201                 fprintf(stderr, "Failed to create renderer for cows.\n");
202                 return false;
203         }
204
205         /* create cow objects */
206         Object *cow0 = cow_scene->objects[0];
207         cow0->transform.rotation_y(M_PI);
208         cow_scene->objects.clear();
209
210         float disk_radius = std::min(p.xsz, p.ysz) / 2.0 * 0.65;
211         std::vector<Vec2> cow_pos;
212
213         for(int i=0; i<num_cows; i++) {
214                 Object *cow = new Object;
215                 *cow = *cow0;
216
217                 if(!gen_poisson(cow_pos, cow_gap, disk_radius))
218                         goto cowgen_end;
219                 Vec2 pos = cow_pos.back();
220                 float y = terrain.get_height(Vec3(pos.x, 1, pos.y));
221
222                 cow->transform.translate(pos.x, y, pos.y);
223                 cow_scene->objects.push_back(cow);
224         }
225
226 cowgen_end:
227         printf("generated: %d cows from %d\n", (int)cow_pos.size(), num_cows);
228         delete cow0;
229         return true;
230 }
231
232 static void cleanup()
233 {
234         delete sdr_man;
235         delete camera;
236
237         delete cow_scene;
238         delete cow_rend;
239
240         delete skybox_tex;
241         delete irradiance_tex;
242         delete terrain_tex;
243         delete terrain_rend;
244
245         gfx_cleanup();
246 }
247
248 static float cow_speed = 10;
249 static Vec3 cow_pos;
250 static bool keystate[256];
251
252 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
253 {
254         if(action == GLFW_REPEAT) return;
255
256         if(action == GLFW_PRESS) {
257                 switch(key) {
258                 case GLFW_KEY_ESCAPE:
259                         glfwSetWindowShouldClose(win, GLFW_TRUE);
260                         return;
261
262                 case ' ':
263                         move_camera = !move_camera;
264                         break;
265
266                 // case 'F':
267                 //      fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1;
268                 //      break;
269
270                 // case 'U':
271                 //      fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0;
272                 //      break;
273
274                 case 'P':
275                         gfx_wireframe(true);
276                         break;
277
278                 case 'F':
279                         gfx_wireframe(false);
280                         break;
281
282                 default:
283                         break;
284                 }
285         }
286
287         if(key < 256) {
288                 keystate[key] = action == GLFW_PRESS;
289         }
290 }
291
292 static double prev_x, prev_y;
293 static bool button[8];
294
295 static void clbk_motion(GLFWwindow *win, double x, double y)
296 {
297         double dx = x - prev_x;
298         double dy = y - prev_y;
299
300         prev_x = x;
301         prev_y = y;
302
303         if(button[0]) {
304                 cam_theta += dx * 0.5;
305                 cam_phi += dy * 0.5;
306
307                 if(cam_phi < 0)
308                         cam_phi = 0;
309                 if(cam_phi > 90)
310                         cam_phi = 90;
311         }
312
313         if(button[1]) {
314                 cam_dist += dy * 0.1;
315                 if(cam_dist < 0.0) {
316                         cam_dist = 0.0;
317                 }
318         }
319 }
320
321 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
322 {
323         button[bn] = action == GLFW_PRESS;
324         glfwGetCursorPos(win, &prev_x, &prev_y);
325 }
326
327 static void clbk_reshape(GLFWwindow *win, int width, int height)
328 {
329         gfx_reshape(width, height);
330         gfx_viewport(0, 0, width, height);
331         aspect = (float)width / (float)height;
332         mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
333
334         win_h = height;
335         win_w = width;
336 }
337
338 static void update(float dt)
339 {
340         Vec3 dir;
341
342         if(keystate['D'])
343                 dir.x += cow_speed * dt;
344         if(keystate['A'])
345                 dir.x -= cow_speed * dt;
346         if(keystate['W'])
347                 dir.z -= cow_speed * dt;
348         if(keystate['S'])
349                 dir.z += cow_speed * dt;
350
351         Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
352         float theta = cam_theta / 180.0 * M_PI;
353         pos->x += dir.x * cos(theta) - dir.z * sin(theta);
354         pos->z += dir.x * sin(theta) + dir.z * cos(theta);
355 }
356
357 static void display()
358 {
359         static float prev_tsec;
360         time_sec = glfwGetTime();
361         float dt = time_sec - prev_tsec;
362         prev_tsec = time_sec;
363
364         update(dt);
365
366         cam_pos.y = terrain.get_height(cam_pos) + 0.5;
367         camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
368         camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
369
370         gfx_begin_drawing();
371
372         gfx_clear(0.1, 0.1, 0.1);
373
374         terrain_rend->draw();
375         cow_rend->draw();
376
377         gfx_end_drawing();
378 }
379
380 static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius)
381 {
382         /* poisson radius */
383         for(int i = 0; i < 1000; i++) {
384                 float angle = (float)rand() / (float)RAND_MAX * 2 * M_PI;
385                 float r = sqrt((float)rand() / (float)RAND_MAX) * radius;
386
387                 Vec2 p;
388                 p.x = cos(angle) * r;
389                 p.y = sin(angle) * r;
390
391                 bool valid = true;
392                 for(size_t j=0; j<points.size(); j++) {
393                         if(length_sq(points[j] - p) < min_dist * min_dist) {
394                                 valid = false;
395                                 break;
396                         }
397                 }
398                 if(valid) {
399                         points.push_back(p);
400                         return true;
401                 }
402         }
403         return false;
404 }