Improve mesh components
This commit is contained in:
59
src/dawn/display/mesh/PlaneMesh.cpp
Normal file
59
src/dawn/display/mesh/PlaneMesh.cpp
Normal 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);
|
||||
}
|
||||
22
src/dawn/display/mesh/PlaneMesh.hpp
Normal file
22
src/dawn/display/mesh/PlaneMesh.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "display/mesh/Mesh.hpp"
|
||||
|
||||
#define PLANE_VERTICE_PER_SEGMENT 4
|
||||
#define PLANE_INDICE_PER_SEGMENT 6
|
||||
|
||||
namespace Dawn {
|
||||
class PlaneMesh {
|
||||
public:
|
||||
static void buffer(
|
||||
const std::shared_ptr<Mesh> mesh,
|
||||
const glm::vec2 planeSize,
|
||||
const int32_t columns,
|
||||
const int32_t rows
|
||||
);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user