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 Scene *cow_scene;
62 static Object *cow_object;
63 static MorphRenderer *cow_rend;
65 static Terrain terrain;
66 static Texture *skybox_tex;
67 static Texture *terrain_tex;
68 static Material terrain_mat;
69 static Renderer *terrain_rend;
73 int main(int argc, char **argv)
77 for(int i=0; i<argc; ++i) {
78 if(strcmp(argv[i], "-opengl") == 0) {
80 printf("Backend: OpenGL.\n");
82 else if(strcmp(argv[i], "-vulkan") == 0) {
84 printf("Backend: Vulkan.\n");
88 printf("No backend specified. Using OpenGL.\n");
93 fprintf(stderr, "Failed to initialize program.\n");
97 glfwSetKeyCallback(win, clbk_key);
98 glfwSetCursorPosCallback(win, clbk_motion);
99 glfwSetMouseButtonCallback(win, clbk_mouse);
100 glfwSetWindowSizeCallback(win, clbk_reshape);
102 glfwGetWindowSize(win, &win_w, &win_h);
103 clbk_reshape(win, win_w, win_h);
105 while(!glfwWindowShouldClose(win)) {
108 glfwSwapBuffers(win);
117 static bool init(Gfx_API api)
122 sdr_man = new ShaderManager;
124 camera = new OrbitCamera;
126 cow_scene = new Scene;
127 if(!cow_scene->load("data/spot/spot.obj")) {
128 fprintf(stderr, "Failed to load scene: spot.obj.\n");
132 cow_rend = new MorphRenderer;
133 cow_rend->camera = camera;
134 cow_rend->scene = cow_scene;
136 if(!cow_rend->create()) {
137 fprintf(stderr, "Failed to create renderer for cows.\n");
141 cow_object = cow_scene->objects[0];
143 terrain_tex = gfx_create_texture();
144 if(!terrain_tex->load("data/grass.jpeg")) {
145 fprintf(stderr, "Failed to load ground texture.\n");
150 if(!ter_hmap.load("data/terhmap.png")) {
151 fprintf(stderr, "Failed to load terrain heightmap.\n");
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 if(!terrain_rend->create()) {
187 fprintf(stderr, "terrain fail\n");
194 static void cleanup()
208 static float cow_speed = 10;
210 static bool keystate[256];
212 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
214 if(action == GLFW_REPEAT) return;
216 if(action == GLFW_PRESS) {
218 case GLFW_KEY_ESCAPE:
219 glfwSetWindowShouldClose(win, GLFW_TRUE);
223 move_camera = !move_camera;
232 keystate[key] = action == GLFW_PRESS;
236 static double prev_x, prev_y;
237 static bool button[8];
239 static void clbk_motion(GLFWwindow *win, double x, double y)
241 double dx = x - prev_x;
242 double dy = y - prev_y;
248 cam_theta += dx * 0.5;
258 cam_dist += dy * 0.1;
265 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
267 button[bn] = action == GLFW_PRESS;
268 glfwGetCursorPos(win, &prev_x, &prev_y);
271 static void clbk_reshape(GLFWwindow *win, int width, int height)
273 gfx_viewport(0, 0, width, height);
274 aspect = (float)width / (float)height;
275 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
281 static void update(float dt)
286 dir.x += cow_speed * dt;
288 dir.x -= cow_speed * dt;
290 dir.z -= cow_speed * dt;
292 dir.z += cow_speed * dt;
294 Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
295 float theta = cam_theta / 180.0 * M_PI;
296 pos->x += dir.x * cos(theta) - dir.z * sin(theta);
297 pos->z += dir.x * sin(theta) + dir.z * cos(theta);
300 static void display()
302 static float prev_tsec;
303 time_sec = glfwGetTime();
304 float dt = time_sec - prev_tsec;
305 prev_tsec = time_sec;
309 cam_pos.y = terrain.get_height(cam_pos) + 0.5;
310 camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
311 camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
313 gfx_clear(0.1, 0.1, 0.1);
315 terrain_rend->draw();
317 cow_pos.y = terrain.get_height(cow_pos);
318 cow_object->transform.translation(cow_pos);