Files
Dawn/src/dawn/display/mesh/PlaneMesh.cpp
2024-12-06 10:48:00 -06:00

64 lines
2.3 KiB
C++

// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "PlaneMesh.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
void PlaneMesh::buffer(
const std::shared_ptr<Mesh> mesh,
const glm::vec2 planeSize,
const int32_t columns,
const int32_t rows,
const glm::vec4 uv
) {
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);
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);
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;
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;
}
}
mesh->bufferPositions(0, positions, verticeCount);
mesh->bufferCoordinates(0, coordinates, verticeCount);
mesh->bufferIndices(0, indices, indiceCount);
}