From: Eleni Maria Stea Date: Sat, 19 Aug 2017 18:24:43 +0000 (+0300) Subject: fixed terrain tiles generation X-Git-Url: http://git.mutantstargoat.com?p=demo;a=commitdiff_plain;h=47982b199010496e34eefb95044275fb231cba18 fixed terrain tiles generation --- diff --git a/src/camera.cc b/src/camera.cc index fd96f78..73980b8 100644 --- a/src/camera.cc +++ b/src/camera.cc @@ -20,7 +20,7 @@ OrbitCamera::OrbitCamera() OrbitCamera::~OrbitCamera() {} -void OrbitCamera::set_orbit_params(float phi, float theta, float distance) +void OrbitCamera::set_orbit_params(float theta, float phi, float distance) { this->phi = phi; this->theta = theta; diff --git a/src/camera.h b/src/camera.h index 0ded6be..62120f1 100644 --- a/src/camera.h +++ b/src/camera.h @@ -6,7 +6,6 @@ class Camera { public: Camera(); - Camera(float phi, float theta, float distance, float fov); virtual ~Camera(); virtual Mat4 get_view_matrix() const = 0; @@ -21,12 +20,11 @@ protected: public: OrbitCamera(); - OrbitCamera(float theta, float phi, float distance); virtual ~OrbitCamera(); virtual Mat4 get_view_matrix() const override; - void set_orbit_params(float phi, float theta, float distance); + void set_orbit_params(float theta, float phi, float distance); }; Mat4 calc_projection_matrix(float fov_deg, float aspect, float n, float f); diff --git a/src/main.cc b/src/main.cc index 933bd62..107fb9d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -45,7 +45,7 @@ int win_h = 600; float phi = 25; float theta = 0; -float dist = 4; +float dist = 16; ShaderManager *sdr_man; @@ -119,7 +119,6 @@ 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")) { @@ -158,11 +157,11 @@ static bool init(Gfx_API api) 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.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; @@ -233,11 +232,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; } @@ -268,7 +267,7 @@ 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.1, 0.1, 0.1); diff --git a/src/mesh.cc b/src/mesh.cc index 340263a..fde7ac4 100644 --- a/src/mesh.cc +++ b/src/mesh.cc @@ -1,10 +1,28 @@ #include "mesh.h" -Mesh::Mesh() {} +Mesh::Mesh() +{ + vdata_valid = false; +} Mesh::~Mesh() { indices.clear(); vertices.clear(); normals.clear(); +} + +void Mesh::transform(const Mat4 &mat) +{ + Mat4 normal_mat = mat.upper3x3(); + for(size_t i=0; i indices; std::vector vertices; @@ -22,8 +25,6 @@ public: std::vector tex_coords; std::string name; - unsigned int mat_idx; - Mesh(); virtual ~Mesh() = 0; @@ -31,6 +32,8 @@ public: virtual void draw() const = 0; virtual void draw_normals(float scale) const = 0; virtual void update_vertex_data() = 0; + virtual void transform(const Mat4 &mat); + virtual void invalidate(); }; #endif // MESH_H_ \ No newline at end of file diff --git a/src/meshgen.cc b/src/meshgen.cc index 9547cee..c6a4f48 100644 --- a/src/meshgen.cc +++ b/src/meshgen.cc @@ -119,7 +119,7 @@ void gen_geosphere(Mesh *mesh, float rad, int subdiv, bool hemi) for(int i=0; iindices[i] = i; } - mesh->update_vertex_data(); + mesh->invalidate(); } // ------ heightfield ------ @@ -129,7 +129,7 @@ static Vec3 hfield_vertex(float u, float v, float h, float xsz, { float x = u * xsz - xsz / 2.0; float y = h * height; - float z = -(v * ysz - ysz / 2.0); + float z = v * ysz - ysz / 2.0; return Vec3(x, y, z); } @@ -165,9 +165,9 @@ void gen_heightfield(Mesh *mesh, float xsz, float ysz, float height, int usub, float dv = 1.0 / (float)num_vvertices; for(int i=0; iupdate_vertex_data(); + mesh->invalidate(); } diff --git a/src/opengl/mesh-gl.cc b/src/opengl/mesh-gl.cc index f0956b4..dd73bf3 100644 --- a/src/opengl/mesh-gl.cc +++ b/src/opengl/mesh-gl.cc @@ -73,6 +73,9 @@ MeshGL::~MeshGL() void MeshGL::draw() const { + if(!vdata_valid) { + ((MeshGL*)this)->update_vertex_data(); + } glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); diff --git a/src/renderer.cc b/src/renderer.cc index 2f50d0b..093b71e 100644 --- a/src/renderer.cc +++ b/src/renderer.cc @@ -119,10 +119,9 @@ void Renderer::draw_object(Object *object) const Mat4 mview = object->transform * camera->get_view_matrix(); sprog->set_uniform_matrix(mview_loc, mview); - object->mesh->update_vertex_data(); object->mesh->draw(); - /* debug */ + /* debug if(nprog) { int loc = nprog->get_uniform_location("mmviewproj"); if(loc != -1) { @@ -130,7 +129,7 @@ void Renderer::draw_object(Object *object) const } nprog->use(); object->mesh->draw_normals(1.0); - } + }*/ // if(m->dtex) // m->dtex->unbind(); diff --git a/src/scene.cc b/src/scene.cc index 11281c3..0666bd8 100644 --- a/src/scene.cc +++ b/src/scene.cc @@ -205,7 +205,6 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) else fprintf(stderr, "No faces found.\n"); - mesh->mat_idx = amesh->mMaterialIndex; return mesh; } diff --git a/src/terrain.cc b/src/terrain.cc index bdd787f..92dce56 100644 --- a/src/terrain.cc +++ b/src/terrain.cc @@ -32,6 +32,12 @@ void Terrain::destroy() delete vis_scene; } +struct GenData { + const TerrainParams *tp; + float xoffs; + float yoffs; +}; + bool Terrain::generate(const TerrainParams ¶ms) { tiles.clear(); @@ -44,11 +50,22 @@ bool Terrain::generate(const TerrainParams ¶ms) TerrainTile tile; tile.mesh = gfx_create_mesh(); + GenData data; + data.tp = ¶ms; + data.xoffs = (float)j / (float)params.xtiles; + data.yoffs = (float)i / (float)params.ytiles; + gen_heightfield(tile.mesh, txsz, tysz, params.max_height, params.tile_usub, params.tile_vsub, calc_height, - (void*)¶ms); + (void*)&data); + + float xoffs = j * txsz - params.xsz / 2.0 + txsz / 2.0; + float yoffs = i * tysz - params.ysz / 2.0 + tysz / 2.0; + + Mat4 offmat; + offmat.translation(xoffs, 0, yoffs); + tile.mesh->transform(offmat); - tile.mesh->update_vertex_data(); tiles.push_back(tile); /* @@ -78,7 +95,15 @@ static float calc_height(float u, float v, void *ptr) return 0; } - TerrainParams *tp = (TerrainParams*)ptr; + GenData *data = (GenData*)ptr; + const TerrainParams *tp = data->tp; + + // float ufreq = tp->noise_freq / tp->xtiles; + // float vfreq = tp->noise_freq / tp->ytiles; + + u = u / tp->xtiles + data->xoffs; + v = v / tp->ytiles + data->yoffs; + float sn = gph::fbm(u * tp->noise_freq, v * tp->noise_freq, tp->num_octaves); /* todo use the image later */ return sn;