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 */
50 ShaderManager *sdr_man;
55 static OrbitCamera *camera;
57 static Scene *cow_scene;
58 static Renderer *cow_rend;
60 static Scene *ground_scene;
61 static Renderer *ground_rend; // default renderer
62 static Texture *skybox_tex;
64 static Renderer *terrain_rend;
65 static Material terrain_mat;
66 static Texture *terrain_tex;
67 static Terrain terrain;
71 int main(int argc, char **argv)
75 for(int i=0; i<argc; ++i) {
76 if(strcmp(argv[i], "-opengl") == 0) {
78 printf("Backend: OpenGL.\n");
80 else if(strcmp(argv[i], "-vulkan") == 0) {
82 printf("Backend: Vulkan.\n");
86 printf("No backend specified. Using OpenGL.\n");
91 fprintf(stderr, "Failed to initialize program.\n");
95 glfwSetKeyCallback(win, clbk_key);
96 glfwSetCursorPosCallback(win, clbk_motion);
97 glfwSetMouseButtonCallback(win, clbk_mouse);
98 glfwSetWindowSizeCallback(win, clbk_reshape);
100 glfwGetWindowSize(win, &win_w, &win_h);
101 clbk_reshape(win, win_w, win_h);
103 while(!glfwWindowShouldClose(win)) {
106 glfwSwapBuffers(win);
115 static bool init(Gfx_API api)
120 sdr_man = new ShaderManager;
122 camera = new OrbitCamera;
124 // ground_scene = new Scene;
125 // if(!ground_scene->load("data/ground.obj")) {
126 // fprintf(stderr, "Failed to load scene: ground.obj.\n");
130 // ground_rend = new Renderer;
131 // ground_rend->camera = camera;
132 // ground_rend->scene = ground_scene;
134 // if(!ground_rend->create()) {
135 // fprintf(stderr, "Failed to create default renderer.\n");
139 // skybox_tex = gfx_create_texture();
140 // skybox_tex->load("data/cubemap/cubemap.hdr");
141 // ground_rend->set_sky_tex(skybox_tex);
143 cow_scene = new Scene;
144 if(!cow_scene->load("data/spot/spot.obj")) {
145 fprintf(stderr, "Failed to load scene: spot.obj.\n");
149 cow_rend = new Renderer;
150 cow_rend->camera = camera;
151 cow_rend->scene = cow_scene;
153 if(!cow_rend->create()) {
154 fprintf(stderr, "Failed to create renderer for cows.\n");
158 terrain_tex = gfx_create_texture();
159 if(!terrain_tex->load("data/grass.jpeg")) {
160 fprintf(stderr, "Failed to load ground texture.\n");
165 if(!ter_hmap.load("data/terhmap.png")) {
166 fprintf(stderr, "Failed to load terrain heightmap.\n");
180 p.coarse_heightmap = &ter_hmap;
185 terrain_mat.diffuse = Vec3(1, 1, 1);
186 terrain_mat.specular = Vec3(0, 0, 0);
187 terrain_mat.shininess = 40;
188 terrain_mat.dtex = terrain_tex;
189 terrain_mat.name = "tt";
191 terrain.material = terrain_mat;
193 terrain_rend = new Renderer;
194 terrain_rend->camera = camera;
195 terrain_rend->scene = terrain.get_visible(camera);
196 if(!terrain_rend->create()) {
197 fprintf(stderr, "terrain fail\n");
202 // TODO delete: debugging
203 // for(size_t i=0; i<ground_scene->objects.size(); ++i) {
204 // printf("object: %d\n", (int)i);
205 // printf("mesh: %s\n", ground_scene->objects[i]->mesh->name.c_str());
206 // printf("material: %s\n", ground_scene->objects[i]->material->name.c_str());
207 // printf("transform:\n");
208 // ground_scene->objects[i]->transform.print();
213 static void cleanup()
221 // delete ground_scene;
222 // delete ground_rend;
230 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
232 if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
233 glfwSetWindowShouldClose(win, GLFW_TRUE);
237 static double prev_x, prev_y;
238 static bool button[8];
240 static void clbk_motion(GLFWwindow *win, double x, double y)
242 double dx = x - prev_x;
243 double dy = y - prev_y;
266 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
268 button[bn] = action == GLFW_PRESS;
269 glfwGetCursorPos(win, &prev_x, &prev_y);
272 static void clbk_reshape(GLFWwindow *win, int width, int height)
274 gfx_viewport(0, 0, width, height);
275 aspect = (float)width / (float)height;
276 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
282 static void display()
284 camera->set_orbit_params(theta, phi, dist);
286 // gfx_clear(0.76, 0.3, 0.43);
287 gfx_clear(0.1, 0.1, 0.1);
289 terrain_rend->draw();
290 // ground_rend->draw();