Improve mesh components

This commit is contained in:
2024-12-06 09:11:03 -06:00
parent a4774e6189
commit e6672945ae
24 changed files with 366 additions and 42 deletions

View File

@@ -0,0 +1,59 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "PlaneMesh.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 int32_t verticeCount = (columns + 1) * (rows + 1) * PLANE_VERTICE_PER_SEGMENT;
const int32_t indiceCount = columns * rows * 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];
int32_t indices[indiceCount];
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;
vertices[verticeIndex] = glm::vec3(x, 0.0f, y);
uvs[verticeIndex] = glm::vec2((float)column / columns, (float)row / rows);
verticeIndex++;
}
}
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->bufferIndices(0, indices, indiceCount);
}