From 4a9653642e1fb5207b1e20dfb83a4eeae3fccec3 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Fri, 15 Mar 2019 23:58:43 +0100 Subject: [PATCH] Add method to generate a plane mesh, planned for heightmaps --- src/utilities/shapes.cpp | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/utilities/shapes.cpp b/src/utilities/shapes.cpp index fbbad40..dbeb0ac 100644 --- a/src/utilities/shapes.cpp +++ b/src/utilities/shapes.cpp @@ -312,3 +312,53 @@ Mesh generateSphere(float sphereRadius, int slices, int layers) { mesh.indices = indices; return mesh; } + +Mesh generateSegmentedPlane(float width, float height, uint x_segments, uint y_segments) { + // i should perhaps initialize these to the length they should be, but insert is prettier :3 + vector vertices; + vector normals; + vector textureCoordinates; + vector indices; + + float step_x = width/x_segments; + float step_y = height/y_segments; + float tex_step_x = 1.0/x_segments; + float tex_step_y = 1.0/y_segments; + uint index_offset = 0; + + for (uint x = 0; x < x_segments; x++) + for (uint y = 0; y < y_segments; y++) { + vertices.insert(vertices.end(), { + vec3(step_x*(x+0), step_y*(y+0), 0), + vec3(step_x*(x+0), step_y*(y+1), 0), + vec3(step_x*(x+1), step_y*(y+0), 0), + vec3(step_x*(x+1), step_y*(y+1), 0), + }); + normals.insert(vertices.end(), { + vec3(0.0, 0.0, 1.0), + vec3(0.0, 0.0, 1.0), + vec3(0.0, 0.0, 1.0), + vec3(0.0, 0.0, 1.0), + }); + textureCoordinates.insert(textureCoordinates.end(), { + vec2(tex_step_x*(x+0), tex_step_y*(y+0)), + vec2(tex_step_x*(x+0), tex_step_y*(y+1)), + vec2(tex_step_x*(x+1), tex_step_y*(y+0)), + vec2(tex_step_x*(x+1), tex_step_y*(y+1)), + }); + indices.insert(indices.end(), { + index_offset + 0, index_offset + 1, index_offset + 2, + index_offset + 1, index_offset + 2, index_offset + 3, + }); + index_offset += 4; + } + + Mesh mesh; + mesh.vertices = vertices; + mesh.normals = normals; + mesh.textureCoordinates = textureCoordinates; + mesh.indices = indices; + + return mesh; + +}