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 *scene_cow;
58 static Renderer *rcow;
60 static Scene *scene_ground;
61 static Renderer *rground; // default renderer
62 static Texture *gskybox;
70 int main(int argc, char **argv)
74 for(int i=0; i<argc; ++i) {
75 if(strcmp(argv[i], "-opengl") == 0) {
77 printf("Backend: OpenGL.\n");
79 else if(strcmp(argv[i], "-vulkan") == 0) {
81 printf("Backend: Vulkan.\n");
85 printf("No backend specified. Using OpenGL.\n");
90 fprintf(stderr, "Failed to initialize program.\n");
94 glfwSetKeyCallback(win, clbk_key);
95 glfwSetCursorPosCallback(win, clbk_motion);
96 glfwSetMouseButtonCallback(win, clbk_mouse);
97 glfwSetWindowSizeCallback(win, clbk_reshape);
99 glfwGetWindowSize(win, &win_w, &win_h);
100 clbk_reshape(win, win_w, win_h);
102 while(!glfwWindowShouldClose(win)) {
105 glfwSwapBuffers(win);
114 static bool init(Gfx_API api)
119 sdr_man = new ShaderManager;
121 camera = new OrbitCamera;
123 // scene_ground = new Scene;
124 // if(!scene_ground->load("data/ground.obj")) {
125 // fprintf(stderr, "Failed to load scene: ground.obj.\n");
129 // rground = new Renderer;
130 // rground->camera = camera;
131 // rground->scene = scene_ground;
133 // if(!rground->create()) {
134 // fprintf(stderr, "Failed to create default renderer.\n");
138 // gskybox = gfx_create_texture();
139 // gskybox->load("data/cubemap/cubemap.hdr");
140 // rground->set_sky_tex(gskybox);
142 scene_cow = new Scene;
143 if(!scene_cow->load("data/spot/spot.obj")) {
144 fprintf(stderr, "Failed to load scene: spot.obj.\n");
149 rcow->camera = camera;
150 rcow->scene = scene_cow;
152 if(!rcow->create()) {
153 fprintf(stderr, "Failed to create renderer for cows.\n");
167 p.coarse_heightmap = 0;
172 tmat.diffuse = Vec3(1, 0, 0);
173 tmat.specular = Vec3(0.5, 0, 0);
182 tr->scene = t.get_visible(camera);
184 fprintf(stderr, "terrain fail\n");
189 // TODO delete: debugging
190 // for(size_t i=0; i<scene_ground->objects.size(); ++i) {
191 // printf("object: %d\n", (int)i);
192 // printf("mesh: %s\n", scene_ground->objects[i]->mesh->name.c_str());
193 // printf("material: %s\n", scene_ground->objects[i]->material->name.c_str());
194 // printf("transform:\n");
195 // scene_ground->objects[i]->transform.print();
200 static void cleanup()
208 // delete scene_ground;
216 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
218 if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
219 glfwSetWindowShouldClose(win, GLFW_TRUE);
223 static double prev_x, prev_y;
224 static bool button[8];
226 static void clbk_motion(GLFWwindow *win, double x, double y)
228 double dx = x - prev_x;
229 double dy = y - prev_y;
252 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
254 button[bn] = action == GLFW_PRESS;
255 glfwGetCursorPos(win, &prev_x, &prev_y);
258 static void clbk_reshape(GLFWwindow *win, int width, int height)
260 gfx_viewport(0, 0, width, height);
261 aspect = (float)width / (float)height;
262 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
268 static void display()
270 camera->set_orbit_params(theta, phi, dist);
272 // gfx_clear(0.76, 0.3, 0.43);
273 gfx_clear(0.1, 0.1, 0.1);