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