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