Added plane

This commit is contained in:
2024-12-06 10:48:00 -06:00
parent e6672945ae
commit 9d91fa6435
10 changed files with 196 additions and 35 deletions

View File

@@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
CubeMesh.cpp
QuadMesh.cpp
SphereMesh.cpp
PlaneMesh.cpp
)

View File

@@ -4,6 +4,7 @@
// https://opensource.org/licenses/MIT
#include "PlaneMesh.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
@@ -11,49 +12,53 @@ void PlaneMesh::buffer(
const std::shared_ptr<Mesh> mesh,
const glm::vec2 planeSize,
const int32_t columns,
const int32_t rows
const int32_t rows,
const glm::vec4 uv
) {
const int32_t verticeCount = (columns + 1) * (rows + 1) * PLANE_VERTICE_PER_SEGMENT;
const int32_t indiceCount = columns * rows * PLANE_INDICE_PER_SEGMENT;
assertNotNull(mesh, "Mesh cannot be null.");
assertTrue(columns > 0, "Columns must be greater than 0.");
assertTrue(rows > 0, "Rows must be greater than 0.");
const int32_t verticeCount = (columns+1)*(rows+1) * PLANE_VERTICE_PER_SEGMENT;
const int32_t indiceCount = (columns+1)*(rows+1) * PLANE_INDICE_PER_SEGMENT;
mesh->createBuffers(verticeCount, indiceCount);
const float columnWidth = planeSize.x / columns;
const float rowHeight = planeSize.y / rows;
glm::vec3 vertices[verticeCount];
glm::vec2 uvs[verticeCount];
glm::vec3 positions[verticeCount];
glm::vec2 coordinates[verticeCount];
int32_t indices[indiceCount];
const glm::vec2 offset(planeSize.x / 2.0f, planeSize.y / 2.0f);
const glm::vec2 step(planeSize.x / columns, planeSize.y / rows);
int32_t verticeIndex = 0;
int32_t indiceIndex = 0;
for (int32_t row = 0; row <= rows; row++) {
for (int32_t column = 0; column <= columns; column++) {
const float x = column * columnWidth;
const float y = row * rowHeight;
for (int32_t y = 0; y <= rows; ++y) {
for (int32_t x = 0; x <= columns; ++x) {
const int32_t verticeStart = (y * (columns + 1) + x) * PLANE_VERTICE_PER_SEGMENT;
const int32_t indiceStart = (y * (columns + 1) + x) * PLANE_INDICE_PER_SEGMENT;
vertices[verticeIndex] = glm::vec3(x, 0.0f, y);
uvs[verticeIndex] = glm::vec2((float)column / columns, (float)row / rows);
verticeIndex++;
const float_t xPos = step.x * x - offset.x;
const float_t yPos = step.y * y - offset.y;
positions[verticeStart + 0] = glm::vec3(xPos, yPos, 0.0f);
positions[verticeStart + 1] = glm::vec3(xPos + step.x, yPos, 0.0f);
positions[verticeStart + 2] = glm::vec3(xPos, yPos + step.y, 0.0f);
positions[verticeStart + 3] = glm::vec3(xPos + step.x, yPos + step.y, 0.0f);
coordinates[verticeStart + 0] = glm::vec2(uv.x, uv.y);
coordinates[verticeStart + 1] = glm::vec2(uv.z, uv.y);
coordinates[verticeStart + 2] = glm::vec2(uv.x, uv.w);
coordinates[verticeStart + 3] = glm::vec2(uv.z, uv.w);
indices[indiceStart + 0] = verticeStart + 0;
indices[indiceStart + 1] = verticeStart + 2;
indices[indiceStart + 2] = verticeStart + 1;
indices[indiceStart + 3] = verticeStart + 1;
indices[indiceStart + 4] = verticeStart + 2;
indices[indiceStart + 5] = verticeStart + 3;
}
}
for (int32_t row = 0; row < rows; row++) {
for (int32_t column = 0; column < columns; column++) {
const int32_t topLeft = (row * (columns + 1)) + column;
const int32_t topRight = topLeft + 1;
const int32_t bottomLeft = topLeft + (columns + 1);
const int32_t bottomRight = bottomLeft + 1;
indices[indiceIndex++] = topLeft;
indices[indiceIndex++] = bottomLeft;
indices[indiceIndex++] = topRight;
indices[indiceIndex++] = topRight;
indices[indiceIndex++] = bottomLeft;
indices[indiceIndex++] = bottomRight;
}
}
mesh->bufferPositions(0, vertices, verticeCount);
mesh->bufferCoordinates(0, uvs, verticeCount);
mesh->bufferPositions(0, positions, verticeCount);
mesh->bufferCoordinates(0, coordinates, verticeCount);
mesh->bufferIndices(0, indices, indiceCount);
}

View File

@@ -12,11 +12,21 @@
namespace Dawn {
class PlaneMesh {
public:
/**
* Buffers a plane mesh into the provided mesh.
*
* @param mesh The mesh to buffer the plane into.
* @param planeSize The size of the plane.
* @param columns The number of columns.
* @param rows The number of rows.
* @param uv The UV coordinates of the plane.
*/
static void buffer(
const std::shared_ptr<Mesh> mesh,
const glm::vec2 planeSize,
const int32_t columns,
const int32_t rows
const int32_t rows,
const glm::vec4 uv = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)
);
};
}