X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fmain.cc;h=c48b38325b8d812effdcd7d8312febd68366ee57;hp=933bd6247b24529ca682471d52ac121ec3e9e9dc;hb=c99060df01719b24ebd793b149d3c2dc5748cad5;hpb=243eb502c5dc70e586a9e81815234069bf623480 diff --git a/src/main.cc b/src/main.cc index 933bd62..c48b383 100644 --- a/src/main.cc +++ b/src/main.cc @@ -13,8 +13,8 @@ /* TODO: fix those */ #include "camera.h" #include "mesh.h" +#include "morph_renderer.h" #include "object.h" -#include "renderer.h" #include "scene.h" #include "terrain.h" #include "texture.h" @@ -43,27 +43,32 @@ GLFWwindow *win; int win_w = 800; int win_h = 600; -float phi = 25; -float theta = 0; -float dist = 4; - ShaderManager *sdr_man; +double time_sec; + /* variables */ +static bool move_camera; + +static float cam_phi = 25; +static float cam_theta = 0; +static float cam_dist = 16; +static Vec3 cam_pos; static float aspect; static OrbitCamera *camera; -static Scene *scene_cow; -static Renderer *rcow; +static float fog_density; -static Scene *scene_ground; -static Renderer *rground; // default renderer -static Texture *gskybox; +static Scene *cow_scene; +static Object *cow_object; +static MorphRenderer *cow_rend; -static Renderer *tr; -static Material tmat; -static Terrain t; +static Terrain terrain; +static Texture *skybox_tex; +static Texture *terrain_tex; +static Material terrain_mat; +static Renderer *terrain_rend; /* *** */ @@ -116,85 +121,77 @@ static bool init(Gfx_API api) if(!gfx_init(api)) return false; + fog_density = 0.0037; + 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; - // } + 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 MorphRenderer; + cow_rend->camera = camera; + cow_rend->scene = cow_scene; - // if(!rground->create()) { - // fprintf(stderr, "Failed to create default renderer.\n"); - // return false; - // } + if(!cow_rend->create()) { + fprintf(stderr, "Failed to create renderer for cows.\n"); + return false; + } - // gskybox = gfx_create_texture(); - // gskybox->load("data/cubemap/cubemap.hdr"); - // rground->set_sky_tex(gskybox); + cow_object = cow_scene->objects[0]; - scene_cow = new Scene; - if(!scene_cow->load("data/spot/spot.obj")) { - fprintf(stderr, "Failed to load scene: spot.obj.\n"); + 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; - - if(!rcow->create()) { - fprintf(stderr, "Failed to create renderer for cows.\n"); + Image ter_hmap; + if(!ter_hmap.load("data/terhmap.png")) { + fprintf(stderr, "Failed to load terrain heightmap.\n"); return false; } TerrainParams p; - p.xsz = 50; - p.ysz = 50; - p.max_height = 1; - p.xtiles = 1; //40; - p.ytiles = 1; // 40; - p.tile_usub = 8; - p.tile_vsub = 8; + p.xsz = 200; + p.ysz = 200; + p.max_height = 30; + p.xtiles = 40; + p.ytiles = 40; + p.tile_usub = 10; + p.tile_vsub = 10; p.num_octaves = 3; - p.noise_freq = 10; - p.coarse_heightmap = 0; + p.noise_freq = 5; + p.coarse_heightmap = ter_hmap; - t.init(); - t.generate(p); + terrain.init(); + terrain.generate(p); - tmat.diffuse = Vec3(1, 0, 0); - tmat.specular = Vec3(0.5, 0, 0); - tmat.shininess = 40; - tmat.dtex = 0; - tmat.name = "tt"; + 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"; - t.material = tmat; + terrain.material = terrain_mat; - tr = new Renderer; - tr->camera = camera; - tr->scene = t.get_visible(camera); - if(!tr->create()) { + terrain_rend = new Renderer; + terrain_rend->camera = camera; + terrain_rend->scene = terrain.get_visible(camera); + + skybox_tex = gfx_create_texture(); + skybox_tex->load("data/cubemap/cubemap.hdr"); + terrain_rend->set_sky_tex(skybox_tex); + + 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(); - // } return true; } @@ -203,21 +200,48 @@ static void cleanup() delete sdr_man; delete camera; - delete scene_cow; - delete rcow; - - // delete scene_ground; - // delete rground; + delete cow_scene; + delete cow_rend; //TODO - delete tr; + delete terrain_tex; + delete terrain_rend; gfx_cleanup(); } +static float cow_speed = 10; +static Vec3 cow_pos; +static bool keystate[256]; + 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); + if(action == GLFW_REPEAT) return; + + if(action == GLFW_PRESS) { + switch(key) { + case GLFW_KEY_ESCAPE: + glfwSetWindowShouldClose(win, GLFW_TRUE); + return; + + case ' ': + move_camera = !move_camera; + break; + + case 'F': + fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1; + break; + + case 'U': + fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0; + break; + + default: + break; + } + } + + if(key < 256) { + keystate[key] = action == GLFW_PRESS; } } @@ -233,22 +257,22 @@ static void clbk_motion(GLFWwindow *win, double x, double y) prev_y = y; if(button[0]) { - theta += dx; - phi += dy; + cam_theta += dx * 0.5; + cam_phi += dy * 0.5; - if(phi < -90) - phi = -90; - if(phi > 90) - phi = 90; + if(cam_phi < 0) + cam_phi = 0; + if(cam_phi > 90) + cam_phi = 90; } if(button[1]) { - dist += dy * 0.1; - if(dist < 0.0) { - dist = 0.0; + cam_dist += dy * 0.1; + if(cam_dist < 0.0) { + cam_dist = 0.0; } } -} + } static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods) { @@ -266,14 +290,45 @@ static void clbk_reshape(GLFWwindow *win, int width, int height) win_w = width; } +static void update(float dt) +{ + Vec3 dir; + + if(keystate['D']) + dir.x += cow_speed * dt; + if(keystate['A']) + dir.x -= cow_speed * dt; + if(keystate['W']) + dir.z -= cow_speed * dt; + if(keystate['S']) + dir.z += cow_speed * dt; + + Vec3 *pos = move_camera ? &cam_pos : &cow_pos; + float theta = cam_theta / 180.0 * M_PI; + pos->x += dir.x * cos(theta) - dir.z * sin(theta); + pos->z += dir.x * sin(theta) + dir.z * cos(theta); +} + static void display() { - camera->set_orbit_params(phi, theta, dist); + static float prev_tsec; + time_sec = glfwGetTime(); + float dt = time_sec - prev_tsec; + prev_tsec = time_sec; + + update(dt); + + cam_pos.y = terrain.get_height(cam_pos) + 0.5; + camera->set_orbit_params(cam_theta, cam_phi, cam_dist); + camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z); - // gfx_clear(0.76, 0.3, 0.43); gfx_clear(0.1, 0.1, 0.1); - tr->draw(); -// rground->draw(); - // rcow->draw(); + printf("fog_density: %f\n", fog_density); + terrain_rend->fog_density = fog_density; + terrain_rend->draw(); + + cow_pos.y = terrain.get_height(cow_pos); + cow_object->transform.translation(cow_pos); + cow_rend->draw(); } \ No newline at end of file