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 Material terrain_mat;
76 static Renderer *terrain_rend;
80 int main(int argc, char **argv)
84 for(int i=1; i<argc; i++) {
85 if(strcmp(argv[i], "-opengl") == 0) {
87 printf("Backend: OpenGL.\n");
89 else if(strcmp(argv[i], "-vulkan") == 0) {
91 printf("Backend: Vulkan.\n");
95 printf("No backend specified. Using OpenGL.\n");
100 fprintf(stderr, "Failed to initialize program.\n");
107 glfwSetKeyCallback(win, clbk_key);
108 glfwSetCursorPosCallback(win, clbk_motion);
109 glfwSetMouseButtonCallback(win, clbk_mouse);
110 glfwSetWindowSizeCallback(win, clbk_reshape);
112 glfwGetWindowSize(win, &win_w, &win_h);
113 clbk_reshape(win, win_w, win_h);
115 while(!glfwWindowShouldClose(win)) {
126 static bool init(Gfx_API api)
131 fog_density = 0.0037;
133 sdr_man = new ShaderManager;
135 camera = new OrbitCamera;
137 terrain_tex = gfx_create_texture();
138 if(!terrain_tex->load("data/grass.jpeg")) {
139 fprintf(stderr, "Failed to load ground texture.\n");
144 if(!ter_hmap.load("data/terhmap.png")) {
145 fprintf(stderr, "Failed to load terrain heightmap.\n");
151 p.max_height = 3;//30;
158 p.coarse_heightmap = ter_hmap;
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";
169 terrain.material = terrain_mat;
171 terrain_rend = new Renderer;
172 terrain_rend->camera = camera;
173 terrain_rend->scene = terrain.get_visible(camera);
175 skybox_tex = gfx_create_texture();
176 skybox_tex->load("data/cubemap/cubemap.hdr");
177 terrain_rend->set_sky_tex(skybox_tex);
179 irradiance_tex = gfx_create_texture();
180 irradiance_tex->load("data/cubemap/irradiance.hdr");
181 terrain_rend->set_diffuse_sky_tex(irradiance_tex);
183 if(!terrain_rend->create()) {
184 fprintf(stderr, "terrain fail\n");
187 terrain_rend->fog_density = fog_density;
189 cow_scene = new Scene;
190 if(!cow_scene->load("data/spot/spot.obj")) {
191 fprintf(stderr, "Failed to load scene: spot.obj.\n");
195 cow_rend = new MorphRenderer;
196 cow_rend->camera = camera;
197 cow_rend->scene = cow_scene;
198 cow_rend->fog_density = fog_density;
200 if(!cow_rend->create()) {
201 fprintf(stderr, "Failed to create renderer for cows.\n");
205 /* create cow objects */
206 Object *cow0 = cow_scene->objects[0];
207 cow0->transform.rotation_y(M_PI);
208 cow_scene->objects.clear();
210 float disk_radius = std::min(p.xsz, p.ysz) / 2.0 * 0.65;
211 std::vector<Vec2> cow_pos;
213 for(int i=0; i<num_cows; i++) {
214 Object *cow = new Object;
217 if(!gen_poisson(cow_pos, cow_gap, disk_radius))
219 Vec2 pos = cow_pos.back();
220 float y = terrain.get_height(Vec3(pos.x, 1, pos.y));
222 cow->transform.translate(pos.x, y, pos.y);
223 cow_scene->objects.push_back(cow);
227 printf("generated: %d cows from %d\n", (int)cow_pos.size(), num_cows);
232 static void cleanup()
241 delete irradiance_tex;
248 static float cow_speed = 10;
250 static bool keystate[256];
252 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
254 if(action == GLFW_REPEAT) return;
256 if(action == GLFW_PRESS) {
258 case GLFW_KEY_ESCAPE:
259 glfwSetWindowShouldClose(win, GLFW_TRUE);
263 move_camera = !move_camera;
267 // fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1;
271 // fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0;
279 gfx_wireframe(false);
288 keystate[key] = action == GLFW_PRESS;
292 static double prev_x, prev_y;
293 static bool button[8];
295 static void clbk_motion(GLFWwindow *win, double x, double y)
297 double dx = x - prev_x;
298 double dy = y - prev_y;
304 cam_theta += dx * 0.5;
314 cam_dist += dy * 0.1;
321 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
323 button[bn] = action == GLFW_PRESS;
324 glfwGetCursorPos(win, &prev_x, &prev_y);
327 static void clbk_reshape(GLFWwindow *win, int width, int height)
329 gfx_reshape(width, height);
330 gfx_viewport(0, 0, width, height);
331 aspect = (float)width / (float)height;
332 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
338 static void update(float dt)
343 dir.x += cow_speed * dt;
345 dir.x -= cow_speed * dt;
347 dir.z -= cow_speed * dt;
349 dir.z += cow_speed * dt;
351 Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
352 float theta = cam_theta / 180.0 * M_PI;
353 pos->x += dir.x * cos(theta) - dir.z * sin(theta);
354 pos->z += dir.x * sin(theta) + dir.z * cos(theta);
357 static void display()
359 static float prev_tsec;
360 time_sec = glfwGetTime();
361 float dt = time_sec - prev_tsec;
362 prev_tsec = time_sec;
366 cam_pos.y = terrain.get_height(cam_pos) + 0.5;
367 camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
368 camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
372 gfx_clear(0.1, 0.1, 0.1);
374 terrain_rend->draw();
380 static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius)
383 for(int i = 0; i < 1000; i++) {
384 float angle = (float)rand() / (float)RAND_MAX * 2 * M_PI;
385 float r = sqrt((float)rand() / (float)RAND_MAX) * radius;
388 p.x = cos(angle) * r;
389 p.y = sin(angle) * r;
392 for(size_t j=0; j<points.size(); j++) {
393 if(length_sq(points[j] - p) < min_dist * min_dist) {