X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fmain.cc;h=e5572e25f94cf8342f1fe230e1aafed9d07dff92;hp=7061bb67509bae8b5d369b75759f2a1960711d9a;hb=fd601d4218b63fdf92c5e4dfa32eac8adbda82fa;hpb=d1a2cf93cf54898bb4ad442da94fc61f2617cf9f diff --git a/src/main.cc b/src/main.cc index 7061bb6..e5572e2 100644 --- a/src/main.cc +++ b/src/main.cc @@ -16,6 +16,7 @@ #include "object.h" #include "renderer.h" #include "scene.h" +#include "terrain.h" #include "texture.h" #include "opengl/opengl.h" @@ -44,7 +45,7 @@ int win_h = 600; float phi = 25; float theta = 0; -float dist = 4; +float dist = 16; ShaderManager *sdr_man; @@ -53,12 +54,17 @@ ShaderManager *sdr_man; static float aspect; static OrbitCamera *camera; -static Scene *scene_cow; -static Renderer *rcow; +static Scene *cow_scene; +static Renderer *cow_rend; -static Scene *scene_ground; -static Renderer *rground; // default renderer -static Texture *gskybox; +static Scene *ground_scene; +static Renderer *ground_rend; // default renderer +static Texture *skybox_tex; + +static Renderer *terrain_rend; +static Material terrain_mat; +static Texture *terrain_tex; +static Terrain terrain; /* *** */ @@ -114,50 +120,93 @@ static bool init(Gfx_API api) sdr_man = new ShaderManager; camera = new OrbitCamera; - camera->set_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; - } + // ground_scene = new Scene; + // if(!ground_scene->load("data/ground.obj")) { + // fprintf(stderr, "Failed to load scene: ground.obj.\n"); + // return false; + // } + + // ground_rend = new Renderer; + // ground_rend->camera = camera; + // ground_rend->scene = ground_scene; - scene_cow = new Scene; - if(!scene_cow->load("data/spot/spot.obj")) { + // if(!ground_rend->create()) { + // fprintf(stderr, "Failed to create default renderer.\n"); + // return false; + // } + + // skybox_tex = gfx_create_texture(); + // skybox_tex->load("data/cubemap/cubemap.hdr"); + // ground_rend->set_sky_tex(skybox_tex); + + cow_scene = new Scene; + if(!cow_scene->load("data/spot/spot.obj")) { fprintf(stderr, "Failed to load scene: spot.obj.\n"); return false; } - rground = new Renderer; - rground->camera = camera; - rground->scene = scene_ground; + cow_rend = new Renderer; + cow_rend->camera = camera; + cow_rend->scene = cow_scene; - if(!rground->create()) { - fprintf(stderr, "Failed to create default renderer.\n"); + if(!cow_rend->create()) { + fprintf(stderr, "Failed to create renderer for cows.\n"); return false; } - gskybox = gfx_create_texture(); - gskybox->load("data/cubemap/cubemap.jpg"); - rground->set_sky_tex(gskybox); + terrain_tex = gfx_create_texture(); + if(!terrain_tex->load("data/grass.jpeg")) { + fprintf(stderr, "Failed to load ground texture.\n"); + return false; + } - rcow = new Renderer; - rcow->camera = camera; - rcow->scene = scene_cow; + Image ter_hmap; + if(!ter_hmap.load("data/terhmap.png")) { + fprintf(stderr, "Failed to load terrain heightmap.\n"); + return false; + } - if(!rcow->create()) { - fprintf(stderr, "Failed to create renderer for cows.\n"); + TerrainParams p; + p.xsz = 50; + p.ysz = 50; + p.max_height = 5; + 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 = &ter_hmap; + + terrain.init(); + terrain.generate(p); + + terrain_mat.diffuse = Vec3(1, 1, 1); + terrain_mat.specular = Vec3(0, 0, 0); + terrain_mat.shininess = 40; + terrain_mat.dtex = terrain_tex; + terrain_mat.name = "tt"; + + terrain.material = terrain_mat; + + terrain_rend = new Renderer; + terrain_rend->camera = camera; + terrain_rend->scene = terrain.get_visible(camera); + if(!terrain_rend->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(); - } + // for(size_t i=0; iobjects.size(); ++i) { + // printf("object: %d\n", (int)i); + // printf("mesh: %s\n", ground_scene->objects[i]->mesh->name.c_str()); + // printf("material: %s\n", ground_scene->objects[i]->material->name.c_str()); + // printf("transform:\n"); + // ground_scene->objects[i]->transform.print(); + // } return true; } @@ -166,12 +215,15 @@ static void cleanup() delete sdr_man; delete camera; - delete scene_cow; - delete rcow; + delete cow_scene; + delete cow_rend; - delete scene_ground; - delete rground; + // delete ground_scene; + // delete ground_rend; +//TODO + delete terrain_tex; + delete terrain_rend; gfx_cleanup(); } @@ -194,11 +246,11 @@ static void clbk_motion(GLFWwindow *win, double x, double y) prev_y = y; if(button[0]) { - theta += dx; - phi += dy; + theta += dx * 0.5; + phi += dy * 0.5; - if(phi < -90) - phi = -90; + if(phi < 0) + phi = 0; if(phi > 90) phi = 90; } @@ -229,10 +281,12 @@ static void clbk_reshape(GLFWwindow *win, int width, int height) static void display() { - camera->set_orbit_params(phi, theta, dist); + camera->set_orbit_params(theta, phi, dist); - gfx_clear(0.76, 0.3, 0.43); + // gfx_clear(0.76, 0.3, 0.43); + gfx_clear(0.1, 0.1, 0.1); - rground->draw(); - rcow->draw(); + terrain_rend->draw(); +// ground_rend->draw(); + cow_rend->draw(); } \ No newline at end of file