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 *irradiance_tex;
73 static Texture *terrain_tex;
74 static Material terrain_mat;
75 static Renderer *terrain_rend;
79 int main(int argc, char **argv)
83 for(int i=0; i<argc; ++i) {
84 if(strcmp(argv[i], "-opengl") == 0) {
86 printf("Backend: OpenGL.\n");
88 else if(strcmp(argv[i], "-vulkan") == 0) {
90 printf("Backend: Vulkan.\n");
94 printf("No backend specified. Using OpenGL.\n");
99 fprintf(stderr, "Failed to initialize program.\n");
103 glfwSetKeyCallback(win, clbk_key);
104 glfwSetCursorPosCallback(win, clbk_motion);
105 glfwSetMouseButtonCallback(win, clbk_mouse);
106 glfwSetWindowSizeCallback(win, clbk_reshape);
108 glfwGetWindowSize(win, &win_w, &win_h);
109 clbk_reshape(win, win_w, win_h);
111 while(!glfwWindowShouldClose(win)) {
114 glfwSwapBuffers(win);
123 static bool init(Gfx_API api)
128 fog_density = 0.0037;
130 sdr_man = new ShaderManager;
132 camera = new OrbitCamera;
134 terrain_tex = gfx_create_texture();
135 if(!terrain_tex->load("data/grass.jpeg")) {
136 fprintf(stderr, "Failed to load ground texture.\n");
141 if(!ter_hmap.load("data/terhmap.png")) {
142 fprintf(stderr, "Failed to load terrain heightmap.\n");
155 p.coarse_heightmap = ter_hmap;
160 terrain_mat.diffuse = Vec3(1, 1, 1);
161 terrain_mat.specular = Vec3(0, 0, 0);
162 terrain_mat.shininess = 40;
163 terrain_mat.dtex = terrain_tex;
164 terrain_mat.name = "tt";
166 terrain.material = terrain_mat;
168 terrain_rend = new Renderer;
169 terrain_rend->camera = camera;
170 terrain_rend->scene = terrain.get_visible(camera);
172 skybox_tex = gfx_create_texture();
173 skybox_tex->load("data/cubemap/cubemap.hdr");
174 terrain_rend->set_sky_tex(skybox_tex);
176 irradiance_tex = gfx_create_texture();
177 irradiance_tex->load("data/cubemap/irradiance.hdr");
178 terrain_rend->set_diffuse_sky_tex(irradiance_tex);
180 if(!terrain_rend->create()) {
181 fprintf(stderr, "terrain fail\n");
184 terrain_rend->fog_density = fog_density;
186 cow_scene = new Scene;
187 if(!cow_scene->load("data/spot/spot.obj")) {
188 fprintf(stderr, "Failed to load scene: spot.obj.\n");
192 cow_rend = new MorphRenderer;
193 cow_rend->camera = camera;
194 cow_rend->scene = cow_scene;
195 cow_rend->fog_density = fog_density;
197 if(!cow_rend->create()) {
198 fprintf(stderr, "Failed to create renderer for cows.\n");
202 /* create cow objects */
203 Object *cow0 = cow_scene->objects[0];
204 cow0->transform.rotation_y(M_PI);
205 cow_scene->objects.clear();
207 float disk_radius = std::min(p.xsz, p.ysz) / 2.0 * 0.65;
208 std::vector<Vec2> cow_pos;
210 for(int i=0; i<num_cows; i++) {
211 Object *cow = new Object;
214 if (!gen_poisson(cow_pos, cow_gap, disk_radius))
216 Vec2 pos = cow_pos.back();
217 float y = terrain.get_height(Vec3(pos.x, 1, pos.y));
219 cow->transform.translate(pos.x, y, pos.y);
220 cow_scene->objects.push_back(cow);
224 printf("generated: %d cows from %d\n", (int)cow_pos.size(), num_cows);
229 static void cleanup()
238 delete irradiance_tex;
245 static float cow_speed = 10;
247 static bool keystate[256];
249 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
251 if(action == GLFW_REPEAT) return;
253 if(action == GLFW_PRESS) {
255 case GLFW_KEY_ESCAPE:
256 glfwSetWindowShouldClose(win, GLFW_TRUE);
260 move_camera = !move_camera;
264 // fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1;
268 // fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0;
277 keystate[key] = action == GLFW_PRESS;
281 static double prev_x, prev_y;
282 static bool button[8];
284 static void clbk_motion(GLFWwindow *win, double x, double y)
286 double dx = x - prev_x;
287 double dy = y - prev_y;
293 cam_theta += dx * 0.5;
303 cam_dist += dy * 0.1;
310 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
312 button[bn] = action == GLFW_PRESS;
313 glfwGetCursorPos(win, &prev_x, &prev_y);
316 static void clbk_reshape(GLFWwindow *win, int width, int height)
318 gfx_viewport(0, 0, width, height);
319 aspect = (float)width / (float)height;
320 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
326 static void update(float dt)
331 dir.x += cow_speed * dt;
333 dir.x -= cow_speed * dt;
335 dir.z -= cow_speed * dt;
337 dir.z += cow_speed * dt;
339 Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
340 float theta = cam_theta / 180.0 * M_PI;
341 pos->x += dir.x * cos(theta) - dir.z * sin(theta);
342 pos->z += dir.x * sin(theta) + dir.z * cos(theta);
345 static void display()
347 static float prev_tsec;
348 time_sec = glfwGetTime();
349 float dt = time_sec - prev_tsec;
350 prev_tsec = time_sec;
354 cam_pos.y = terrain.get_height(cam_pos) + 0.5;
355 camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
356 camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
358 gfx_clear(0.1, 0.1, 0.1);
360 terrain_rend->draw();
364 static bool gen_poisson(std::vector<Vec2> &points, float min_dist, float radius)
367 for (int i = 0; i < 1000; i++)
369 float angle = (float)rand() / (float)RAND_MAX * 2 * M_PI;
370 float r = sqrt((float)rand() / (float)RAND_MAX) * radius;
373 p.x = cos(angle) * r;
374 p.y = sin(angle) * r;
377 for(size_t j=0; j<points.size(); j++) {
378 if(length_sq(points[j] - p) < min_dist * min_dist) {