X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fmain.cc;h=107fb9d765a53c0a1d02f13d388d573cc991b2e8;hb=47982b199010496e34eefb95044275fb231cba18;hp=1e45c4bee3f2f348dcd685d68df675e66e807cef;hpb=3082e3c645d1933610b261e6c93c040f32093637;p=demo diff --git a/src/main.cc b/src/main.cc index 1e45c4b..107fb9d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -5,21 +7,24 @@ #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 "shader_manager.h" +#include "terrain.h" +#include "texture.h" #include "opengl/opengl.h" #include "vulkan/vk.h" /* static functions */ -static bool init(); +static bool init(Gfx_API api); static void cleanup(); static void display(); @@ -32,43 +37,56 @@ 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; +float dist = 16; ShaderManager *sdr_man; /* variables */ + static float aspect; +static OrbitCamera *camera; + +static Scene *scene_cow; +static Renderer *rcow; -// TODO: remove just for test: -static Scene scene; +static Scene *scene_ground; +static Renderer *rground; // default renderer +static Texture *gskybox; + +static Renderer *tr; +static Material tmat; +static Terrain t; + +/* *** */ int main(int argc, char **argv) { + Gfx_API api; + for(int i=0; iset_orbit_params(phi, theta, dist); - if(!scene.load("data/spot/spot_control_mesh.obj")) { - fprintf(stderr, "Failed to load scene.\n"); + // scene_ground = new Scene; + // if(!scene_ground->load("data/ground.obj")) { + // fprintf(stderr, "Failed to load scene: ground.obj.\n"); + // return false; + // } + + // rground = new Renderer; + // rground->camera = camera; + // rground->scene = scene_ground; + + // if(!rground->create()) { + // fprintf(stderr, "Failed to create default renderer.\n"); + // return false; + // } + + // gskybox = gfx_create_texture(); + // gskybox->load("data/cubemap/cubemap.hdr"); + // rground->set_sky_tex(gskybox); + + scene_cow = new Scene; + if(!scene_cow->load("data/spot/spot.obj")) { + fprintf(stderr, "Failed to load scene: spot.obj.\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(); + rcow = new Renderer; + rcow->camera = camera; + rcow->scene = scene_cow; + + if(!rcow->create()) { + fprintf(stderr, "Failed to create renderer for cows.\n"); + return false; } + + TerrainParams p; + p.xsz = 50; + p.ysz = 50; + p.max_height = 2; + p.xtiles = 20; + p.ytiles = 20; + p.tile_usub = 10; + p.tile_vsub = 10; + p.num_octaves = 3; + p.noise_freq = 10; + p.coarse_heightmap = 0; + + t.init(); + t.generate(p); + + tmat.diffuse = Vec3(1, 0, 0); + tmat.specular = Vec3(0.5, 0, 0); + tmat.shininess = 40; + tmat.dtex = 0; + tmat.name = "tt"; + + t.material = tmat; + + tr = new Renderer; + tr->camera = camera; + tr->scene = t.get_visible(camera); + if(!tr->create()) { + fprintf(stderr, "terrain fail\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_ground->objects[i]->mesh->name.c_str()); + // printf("material: %s\n", scene_ground->objects[i]->material->name.c_str()); + // printf("transform:\n"); + // scene_ground->objects[i]->transform.print(); + // } return true; } @@ -126,12 +202,15 @@ static void cleanup() delete sdr_man; delete camera; - if(use_vulkan) { - cleanup_vulkan(); - } - else { - cleanup_opengl(); - } + delete scene_cow; + delete rcow; + + // delete scene_ground; + // delete rground; + +//TODO + delete tr; + gfx_cleanup(); } static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods) @@ -142,60 +221,58 @@ static void clbk_key(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 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; - if(phi < -90) - phi = -90; + prev_x = x; + prev_y = y; + + if(button[0]) { + theta += dx * 0.5; + phi += dy * 0.5; + + if(phi < 0) + phi = 0; if(phi > 90) phi = 90; + } - break; - - case GLFW_MOUSE_BUTTON_RIGHT: - dist *= (y - prev_y) * 0.01 + 1; + if(button[1]) { + dist += dy * 0.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; + button[bn] = action == GLFW_PRESS; 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); - } + 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() { - if(use_vulkan) { - display_vulkan(); - } - else { - display_opengl(); - scene.objects[0]->mesh->draw(); - } + camera->set_orbit_params(theta, phi, dist); + + // gfx_clear(0.76, 0.3, 0.43); + gfx_clear(0.1, 0.1, 0.1); + + tr->draw(); +// rground->draw(); + // rcow->draw(); } \ No newline at end of file