4e2c72fb853b26240a6590e7c21a8fd44e128143
[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 = 400;
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                 glfwSwapBuffers(win);
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 = 200;
150         p.ysz = 200;
151         p.max_height = 30;
152         p.xtiles = 40;
153         p.ytiles = 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                 default:
275                         break;
276                 }
277         }
278
279         if(key < 256) {
280                 keystate[key] = action == GLFW_PRESS;
281         }
282 }
283
284 static double prev_x, prev_y;
285 static bool button[8];
286
287 static void clbk_motion(GLFWwindow *win, double x, double y)
288 {
289         double dx = x - prev_x;
290         double dy = y - prev_y;
291
292         prev_x = x;
293         prev_y = y;
294
295         if(button[0]) {
296                 cam_theta += dx * 0.5;
297                 cam_phi += dy * 0.5;
298
299                 if(cam_phi < 0)
300                         cam_phi = 0;
301                 if(cam_phi > 90)
302                         cam_phi = 90;
303         }
304
305         if(button[1]) {
306                 cam_dist += dy * 0.1;
307                 if(cam_dist < 0.0) {
308                         cam_dist = 0.0;
309                 }
310         }
311 }
312
313 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
314 {
315         button[bn] = action == GLFW_PRESS;
316         glfwGetCursorPos(win, &prev_x, &prev_y);
317 }
318
319 static void clbk_reshape(GLFWwindow *win, int width, int height)
320 {
321         gfx_reshape(width, height);
322         gfx_viewport(0, 0, width, height);
323         aspect = (float)width / (float)height;
324         mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
325
326         win_h = height;
327         win_w = width;
328 }
329
330 static void update(float dt)
331 {
332         Vec3 dir;
333
334         if(keystate['D'])
335                 dir.x += cow_speed * dt;
336         if(keystate['A'])
337                 dir.x -= cow_speed * dt;
338         if(keystate['W'])
339                 dir.z -= cow_speed * dt;
340         if(keystate['S'])
341                 dir.z += cow_speed * dt;
342
343         Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
344         float theta = cam_theta / 180.0 * M_PI;
345         pos->x += dir.x * cos(theta) - dir.z * sin(theta);
346         pos->z += dir.x * sin(theta) + dir.z * cos(theta);
347 }
348
349 static void display()
350 {
351         static float prev_tsec;
352         time_sec = glfwGetTime();
353         float dt = time_sec - prev_tsec;
354         prev_tsec = time_sec;
355
356         update(dt);
357
358         cam_pos.y = terrain.get_height(cam_pos) + 0.5;
359         camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
360         camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
361
362         gfx_clear(0.1, 0.1, 0.1);
363
364         terrain_rend->draw();
365         cow_rend->draw();
366 }
367
368 static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius)
369 {
370         /* poisson radius */
371         for(int i = 0; i < 1000; i++) {
372                 float angle = (float)rand() / (float)RAND_MAX * 2 * M_PI;
373                 float r = sqrt((float)rand() / (float)RAND_MAX) * radius;
374
375                 Vec2 p;
376                 p.x = cos(angle) * r;
377                 p.y = sin(angle) * r;
378
379                 bool valid = true;
380                 for(size_t j=0; j<points.size(); j++) {
381                         if(length_sq(points[j] - p) < min_dist * min_dist) {
382                                 valid = false;
383                                 break;
384                         }
385                 }
386                 if(valid) {
387                         points.push_back(p);
388                         return true;
389                 }
390         }
391         return false;
392 }