8 #include <gmath/gmath.h>
16 #include "morph_renderer.h"
22 #include "opengl/opengl.h"
23 #include "vulkan/vk.h"
25 /* static functions */
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);
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);
39 /* global variables */
47 ShaderManager *sdr_man;
52 static bool move_camera;
54 static float cam_phi = 25;
55 static float cam_theta = 0;
56 static float cam_dist = 16;
60 static OrbitCamera *camera;
62 static float fog_density;
64 static int num_cows = 400;
65 static float cow_gap = 4;
66 static Scene *cow_scene;
67 static MorphRenderer *cow_rend;
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;
78 int main(int argc, char **argv)
82 for(int i=0; i<argc; ++i) {
83 if(strcmp(argv[i], "-opengl") == 0) {
85 printf("Backend: OpenGL.\n");
87 else if(strcmp(argv[i], "-vulkan") == 0) {
89 printf("Backend: Vulkan.\n");
93 printf("No backend specified. Using OpenGL.\n");
98 fprintf(stderr, "Failed to initialize program.\n");
102 glfwSetKeyCallback(win, clbk_key);
103 glfwSetCursorPosCallback(win, clbk_motion);
104 glfwSetMouseButtonCallback(win, clbk_mouse);
105 glfwSetWindowSizeCallback(win, clbk_reshape);
107 glfwGetWindowSize(win, &win_w, &win_h);
108 clbk_reshape(win, win_w, win_h);
110 while(!glfwWindowShouldClose(win)) {
113 glfwSwapBuffers(win);
122 static bool init(Gfx_API api)
127 fog_density = 0.0037;
129 sdr_man = new ShaderManager;
131 camera = new OrbitCamera;
133 terrain_tex = gfx_create_texture();
134 if(!terrain_tex->load("data/grass.jpeg")) {
135 fprintf(stderr, "Failed to load ground texture.\n");
140 if(!ter_hmap.load("data/terhmap.png")) {
141 fprintf(stderr, "Failed to load terrain heightmap.\n");
154 p.coarse_heightmap = ter_hmap;
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";
165 terrain.material = terrain_mat;
167 terrain_rend = new Renderer;
168 terrain_rend->camera = camera;
169 terrain_rend->scene = terrain.get_visible(camera);
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);
176 if(!terrain_rend->create()) {
177 fprintf(stderr, "terrain fail\n");
180 terrain_rend->fog_density = fog_density;
182 cow_scene = new Scene;
183 if(!cow_scene->load("data/spot/spot.obj")) {
184 fprintf(stderr, "Failed to load scene: spot.obj.\n");
188 cow_rend = new MorphRenderer;
189 cow_rend->camera = camera;
190 cow_rend->scene = cow_scene;
191 cow_rend->fog_density = fog_density;
193 if(!cow_rend->create()) {
194 fprintf(stderr, "Failed to create renderer for cows.\n");
198 /* create cow objects */
199 Object *cow0 = cow_scene->objects[0];
200 cow0->transform.rotation_y(M_PI);
201 cow_scene->objects.clear();
203 float disk_radius = std::min(p.xsz, p.ysz) / 2.0 * 0.65;
204 std::vector<Vec2> cow_pos;
206 for(int i=0; i<num_cows; i++) {
207 Object *cow = new Object;
210 if (!gen_poisson(cow_pos, cow_gap, disk_radius))
212 Vec2 pos = cow_pos.back();
213 float y = terrain.get_height(Vec3(pos.x, 1, pos.y));
215 cow->transform.translate(pos.x, y, pos.y);
216 cow_scene->objects.push_back(cow);
220 printf("generated: %d cows from %d\n", (int)cow_pos.size(), num_cows);
225 static void cleanup()
238 static float cow_speed = 10;
240 static bool keystate[256];
242 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
244 if(action == GLFW_REPEAT) return;
246 if(action == GLFW_PRESS) {
248 case GLFW_KEY_ESCAPE:
249 glfwSetWindowShouldClose(win, GLFW_TRUE);
253 move_camera = !move_camera;
257 // fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1;
261 // fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0;
270 keystate[key] = action == GLFW_PRESS;
274 static double prev_x, prev_y;
275 static bool button[8];
277 static void clbk_motion(GLFWwindow *win, double x, double y)
279 double dx = x - prev_x;
280 double dy = y - prev_y;
286 cam_theta += dx * 0.5;
296 cam_dist += dy * 0.1;
303 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
305 button[bn] = action == GLFW_PRESS;
306 glfwGetCursorPos(win, &prev_x, &prev_y);
309 static void clbk_reshape(GLFWwindow *win, int width, int height)
311 gfx_viewport(0, 0, width, height);
312 aspect = (float)width / (float)height;
313 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
319 static void update(float dt)
324 dir.x += cow_speed * dt;
326 dir.x -= cow_speed * dt;
328 dir.z -= cow_speed * dt;
330 dir.z += cow_speed * dt;
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);
338 static void display()
340 static float prev_tsec;
341 time_sec = glfwGetTime();
342 float dt = time_sec - prev_tsec;
343 prev_tsec = time_sec;
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);
351 gfx_clear(0.1, 0.1, 0.1);
353 terrain_rend->draw();
357 static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius)
360 for (int i = 0; i < 1000; i++)
362 float angle = (float)rand() / (float)RAND_MAX * 2 * M_PI;
363 float r = sqrt((float)rand() / (float)RAND_MAX) * radius;
366 p.x = cos(angle) * r;
367 p.y = sin(angle) * r;
370 for(size_t j=0; j<points.size(); j++) {
371 if(length_sq(points[j] - p) < min_dist * min_dist) {