X-Git-Url: https://eleni.mutantstargoat.com/git/?p=demo;a=blobdiff_plain;f=src%2Fmain.cc;h=1e45c4bee3f2f348dcd685d68df675e66e807cef;hp=2926420e0280f8954b4d237917b3807ade77c245;hb=3082e3c645d1933610b261e6c93c040f32093637;hpb=64e2adbbab48320b6cd792e515b44cea112a3be4 diff --git a/src/main.cc b/src/main.cc index 2926420..1e45c4b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,6 +3,17 @@ #include #include +#include + +#include "global.h" + +/* TODO: fix those */ +#include "camera.h" +#include "mesh.h" +#include "object.h" +#include "scene.h" +#include "shader_manager.h" + #include "opengl/opengl.h" #include "vulkan/vk.h" @@ -13,13 +24,33 @@ static void cleanup(); static void display(); /* glfw callbacks */ -static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods); + +static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods); +static void clbk_motion(GLFWwindow *win, double x, double y); +static void clbk_mouse(GLFWwindow *win, int button, int action, int mods); +static void clbk_reshape(GLFWwindow *win, int width, int height); /* global variables */ + bool use_vulkan; +Mat4 mprojection; + GLFWwindow *win; +int win_w = 800; +int win_h = 600; + +OrbitCamera *camera; +float phi = 25; +float theta = 0; +float dist = 4; + +ShaderManager *sdr_man; /* variables */ +static float aspect; + +// TODO: remove just for test: +static Scene scene; int main(int argc, char **argv) { @@ -42,7 +73,10 @@ int main(int argc, char **argv) return 1; } - glfwSetKeyCallback(win, key_clbk); + glfwSetKeyCallback(win, clbk_key); + glfwSetCursorPosCallback(win, clbk_motion); + glfwSetMouseButtonCallback(win, clbk_mouse); + glfwSetWindowSizeCallback(win, clbk_reshape); while(!glfwWindowShouldClose(win)) { display(); @@ -66,11 +100,32 @@ static bool init() return false; } + sdr_man = new ShaderManager; + + camera = new OrbitCamera; + camera->set_orbit_params(phi, theta, dist); + + if(!scene.load("data/spot/spot_control_mesh.obj")) { + fprintf(stderr, "Failed to load scene.\n"); + return false; + } + +// TODO delete: debugging + for(size_t i=0; imesh->name.c_str()); + printf("material: %s\n", scene.objects[i]->material->name.c_str()); + printf("transform:\n"); + scene.objects[i]->transform.print(); + } return true; } static void cleanup() { + delete sdr_man; + delete camera; + if(use_vulkan) { cleanup_vulkan(); } @@ -79,13 +134,61 @@ static void cleanup() } } -static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods) +static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods) { if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { glfwSetWindowShouldClose(win, GLFW_TRUE); } } +static double prev_x, prev_y; +static int button; + +static void clbk_motion(GLFWwindow *win, double x, double y) +{ + switch(button) { + case GLFW_MOUSE_BUTTON_LEFT: + theta += x - prev_x; + phi += y - prev_y; + + if(phi < -90) + phi = -90; + if(phi > 90) + phi = 90; + + break; + + case GLFW_MOUSE_BUTTON_RIGHT: + dist *= (y - prev_y) * 0.01 + 1; + if(dist < 0.0) { + dist = 0.0; + } + break; + } + + prev_x = x; + prev_y = y; +} + +static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods) +{ + button = bn; + glfwGetCursorPos(win, &prev_x, &prev_y); +} + +static void clbk_reshape(GLFWwindow *win, int width, int height) +{ + if(use_vulkan) { + //TODO + return; + } + else { + glViewport(0, 0, width, height); + aspect = (float)width / (float)height; + mprojection = calc_projection_matrix(1000, 0.5, aspect, 45); + } +} + static void display() { if(use_vulkan) { @@ -93,5 +196,6 @@ static void display() } else { display_opengl(); + scene.objects[0]->mesh->draw(); } -} +} \ No newline at end of file