X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fmeshgen.cc;h=ff7906d4a251803ebecdac612452ec3d6f1fcadc;hp=fb4fae2545a797c338f4c2bdd3e2d9060b512548;hb=197f20b5d4db937029f7b585be23ee7fe5da66dc;hpb=dd0aabd3aed66f9c5d25a9e8bcbbb07c7f886085 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(); +}