6 #include <gmath/gmath.h>
16 #include "shader_manager.h"
18 #include "opengl/opengl.h"
19 #include "vulkan/vk.h"
21 /* static functions */
24 static void cleanup();
25 static void display();
29 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
30 static void clbk_motion(GLFWwindow *win, double x, double y);
31 static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
32 static void clbk_reshape(GLFWwindow *win, int width, int height);
34 /* global variables */
36 bool use_vulkan = false;
47 ShaderManager *sdr_man;
52 static OrbitCamera *camera;
53 static Renderer *rdefault; // default renderer
55 int main(int argc, char **argv)
57 for(int i=0; i<argc; ++i) {
58 if(strcmp(argv[i], "-opengl") == 0) {
59 printf("Backend: OpenGL.\n");
61 else if(strcmp(argv[i], "-vulkan") == 0) {
63 printf("Backend: Vulkan.\n");
66 printf("No backend specified. Using OpenGL.\n");
71 fprintf(stderr, "Failed to initialize program.\n");
75 glfwSetKeyCallback(win, clbk_key);
76 glfwSetCursorPosCallback(win, clbk_motion);
77 glfwSetMouseButtonCallback(win, clbk_mouse);
78 glfwSetWindowSizeCallback(win, clbk_reshape);
80 glfwGetWindowSize(win, &win_w, &win_h);
81 clbk_reshape(win, win_w, win_h);
83 while(!glfwWindowShouldClose(win)) {
106 sdr_man = new ShaderManager;
108 camera = new OrbitCamera;
109 camera->set_orbit_params(phi, theta, dist);
112 if(!scene->load("data/spot/spot.obj")) {
113 fprintf(stderr, "Failed to load scene.\n");
117 rdefault = new Renderer;
118 rdefault->camera = camera;
119 rdefault->scene = scene;
121 if(!rdefault->create()) {
122 fprintf(stderr, "Failed to create default renderer.\n");
126 // TODO delete: debugging
127 for(size_t i=0; i<scene->objects.size(); ++i) {
128 printf("object: %d\n", (int)i);
129 printf("mesh: %s\n", scene->objects[i]->mesh->name.c_str());
130 printf("material: %s\n", scene->objects[i]->material->name.c_str());
131 printf("transform:\n");
132 scene->objects[i]->transform.print();
137 static void cleanup()
152 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
154 if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
155 glfwSetWindowShouldClose(win, GLFW_TRUE);
159 static double prev_x, prev_y;
160 static bool button[8];
162 static void clbk_motion(GLFWwindow *win, double x, double y)
164 double dx = x - prev_x;
165 double dy = y - prev_y;
188 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
190 button[bn] = action == GLFW_PRESS;
191 glfwGetCursorPos(win, &prev_x, &prev_y);
194 static void clbk_reshape(GLFWwindow *win, int width, int height)
201 glViewport(0, 0, width, height);
202 aspect = (float)width / (float)height;
203 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
210 static void display()
212 camera->set_orbit_params(phi, theta, dist);