8 #include <gmath/gmath.h>
21 #include "opengl/opengl.h"
22 #include "vulkan/vk.h"
24 /* static functions */
26 static bool init(Gfx_API api);
27 static void cleanup();
28 static void display();
32 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
33 static void clbk_motion(GLFWwindow *win, double x, double y);
34 static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
35 static void clbk_reshape(GLFWwindow *win, int width, int height);
37 /* global variables */
49 ShaderManager *sdr_man;
54 static OrbitCamera *camera;
56 static Scene *scene_cow;
57 static Renderer *rcow;
59 static Scene *scene_ground;
60 static Renderer *rground; // default renderer
61 static Texture *gskybox;
65 int main(int argc, char **argv)
69 for(int i=0; i<argc; ++i) {
70 if(strcmp(argv[i], "-opengl") == 0) {
72 printf("Backend: OpenGL.\n");
74 else if(strcmp(argv[i], "-vulkan") == 0) {
76 printf("Backend: Vulkan.\n");
80 printf("No backend specified. Using OpenGL.\n");
85 fprintf(stderr, "Failed to initialize program.\n");
89 glfwSetKeyCallback(win, clbk_key);
90 glfwSetCursorPosCallback(win, clbk_motion);
91 glfwSetMouseButtonCallback(win, clbk_mouse);
92 glfwSetWindowSizeCallback(win, clbk_reshape);
94 glfwGetWindowSize(win, &win_w, &win_h);
95 clbk_reshape(win, win_w, win_h);
97 while(!glfwWindowShouldClose(win)) {
100 glfwSwapBuffers(win);
109 static bool init(Gfx_API api)
114 sdr_man = new ShaderManager;
116 camera = new OrbitCamera;
117 camera->set_orbit_params(phi, theta, dist);
119 scene_ground = new Scene;
120 if(!scene_ground->load("data/ground.obj")) {
121 fprintf(stderr, "Failed to load scene: ground.obj.\n");
125 scene_cow = new Scene;
126 if(!scene_cow->load("data/spot/spot.obj")) {
127 fprintf(stderr, "Failed to load scene: spot.obj.\n");
131 rground = new Renderer;
132 rground->camera = camera;
133 rground->scene = scene_ground;
135 if(!rground->create()) {
136 fprintf(stderr, "Failed to create default renderer.\n");
140 gskybox = gfx_create_texture();
141 gskybox->load("data/cubemap/cubemap.jpg");
142 rground->set_sky_tex(gskybox);
145 rcow->camera = camera;
146 rcow->scene = scene_cow;
148 if(!rcow->create()) {
149 fprintf(stderr, "Failed to create renderer for cows.\n");
153 // TODO delete: debugging
154 for(size_t i=0; i<scene_ground->objects.size(); ++i) {
155 printf("object: %d\n", (int)i);
156 printf("mesh: %s\n", scene_ground->objects[i]->mesh->name.c_str());
157 printf("material: %s\n", scene_ground->objects[i]->material->name.c_str());
158 printf("transform:\n");
159 scene_ground->objects[i]->transform.print();
164 static void cleanup()
178 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
180 if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
181 glfwSetWindowShouldClose(win, GLFW_TRUE);
185 static double prev_x, prev_y;
186 static bool button[8];
188 static void clbk_motion(GLFWwindow *win, double x, double y)
190 double dx = x - prev_x;
191 double dy = y - prev_y;
214 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
216 button[bn] = action == GLFW_PRESS;
217 glfwGetCursorPos(win, &prev_x, &prev_y);
220 static void clbk_reshape(GLFWwindow *win, int width, int height)
222 gfx_viewport(0, 0, width, height);
223 aspect = (float)width / (float)height;
224 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
230 static void display()
232 camera->set_orbit_params(phi, theta, dist);
234 gfx_clear(0.76, 0.3, 0.43);