X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fmain.cc;h=2727f4b1d546f4eab7fd2022bff0ef78027f018c;hp=50eea418a7b8aff72e0c93ff0986feba69e5f041;hb=63d7f3b0e70ab5e3d530c579b1881967c96b0b92;hpb=fcd6773bc770425ede4b47a84eef7cc78342542c diff --git a/src/main.cc b/src/main.cc index 50eea41..2727f4b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,12 +3,15 @@ #include #include +#include + #include "global.h" /* TODO: fix those */ #include "camera.h" #include "mesh.h" #include "object.h" +#include "renderer.h" #include "scene.h" #include "shader_manager.h" @@ -22,33 +25,37 @@ static void cleanup(); 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); + +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; + +bool use_vulkan = false; +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 phi = 25; -static float theta = 0; -static float dist = 4; - -// TODO: remove just for test: -static Scene scene; +static float aspect; +static Scene *scene; +static OrbitCamera *camera; +static Renderer *rdefault; // default renderer int main(int argc, char **argv) { for(int i=0; iset_orbit_params(phi, theta, dist); - if(!scene.load("data/spot/spot_control_mesh.obj")) { + scene = new Scene; + if(!scene->load("data/spot/spot.obj")) { fprintf(stderr, "Failed to load scene.\n"); return false; } - for(size_t i=0; icamera = camera; + rdefault->scene = scene; + + if(!rdefault->create()) { + fprintf(stderr, "Failed to create default renderer.\n"); + return false; + } + +// TODO delete: debugging + for(size_t i=0; iobjects.size(); ++i) { printf("object: %d\n", (int)i); - printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str()); - printf("material: %s\n", scene.objects[i]->material->name.c_str()); + printf("mesh: %s\n", scene->objects[i]->mesh->name.c_str()); + printf("material: %s\n", scene->objects[i]->material->name.c_str()); printf("transform:\n"); - scene.objects[i]->transform.print(); + scene->objects[i]->transform.print(); } return true; } @@ -122,6 +138,7 @@ static void cleanup() { delete sdr_man; delete camera; + delete rdefault; if(use_vulkan) { cleanup_vulkan(); @@ -131,7 +148,7 @@ 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); @@ -139,47 +156,58 @@ static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mod } static double prev_x, prev_y; -static int button; +static bool button[8]; -static void motion_clbk(GLFWwindow *win, double x, double y) +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; + double dx = x - prev_x; + double dy = y - prev_y; + + prev_x = x; + prev_y = y; + + if(button[0]) { + theta += dx; + phi += dy; if(phi < -90) phi = -90; if(phi > 90) phi = 90; + } - break; - - case GLFW_MOUSE_BUTTON_RIGHT: - dist *= (y - prev_y) * 0.01 + 1; + if(button[1]) { + dist += dy; if(dist < 0.0) { dist = 0.0; } - break; } - - prev_x = x; - prev_y = y; } -static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods) +static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods) { - button = bn; + button[bn] = action == GLFW_PRESS; glfwGetCursorPos(win, &prev_x, &prev_y); } -static void display() +static void clbk_reshape(GLFWwindow *win, int width, int height) { if(use_vulkan) { - display_vulkan(); + //TODO + return; } else { - display_opengl(); - scene.objects[0]->mesh->draw(); + glViewport(0, 0, width, height); + aspect = (float)width / (float)height; + mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0); } + + win_h = height; + win_w = width; +} + +static void display() +{ + camera->set_orbit_params(phi, theta, dist); + rdefault->draw(); } \ No newline at end of file