X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain.cc;h=483454827981a8dade869d617717ad2ceb9fff8f;hb=826b0cf7adaf8b1dc4c37f57a4a98c79a3995e21;hp=dc3440d8cb72359ce54f46dccd31658d464de52b;hpb=c47ed466e38c143cc9229c201d3bec42684ddd06;p=demo diff --git a/src/main.cc b/src/main.cc index dc3440d..4834548 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,8 +3,10 @@ #include #include -#include "object.h" +/* TODO: fix those */ +#include "camera.h" #include "mesh.h" +#include "object.h" #include "scene.h" #include "opengl/opengl.h" @@ -18,12 +20,20 @@ static void display(); /* glfw callbacks */ static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods); +static void motion_clbk(GLFWwindow *win, double x, double y); +static void mouse_clbk(GLFWwindow *win, int button, int action, int mods); /* global variables */ bool use_vulkan; + GLFWwindow *win; +int win_w = 800; +int win_h = 600; + +Camera *camera; /* variables */ +// TODO: remove just for test: static Scene scene; int main(int argc, char **argv) @@ -48,6 +58,8 @@ int main(int argc, char **argv) } glfwSetKeyCallback(win, key_clbk); + glfwSetCursorPosCallback(win, motion_clbk); + glfwSetMouseButtonCallback(win, mouse_clbk); while(!glfwWindowShouldClose(win)) { display(); @@ -71,6 +83,8 @@ static bool init() return false; } + camera = new Camera(25, 25, 4, 45); + if(!scene.load("data/spot/spot_control_mesh.obj")) { fprintf(stderr, "Failed to load scene.\n"); return false; @@ -94,6 +108,8 @@ static void cleanup() else { cleanup_opengl(); } + + delete camera; } static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods) @@ -103,6 +119,38 @@ static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mod } } +static double prev_x, prev_y; +static int bnstate[8]; + +static void motion_clbk(GLFWwindow *win, double x, double y) +{ + int dx = x - prev_x; + int dy = y - prev_y; + + prev_x = x; + prev_y = y; + + if(!dx && !dy) return; + + if(bnstate[0]) { + camera->theta += dx * 0.5; + camera->phi += dy * 0.5; + + if(camera->phi < -90) camera->phi = -90; + if(camera->phi > 90) camera->phi = 90; + } + if(bnstate[2]) { + camera->distance += dy * 0.1; + if(camera->distance < 0.0) camera->distance = 0.0; + } +} + +static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods) +{ + bnstate[bn - GLFW_MOUSE_BUTTON_LEFT] = action == GLFW_PRESS ? 1 : 0; + glfwGetCursorPos(win, &prev_x, &prev_y); +} + static void display() { if(use_vulkan) {