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