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;
122 camera->set_orbit_params(phi, theta, dist);
124 // scene_ground = new Scene;
125 // if(!scene_ground->load("data/ground.obj")) {
126 // fprintf(stderr, "Failed to load scene: ground.obj.\n");
130 // rground = new Renderer;
131 // rground->camera = camera;
132 // rground->scene = scene_ground;
134 // if(!rground->create()) {
135 // fprintf(stderr, "Failed to create default renderer.\n");
139 // gskybox = gfx_create_texture();
140 // gskybox->load("data/cubemap/cubemap.hdr");
141 // rground->set_sky_tex(gskybox);
143 scene_cow = new Scene;
144 if(!scene_cow->load("data/spot/spot.obj")) {
145 fprintf(stderr, "Failed to load scene: spot.obj.\n");
150 rcow->camera = camera;
151 rcow->scene = scene_cow;
153 if(!rcow->create()) {
154 fprintf(stderr, "Failed to create renderer for cows.\n");
168 p.coarse_heightmap = 0;
173 tmat.diffuse = Vec3(1, 0, 0);
174 tmat.specular = Vec3(0.5, 0, 0);
183 tr->scene = t.get_visible(camera);
185 fprintf(stderr, "terrain fail\n");
190 // TODO delete: debugging
191 // for(size_t i=0; i<scene_ground->objects.size(); ++i) {
192 // printf("object: %d\n", (int)i);
193 // printf("mesh: %s\n", scene_ground->objects[i]->mesh->name.c_str());
194 // printf("material: %s\n", scene_ground->objects[i]->material->name.c_str());
195 // printf("transform:\n");
196 // scene_ground->objects[i]->transform.print();
201 static void cleanup()
209 // delete scene_ground;
217 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
219 if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
220 glfwSetWindowShouldClose(win, GLFW_TRUE);
224 static double prev_x, prev_y;
225 static bool button[8];
227 static void clbk_motion(GLFWwindow *win, double x, double y)
229 double dx = x - prev_x;
230 double dy = y - prev_y;
253 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
255 button[bn] = action == GLFW_PRESS;
256 glfwGetCursorPos(win, &prev_x, &prev_y);
259 static void clbk_reshape(GLFWwindow *win, int width, int height)
261 gfx_viewport(0, 0, width, height);
262 aspect = (float)width / (float)height;
263 mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
269 static void display()
271 camera->set_orbit_params(phi, theta, dist);
273 // gfx_clear(0.76, 0.3, 0.43);
274 gfx_clear(0.1, 0.1, 0.1);