8 #include <gmath/gmath.h>
20 #include "opengl/opengl.h"
21 #include "vulkan/vk.h"
23 /* static functions */
25 static bool init(Gfx_API api);
26 static void cleanup();
27 static void display();
31 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
32 static void clbk_motion(GLFWwindow *win, double x, double y);
33 static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
34 static void clbk_reshape(GLFWwindow *win, int width, int height);
36 /* global variables */
48 ShaderManager *sdr_man;
53 static OrbitCamera *camera;
55 static Scene *scene_cow;
56 static Renderer *rcow;
58 static Scene *scene_ground;
59 static Renderer *rground; // default renderer
63 int main(int argc, char **argv)
67 for(int i=0; i<argc; ++i) {
68 if(strcmp(argv[i], "-opengl") == 0) {
70 printf("Backend: OpenGL.\n");
72 else if(strcmp(argv[i], "-vulkan") == 0) {
74 printf("Backend: Vulkan.\n");
78 printf("No backend specified. Using OpenGL.\n");
83 fprintf(stderr, "Failed to initialize program.\n");
87 glfwSetKeyCallback(win, clbk_key);
88 glfwSetCursorPosCallback(win, clbk_motion);
89 glfwSetMouseButtonCallback(win, clbk_mouse);
90 glfwSetWindowSizeCallback(win, clbk_reshape);
92 glfwGetWindowSize(win, &win_w, &win_h);
93 clbk_reshape(win, win_w, win_h);
95 while(!glfwWindowShouldClose(win)) {
107 static bool init(Gfx_API api)
112 sdr_man = new ShaderManager;
114 camera = new OrbitCamera;
115 camera->set_orbit_params(phi, theta, dist);
117 scene_ground = new Scene;
118 if(!scene_ground->load("data/ground.obj")) {
119 fprintf(stderr, "Failed to load scene: ground.obj.\n");
123 scene_cow = new Scene;
124 if(!scene_cow->load("data/spot/spot.obj")) {
125 fprintf(stderr, "Failed to load scene: spot.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");
139 rcow->camera = camera;
140 rcow->scene = scene_cow;
142 if(!rcow->create()) {
143 fprintf(stderr, "Failed to create renderer for cows.\n");
147 // TODO delete: debugging
148 for(size_t i=0; i<scene_ground->objects.size(); ++i) {
149 printf("object: %d\n", (int)i);
150 printf("mesh: %s\n", scene_ground->objects[i]->mesh->name.c_str());
151 printf("material: %s\n", scene_ground->objects[i]->material->name.c_str());
152 printf("transform:\n");
153 scene_ground->objects[i]->transform.print();
158 static void cleanup()
172 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
174 if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
175 glfwSetWindowShouldClose(win, GLFW_TRUE);
179 static double prev_x, prev_y;
180 static bool button[8];
182 static void clbk_motion(GLFWwindow *win, double x, double y)
184 double dx = x - prev_x;
185 double dy = y - prev_y;
208 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
210 button[bn] = action == GLFW_PRESS;
211 glfwGetCursorPos(win, &prev_x, &prev_y);
214 static void clbk_reshape(GLFWwindow *win, int width, int height)
216 gfx_viewport(0, 0, width, height);
217 aspect = (float)width / (float)height;
218 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
224 static void display()
226 camera->set_orbit_params(phi, theta, dist);
228 gfx_clear(0.76, 0.3, 0.43);