2 #include <GLFW/glfw3.h>
9 #include <gmath/gmath.h>
17 #include "morph_renderer.h"
23 #include "opengl/opengl.h"
24 #include "vulkan/vk.h"
26 /* static functions */
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);
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);
40 /* global variables */
48 ShaderManager *sdr_man;
53 static bool move_camera;
55 static float cam_phi = 25;
56 static float cam_theta = 0;
57 static float cam_dist = 16;
61 static OrbitCamera *camera;
63 static float fog_density;
65 static int num_cows = 1;
66 static float cow_gap = 4;
67 static Scene *cow_scene;
68 static MorphRenderer *cow_rend;
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;
81 int main(int argc, char **argv)
85 for(int i=1; i<argc; i++) {
86 if(strcmp(argv[i], "-opengl") == 0) {
88 printf("Backend: OpenGL.\n");
90 else if(strcmp(argv[i], "-vulkan") == 0) {
92 printf("Backend: Vulkan.\n");
96 printf("No backend specified. Using OpenGL.\n");
101 fprintf(stderr, "Failed to initialize program.\n");
108 glfwSetKeyCallback(win, clbk_key);
109 glfwSetCursorPosCallback(win, clbk_motion);
110 glfwSetMouseButtonCallback(win, clbk_mouse);
111 glfwSetWindowSizeCallback(win, clbk_reshape);
113 glfwGetWindowSize(win, &win_w, &win_h);
114 clbk_reshape(win, win_w, win_h);
116 while(!glfwWindowShouldClose(win)) {
127 static bool init(Gfx_API api)
132 fog_density = 0.0037;
134 sdr_man = new ShaderManager;
136 camera = new OrbitCamera;
138 terrain_tex = gfx_create_texture();
139 if(!terrain_tex->load("data/grass.jpeg")) {
140 fprintf(stderr, "Failed to load ground grass texture.\n");
144 dirt_tex = gfx_create_texture();
145 if(!dirt_tex->load("data/dirt.jpg")) {
146 fprintf(stderr, "Failed to load ground dirt texture.\n");
151 if(!ter_hmap.load("data/terhmap.png")) {
152 fprintf(stderr, "Failed to load terrain heightmap.\n");
158 p.max_height = 3;//30;
165 p.coarse_heightmap = ter_hmap;
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";
176 terrain.material = terrain_mat;
178 terrain_rend = new Renderer;
179 terrain_rend->camera = camera;
180 terrain_rend->scene = terrain.get_visible(camera);
182 skybox_tex = gfx_create_texture();
183 skybox_tex->load("data/cubemap/cubemap.hdr");
184 terrain_rend->set_sky_tex(skybox_tex);
186 irradiance_tex = gfx_create_texture();
187 irradiance_tex->load("data/cubemap/irradiance.hdr");
188 terrain_rend->set_diffuse_sky_tex(irradiance_tex);
190 if(!terrain_rend->create()) {
191 fprintf(stderr, "terrain fail\n");
194 terrain_rend->fog_density = fog_density;
196 cow_scene = new Scene;
197 if(!cow_scene->load("data/spot/spot.obj")) {
198 fprintf(stderr, "Failed to load scene: spot.obj.\n");
202 cow_rend = new MorphRenderer;
203 cow_rend->camera = camera;
204 cow_rend->scene = cow_scene;
205 cow_rend->fog_density = fog_density;
207 if(!cow_rend->create()) {
208 fprintf(stderr, "Failed to create renderer for cows.\n");
212 /* create cow objects */
213 Object *cow0 = cow_scene->objects[0];
214 cow0->transform.rotation_y(M_PI);
215 cow_scene->objects.clear();
217 float disk_radius = std::min(p.xsz, p.ysz) / 2.0 * 0.65;
218 std::vector<Vec2> cow_pos;
220 for(int i=0; i<num_cows; i++) {
221 Object *cow = new Object;
224 if(!gen_poisson(cow_pos, cow_gap, disk_radius))
226 Vec2 pos = cow_pos.back();
227 float y = terrain.get_height(Vec3(pos.x, 1, pos.y));
229 cow->transform.translate(pos.x, y, pos.y);
230 cow_scene->objects.push_back(cow);
234 printf("generated: %d cows from %d\n", (int)cow_pos.size(), num_cows);
239 static void cleanup()
248 delete irradiance_tex;
255 static float cow_speed = 10;
257 static bool keystate[256];
259 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
261 if(action == GLFW_REPEAT) return;
263 if(action == GLFW_PRESS) {
265 case GLFW_KEY_ESCAPE:
266 glfwSetWindowShouldClose(win, GLFW_TRUE);
270 move_camera = !move_camera;
274 // fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1;
278 // fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0;
286 gfx_wireframe(false);
295 keystate[key] = action == GLFW_PRESS;
299 static double prev_x, prev_y;
300 static bool button[8];
302 static void clbk_motion(GLFWwindow *win, double x, double y)
304 double dx = x - prev_x;
305 double dy = y - prev_y;
311 cam_theta += dx * 0.5;
321 cam_dist += dy * 0.1;
328 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
330 button[bn] = action == GLFW_PRESS;
331 glfwGetCursorPos(win, &prev_x, &prev_y);
334 static void clbk_reshape(GLFWwindow *win, int width, int height)
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);
345 static void update(float dt)
350 dir.x += cow_speed * dt;
352 dir.x -= cow_speed * dt;
354 dir.z -= cow_speed * dt;
356 dir.z += cow_speed * dt;
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);
364 static void display()
366 static float prev_tsec;
367 time_sec = glfwGetTime();
368 float dt = time_sec - prev_tsec;
369 prev_tsec = time_sec;
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);
379 gfx_clear(0.1, 0.1, 0.1);
381 terrain_rend->draw();
387 static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float 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;
395 p.x = cos(angle) * r;
396 p.y = sin(angle) * r;
399 for(size_t j=0; j<points.size(); j++) {
400 if(length_sq(points[j] - p) < min_dist * min_dist) {