8 #include <gmath/gmath.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 */
47 ShaderManager *sdr_man;
50 static bool move_camera;
52 static float cam_phi = 25;
53 static float cam_theta = 0;
54 static float cam_dist = 16;
58 static OrbitCamera *camera;
60 static Scene *cow_scene;
61 static Object *cow_object;
62 static Renderer *cow_rend;
64 static Terrain terrain;
65 static Texture *skybox_tex;
66 static Texture *terrain_tex;
67 static Material terrain_mat;
68 static Renderer *terrain_rend;
72 int main(int argc, char **argv)
76 for(int i=0; i<argc; ++i) {
77 if(strcmp(argv[i], "-opengl") == 0) {
79 printf("Backend: OpenGL.\n");
81 else if(strcmp(argv[i], "-vulkan") == 0) {
83 printf("Backend: Vulkan.\n");
87 printf("No backend specified. Using OpenGL.\n");
92 fprintf(stderr, "Failed to initialize program.\n");
96 glfwSetKeyCallback(win, clbk_key);
97 glfwSetCursorPosCallback(win, clbk_motion);
98 glfwSetMouseButtonCallback(win, clbk_mouse);
99 glfwSetWindowSizeCallback(win, clbk_reshape);
101 glfwGetWindowSize(win, &win_w, &win_h);
102 clbk_reshape(win, win_w, win_h);
104 while(!glfwWindowShouldClose(win)) {
107 glfwSwapBuffers(win);
116 static bool init(Gfx_API api)
121 sdr_man = new ShaderManager;
123 camera = new OrbitCamera;
125 cow_scene = new Scene;
126 if(!cow_scene->load("data/spot/spot.obj")) {
127 fprintf(stderr, "Failed to load scene: spot.obj.\n");
131 cow_rend = new Renderer;
132 cow_rend->camera = camera;
133 cow_rend->scene = cow_scene;
135 if(!cow_rend->create()) {
136 fprintf(stderr, "Failed to create renderer for cows.\n");
140 cow_object = cow_scene->objects[0];
142 terrain_tex = gfx_create_texture();
143 if(!terrain_tex->load("data/grass.jpeg")) {
144 fprintf(stderr, "Failed to load ground texture.\n");
149 if(!ter_hmap.load("data/terhmap.png")) {
150 fprintf(stderr, "Failed to load terrain heightmap.\n");
164 p.coarse_heightmap = ter_hmap;
169 terrain_mat.diffuse = Vec3(1, 1, 1);
170 terrain_mat.specular = Vec3(0, 0, 0);
171 terrain_mat.shininess = 40;
172 terrain_mat.dtex = terrain_tex;
173 terrain_mat.name = "tt";
175 terrain.material = terrain_mat;
177 terrain_rend = new Renderer;
178 terrain_rend->camera = camera;
179 terrain_rend->scene = terrain.get_visible(camera);
181 skybox_tex = gfx_create_texture();
182 skybox_tex->load("data/cubemap/cubemap.hdr");
183 terrain_rend->set_sky_tex(skybox_tex);
185 if(!terrain_rend->create()) {
186 fprintf(stderr, "terrain fail\n");
193 static void cleanup()
207 static float cow_speed = 10;
209 static bool keystate[256];
211 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
213 if(action == GLFW_REPEAT) return;
215 if(action == GLFW_PRESS) {
217 case GLFW_KEY_ESCAPE:
218 glfwSetWindowShouldClose(win, GLFW_TRUE);
222 move_camera = !move_camera;
231 keystate[key] = action == GLFW_PRESS;
235 static double prev_x, prev_y;
236 static bool button[8];
238 static void clbk_motion(GLFWwindow *win, double x, double y)
240 double dx = x - prev_x;
241 double dy = y - prev_y;
247 cam_theta += dx * 0.5;
257 cam_dist += dy * 0.1;
264 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
266 button[bn] = action == GLFW_PRESS;
267 glfwGetCursorPos(win, &prev_x, &prev_y);
270 static void clbk_reshape(GLFWwindow *win, int width, int height)
272 gfx_viewport(0, 0, width, height);
273 aspect = (float)width / (float)height;
274 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
280 static void update(float dt)
285 dir.x += cow_speed * dt;
287 dir.x -= cow_speed * dt;
289 dir.z -= cow_speed * dt;
291 dir.z += cow_speed * dt;
293 Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
294 float theta = cam_theta / 180.0 * M_PI;
295 pos->x += dir.x * cos(theta) - dir.z * sin(theta);
296 pos->z += dir.x * sin(theta) + dir.z * cos(theta);
299 static void display()
301 static float prev_tsec;
302 float tsec = glfwGetTime();
303 float dt = tsec - prev_tsec;
308 cam_pos.y = terrain.get_height(cam_pos) + 0.5;
309 camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
310 camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
312 gfx_clear(0.1, 0.1, 0.1);
314 terrain_rend->draw();
316 cow_pos.y = terrain.get_height(cow_pos);
317 cow_object->transform.translation(cow_pos);