From 197f20b5d4db937029f7b585be23ee7fe5da66dc Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Wed, 16 Aug 2017 16:25:47 +0300 Subject: [PATCH] added function that procedurally generates heightfield replaced jpg images with hdr --- external/gph-math | 2 +- external/libimago | 2 +- src/main.cc | 2 +- src/meshgen.cc | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/meshgen.h | 2 ++ src/terrain.cc | 39 +++++++++++++++++++++ src/terrain.h | 38 +++++++++++++++++++++ 7 files changed, 178 insertions(+), 4 deletions(-) diff --git a/external/gph-math b/external/gph-math index 4c82eda..54ac4cb 160000 --- a/external/gph-math +++ b/external/gph-math @@ -1 +1 @@ -Subproject commit 4c82eda2dcba00a4998cfd60a13330dc793b16ca +Subproject commit 54ac4cbc3b08744e570a53e7f1a8ba862ea87b80 diff --git a/external/libimago b/external/libimago index d7e3914..843ac48 160000 --- a/external/libimago +++ b/external/libimago @@ -1 +1 @@ -Subproject commit d7e3914d568a9e397b1bcd11b3722a84e7f4334e +Subproject commit 843ac48c7cd58166bb9dc0d49097c1b0ae7e2029 diff --git a/src/main.cc b/src/main.cc index 7061bb6..166b467 100644 --- a/src/main.cc +++ b/src/main.cc @@ -138,7 +138,7 @@ static bool init(Gfx_API api) } gskybox = gfx_create_texture(); - gskybox->load("data/cubemap/cubemap.jpg"); + gskybox->load("data/cubemap/cubemap.hdr"); rground->set_sky_tex(gskybox); rcow = new Renderer; diff --git a/src/meshgen.cc b/src/meshgen.cc index fb4fae2..ff7906d 100644 --- a/src/meshgen.cc +++ b/src/meshgen.cc @@ -120,4 +120,99 @@ void gen_geosphere(Mesh *mesh, float rad, int subdiv, bool hemi) mesh->indices[i] = i; } mesh->update_vertex_data(); -} \ No newline at end of file +} + +// ------ heightfield ------ + +static Vec3 hfield_vertex(float u, float v, float h, float xsz, + float ysz, float height) +{ + float x = u * xsz - xsz / 2.0; + float y = h * height; + float z = v * ysz - ysz / 2.0; + + return Vec3(x, y, z); +} + +void gen_heightfield(Mesh *mesh, float xsz, float ysz, float height, int usub, + int vsub, float (*calc_height)(float u, float v, void *ptr), void *ptr) +{ + /* + usub and vsub is the number of subdivision at each axis + (heightfield = grid) + */ + if(usub < 1) + usub = 1; + + if(vsub < 1) + vsub = 1; + + int num_uvertices = usub + 1; + int num_vvertices = vsub + 1; + + int num_quads = usub * vsub; + int num_triangles = num_quads * 2; + + int num_vertices = num_uvertices * num_vvertices; + + mesh->vertices.resize(num_vertices); + mesh->normals.resize(num_vertices); + mesh->tex_coords.resize(num_vertices); + mesh->indices.resize(num_triangles * 3); + + int vidx = 0; + float du = 1.0 / (float)num_uvertices; + float dv = 1.0 / (float)num_vvertices; + + for(int i=0; ivertices[vidx] = vtx; + mesh->normals[vidx] = normal; + mesh->tex_coords[vidx] = Vec2(u, v); + + vidx++; + } + } + + /* + indices: + */ + uint16_t *iptr = &mesh->indices[0]; + + for(int i=0; iupdate_vertex_data(); +} diff --git a/src/meshgen.h b/src/meshgen.h index bcd09ae..81880f6 100644 --- a/src/meshgen.h +++ b/src/meshgen.h @@ -7,5 +7,7 @@ class Mesh; /* generates geodesic sphere: if hemi is true we only gen the hemisphere */ void gen_geosphere(Mesh *mesh, float rad, int subdiv, bool hemi = false); +void gen_heightfield(Mesh *mesh, float xsz, float ysz, float height, int usub, + int vsub, float (*calc_height)(float u, float v, void *ptr), void *ptr); #endif // MESHGEN_H_ diff --git a/src/terrain.cc b/src/terrain.cc index e69de29..6bfabe8 100644 --- a/src/terrain.cc +++ b/src/terrain.cc @@ -0,0 +1,39 @@ +#include "camera.h" +#include "image.h" +#include "scene.h" +#include "terrain.h" + +Terrain::Terrain() {} + +Terrain::~Terrain() +{ +} + +bool Terrain::generate(const TerrainParams ¶ms) +{ + // if(xsz <= 0 || ysz <=0) { + // fprintf(stderr, "Invalid terrain size.\n"); + // return false; + // } + + // if(xtiles <= 0 || ytiles <= 0) { + // fprintf(stderr, "Invalid number of terrain tiles.\n"); + // return false; + // } + + // if(tiles) + // tiles.clear(); + + // int tsz = xtiles * ytiles; + // tiles.resize(tsz); + + // for(int i=0; i tiles; + +public: + Terrain(); + ~Terrain(); + + bool generate(const TerrainParams ¶ms); + Scene *get_visible(const Camera *camera) const; +}; #endif // TERRAIN_H_ \ No newline at end of file -- 1.7.10.4