X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fmain.cc;h=6d7f4e710e6b911273b5872cff5824ba46f98d3c;hb=3bf3536271e4afa8a8a93c75e2ab1256c0bf718a;hp=483454827981a8dade869d617717ad2ceb9fff8f;hpb=826b0cf7adaf8b1dc4c37f57a4a98c79a3995e21;p=demo diff --git a/src/main.cc b/src/main.cc index 4834548..6d7f4e7 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,12 +1,20 @@ +#include + #include #include #include #include +#include + +#include "gfxapi.h" +#include "global.h" + /* TODO: fix those */ #include "camera.h" #include "mesh.h" #include "object.h" +#include "renderer.h" #include "scene.h" #include "opengl/opengl.h" @@ -14,52 +22,75 @@ /* static functions */ -static bool init(); +static bool init(Gfx_API api); 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; + +Mat4 mprojection; GLFWwindow *win; int win_w = 800; int win_h = 600; -Camera *camera; +float phi = 25; +float theta = 0; +float dist = 4; + +ShaderManager *sdr_man; /* variables */ -// TODO: remove just for test: -static Scene scene; + +static float aspect; +static OrbitCamera *camera; + +static Scene *scene_cow; +static Renderer *rcow; + +static Scene *scene_ground; +static Renderer *rground; // default renderer + +/* *** */ int main(int argc, char **argv) { + Gfx_API api; + for(int i=0; iset_orbit_params(phi, theta, dist); + + scene_ground = new Scene; + if(!scene_ground->load("data/ground.obj")) { + fprintf(stderr, "Failed to load scene: ground.obj.\n"); + return false; } - else { - if(!init_opengl()) - return false; + + scene_cow = new Scene; + if(!scene_cow->load("data/spot/spot.obj")) { + fprintf(stderr, "Failed to load scene: spot.obj.\n"); + return false; } - camera = new Camera(25, 25, 4, 45); + rground = new Renderer; + rground->camera = camera; + rground->scene = scene_ground; - if(!scene.load("data/spot/spot_control_mesh.obj")) { - fprintf(stderr, "Failed to load scene.\n"); + if(!rground->create()) { + fprintf(stderr, "Failed to create default renderer.\n"); return false; } - for(size_t i=0; icamera = camera; + rcow->scene = scene_cow; + + if(!rcow->create()) { + fprintf(stderr, "Failed to create renderer for cows.\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_ground->objects[i]->mesh->name.c_str()); + printf("material: %s\n", scene_ground->objects[i]->material->name.c_str()); printf("transform:\n"); - scene.objects[i]->transform.print(); + scene_ground->objects[i]->transform.print(); } return true; } static void cleanup() { - if(use_vulkan) { - cleanup_vulkan(); - } - else { - cleanup_opengl(); - } - + delete sdr_man; delete camera; + + delete scene_cow; + delete rcow; + + delete scene_ground; + delete rground; + + gfx_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); @@ -120,43 +177,56 @@ 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 bool button[8]; -static void motion_clbk(GLFWwindow *win, double x, double y) +static void clbk_motion(GLFWwindow *win, double x, double y) { - int dx = x - prev_x; - int dy = y - prev_y; + double dx = x - prev_x; + double dy = y - prev_y; prev_x = x; prev_y = y; - if(!dx && !dy) return; + if(button[0]) { + theta += dx; + phi += dy; - 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(phi < -90) + phi = -90; + if(phi > 90) + phi = 90; } - if(bnstate[2]) { - camera->distance += dy * 0.1; - if(camera->distance < 0.0) camera->distance = 0.0; + + if(button[1]) { + dist += dy * 0.1; + if(dist < 0.0) { + dist = 0.0; + } } } -static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods) +static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods) { - bnstate[bn - GLFW_MOUSE_BUTTON_LEFT] = action == GLFW_PRESS ? 1 : 0; + 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(); - } - else { - display_opengl(); - } + gfx_viewport(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); + + gfx_clear(0.76, 0.3, 0.43); + + rground->draw(); + rcow->draw(); +} \ No newline at end of file