fixed terrain tiles generation
authorEleni Maria Stea <estea@igalia.com>
Sat, 19 Aug 2017 18:24:43 +0000 (21:24 +0300)
committerEleni Maria Stea <estea@igalia.com>
Sat, 19 Aug 2017 18:24:43 +0000 (21:24 +0300)
src/camera.cc
src/camera.h
src/main.cc
src/mesh.cc
src/mesh.h
src/meshgen.cc
src/opengl/mesh-gl.cc
src/renderer.cc
src/scene.cc
src/terrain.cc

index fd96f78..73980b8 100644 (file)
@@ -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;
index 0ded6be..62120f1 100644 (file)
@@ -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);
index 933bd62..107fb9d 100644 (file)
@@ -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);
index 340263a..fde7ac4 100644 (file)
@@ -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<vertices.size(); i++) {
+               vertices[i] = mat * vertices[i];
+               normals[i] = normal_mat * normals[i];
+       }
+       invalidate();
+}
+
+void Mesh::invalidate()
+{
+       vdata_valid = false;
 }
\ No newline at end of file
index 82f5c48..84cbbcb 100644 (file)
@@ -15,6 +15,9 @@ enum {
 };
 
 class Mesh {
+protected:
+       bool vdata_valid;
+
 public:
        std::vector<uint16_t> indices;
        std::vector<Vec3> vertices;
@@ -22,8 +25,6 @@ public:
        std::vector<Vec2> 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
index 9547cee..c6a4f48 100644 (file)
@@ -119,7 +119,7 @@ void gen_geosphere(Mesh *mesh, float rad, int subdiv, bool hemi)
        for(int i=0; i<num_verts; i++) {
                mesh->indices[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; i<num_vvertices; i++) {
-               float v = (float)i / (float)num_vvertices;
+               float v = 1 - (float)i / (float)(num_vvertices - 1);
                for(int j=0; j<num_uvertices; j++) {
-                       float u = (float)j / (float)num_uvertices;
+                       float u = (float)j / (float)(num_uvertices - 1);
                        Vec3 vtx = hfield_vertex(u, v, calc_height(u, v, ptr), xsz, ysz, height);
 
                        /* calculating normal with forward differences:
@@ -213,5 +213,5 @@ void gen_heightfield(Mesh *mesh, float xsz, float ysz, float height, int usub,
                        *iptr++ = a;
                }
        }
-       mesh->update_vertex_data();
+       mesh->invalidate();
 }
index f0956b4..dd73bf3 100644 (file)
@@ -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);
index 2f50d0b..093b71e 100644 (file)
@@ -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();
index 11281c3..0666bd8 100644 (file)
@@ -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;
 }
 
index bdd787f..92dce56 100644 (file)
@@ -32,6 +32,12 @@ void Terrain::destroy()
        delete vis_scene;
 }
 
+struct GenData {
+       const TerrainParams *tp;
+       float xoffs;
+       float yoffs;
+};
+
 bool Terrain::generate(const TerrainParams &params)
 {
        tiles.clear();
@@ -44,11 +50,22 @@ bool Terrain::generate(const TerrainParams &params)
                        TerrainTile tile;
                        tile.mesh = gfx_create_mesh();
 
+                       GenData data;
+                       data.tp = &params;
+                       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*)&params);
+                                       (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;