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();
33 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
34 static void clbk_motion(GLFWwindow *win, double x, double y);
35 static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
36 static void clbk_reshape(GLFWwindow *win, int width, int height);
38 /* global variables */
46 ShaderManager *sdr_man;
51 static bool move_camera;
53 static float cam_phi = 25;
54 static float cam_theta = 0;
55 static float cam_dist = 16;
59 static OrbitCamera *camera;
61 static float fog_density;
63 static Scene *cow_scene;
64 static Object *cow_object;
65 static MorphRenderer *cow_rend;
67 static Terrain terrain;
68 static Texture *skybox_tex;
69 static Texture *terrain_tex;
70 static Material terrain_mat;
71 static Renderer *terrain_rend;
75 int main(int argc, char **argv)
79 for(int i=0; i<argc; ++i) {
80 if(strcmp(argv[i], "-opengl") == 0) {
82 printf("Backend: OpenGL.\n");
84 else if(strcmp(argv[i], "-vulkan") == 0) {
86 printf("Backend: Vulkan.\n");
90 printf("No backend specified. Using OpenGL.\n");
95 fprintf(stderr, "Failed to initialize program.\n");
99 glfwSetKeyCallback(win, clbk_key);
100 glfwSetCursorPosCallback(win, clbk_motion);
101 glfwSetMouseButtonCallback(win, clbk_mouse);
102 glfwSetWindowSizeCallback(win, clbk_reshape);
104 glfwGetWindowSize(win, &win_w, &win_h);
105 clbk_reshape(win, win_w, win_h);
107 while(!glfwWindowShouldClose(win)) {
110 glfwSwapBuffers(win);
119 static bool init(Gfx_API api)
124 fog_density = 0.0037;
126 sdr_man = new ShaderManager;
128 camera = new OrbitCamera;
130 cow_scene = new Scene;
131 if(!cow_scene->load("data/spot/spot.obj")) {
132 fprintf(stderr, "Failed to load scene: spot.obj.\n");
136 cow_rend = new MorphRenderer;
137 cow_rend->camera = camera;
138 cow_rend->scene = cow_scene;
140 if(!cow_rend->create()) {
141 fprintf(stderr, "Failed to create renderer for cows.\n");
145 cow_object = cow_scene->objects[0];
147 terrain_tex = gfx_create_texture();
148 if(!terrain_tex->load("data/grass.jpeg")) {
149 fprintf(stderr, "Failed to load ground texture.\n");
154 if(!ter_hmap.load("data/terhmap.png")) {
155 fprintf(stderr, "Failed to load terrain heightmap.\n");
169 p.coarse_heightmap = ter_hmap;
174 terrain_mat.diffuse = Vec3(1, 1, 1);
175 terrain_mat.specular = Vec3(0, 0, 0);
176 terrain_mat.shininess = 40;
177 terrain_mat.dtex = terrain_tex;
178 terrain_mat.name = "tt";
180 terrain.material = terrain_mat;
182 terrain_rend = new Renderer;
183 terrain_rend->camera = camera;
184 terrain_rend->scene = terrain.get_visible(camera);
186 skybox_tex = gfx_create_texture();
187 skybox_tex->load("data/cubemap/cubemap.hdr");
188 // skybox_tex->load("data/cubemap/irradiance.hdr");
189 terrain_rend->set_sky_tex(skybox_tex);
191 if(!terrain_rend->create()) {
192 fprintf(stderr, "terrain fail\n");
199 static void cleanup()
213 static float cow_speed = 10;
215 static bool keystate[256];
217 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
219 if(action == GLFW_REPEAT) return;
221 if(action == GLFW_PRESS) {
223 case GLFW_KEY_ESCAPE:
224 glfwSetWindowShouldClose(win, GLFW_TRUE);
228 move_camera = !move_camera;
232 fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1;
236 fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0;
245 keystate[key] = action == GLFW_PRESS;
249 static double prev_x, prev_y;
250 static bool button[8];
252 static void clbk_motion(GLFWwindow *win, double x, double y)
254 double dx = x - prev_x;
255 double dy = y - prev_y;
261 cam_theta += dx * 0.5;
271 cam_dist += dy * 0.1;
278 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
280 button[bn] = action == GLFW_PRESS;
281 glfwGetCursorPos(win, &prev_x, &prev_y);
284 static void clbk_reshape(GLFWwindow *win, int width, int height)
286 gfx_viewport(0, 0, width, height);
287 aspect = (float)width / (float)height;
288 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
294 static void update(float dt)
299 dir.x += cow_speed * dt;
301 dir.x -= cow_speed * dt;
303 dir.z -= cow_speed * dt;
305 dir.z += cow_speed * dt;
307 Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
308 float theta = cam_theta / 180.0 * M_PI;
309 pos->x += dir.x * cos(theta) - dir.z * sin(theta);
310 pos->z += dir.x * sin(theta) + dir.z * cos(theta);
313 static void display()
315 static float prev_tsec;
316 time_sec = glfwGetTime();
317 float dt = time_sec - prev_tsec;
318 prev_tsec = time_sec;
322 cam_pos.y = terrain.get_height(cam_pos) + 0.5;
323 camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
324 camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
326 gfx_clear(0.1, 0.1, 0.1);
328 printf("fog_density: %f\n", fog_density);
329 terrain_rend->fog_density = fog_density;
330 terrain_rend->draw();
332 cow_pos.y = terrain.get_height(cow_pos);
333 cow_object->transform.translation(cow_pos);
334 cow_rend->fog_density = fog_density;