Base refactor

This commit is contained in:
2023-11-14 09:16:48 -06:00
parent 214082d00f
commit 1817dcaf3a
410 changed files with 749 additions and 20823 deletions

View File

@@ -1,14 +0,0 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
CapsuleMesh.cpp
CubeMesh.cpp
TriangleMesh.cpp
QuadMesh.cpp
SphereMesh.cpp
)

View File

@@ -1,97 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CapsuleMesh.hpp"
#include "util/mathutils.hpp"
using namespace Dawn;
void CapsuleMesh::calculateRing(
const int32_t segments,
const float_t height,
const float_t radius,
const float_t dr,
const float_t y,
const float_t dy,
std::vector<glm::vec3> &positions
) {
float_t segIncr = 1.0f / (float_t)(segments - 1);
for(int32_t s = 0; s < segments; s++ ) {
float_t x = cosf(MATH_PI * 2 * s * segIncr) * dr;
float_t z = sinf(MATH_PI * 2 * s * segIncr) * dr;
positions.emplace_back(glm::vec3(radius * x, radius * y + height * dy, radius * z ));
}
}
void CapsuleMesh::create(
Mesh &mesh,
const float_t radius,
const float_t height
) {
std::vector<glm::vec3> positions;
std::vector<meshindice_t> indices;
const int32_t slices = 12;
const int32_t segments = 12;
const int32_t ringsBody = slices + 1;
const int32_t ringsTotal = slices + ringsBody;
positions.reserve(segments * ringsTotal);
indices.reserve((segments - 1) * (ringsTotal - 1) * 6);
const float_t bodyIncr = 1.0f / (float_t)(ringsBody - 1);
const float_t ringIncr = 1.0f / (float_t)(slices - 1);
for(int32_t r = 0; r < slices / 2; r++) {
calculateRing(
segments,
height,
radius,
sinf(MATH_PI * r * ringIncr),
sinf(MATH_PI * (r * ringIncr - 0.5f)),
-0.5f,
positions
);
}
for(int32_t r = 0; r < ringsBody; r++ ) {
calculateRing(
segments,
height,
radius,
1.0f,
0.0f,
r * bodyIncr - 0.5f,
positions
);
}
for(int32_t r = slices / 2; r < slices; r++) {
calculateRing(
segments,
height,
radius,
sinf(MATH_PI * r * ringIncr),
sinf(MATH_PI * (r * ringIncr - 0.5f)),
0.5f,
positions
);
}
for(int32_t r = 0; r < ringsTotal - 1; r++ ) {
for(int32_t s = 0; s < segments - 1; s++ ) {
indices.push_back( (uint32_t)(r * segments + ( s + 1 )) );
indices.push_back( (uint32_t)(r * segments + ( s + 0 )) );
indices.push_back( (uint32_t)(( r + 1 ) * segments + ( s + 1 )) );
indices.push_back( (uint32_t)(( r + 1 ) * segments + ( s + 0 )) );
indices.push_back( (uint32_t)(( r + 1 ) * segments + ( s + 1 )) );
indices.push_back( (uint32_t)(r * segments + s) );
}
}
mesh.createBuffers(positions.size(), indices.size());
mesh.bufferPositions(0, positions.data(), positions.size());
mesh.bufferIndices(0, indices.data(), indices.size());
}

View File

@@ -1,47 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class CapsuleMesh {
protected:
/**
* Calculates a ring of vertices within a capsule.
*
* @param segments Count of segments in the ring.
* @param height Height of the ring.
* @param radius Radius of the ring.
* @param dr The delta radius of the ring.
* @param y The y position of the ring.
* @param dy The delta y position of the ring.
* @param positions The positions vector to push the positions to.
*/
static void calculateRing(
const int32_t segments,
const float_t height,
const float_t radius,
const float_t dr,
const float_t y,
const float_t dy,
std::vector<glm::vec3> &positions
);
public:
/**
* Creates a capsule mesh.
*
* @param mesh Mesh to instanciate.
* @param radius Radius of the capsule.
* @param height Height of the capsule.
*/
static void create(
Mesh &mesh,
const float_t radius,
const float_t height
);
};
}

View File

@@ -1,70 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CubeMesh.hpp"
using namespace Dawn;
void CubeMesh::buffer(
Mesh &mesh,
const glm::vec3 pos,
const glm::vec3 size,
const int32_t verticeStart,
const int32_t indiceStart
) {
glm::vec3 positions[CUBE_VERTICE_COUNT] = {
pos,
glm::vec3(pos.x+size.x, pos.y, pos.z),
glm::vec3(pos.x, pos.y+size.y, pos.z),
glm::vec3(pos.x+size.x, pos.y+size.y, pos.z),
glm::vec3(pos.x, pos.y, pos.z+size.z),
glm::vec3(pos.x+size.x, pos.y, pos.z+size.z),
glm::vec3(pos.x, pos.y+size.y, pos.z+size.z),
pos + size
};
glm::vec2 coordinates[CUBE_VERTICE_COUNT] = {
glm::vec2(0, 0),
glm::vec2(1, 0),
glm::vec2(0, 1),
glm::vec2(1, 1),
glm::vec2(0, 0),
glm::vec2(1, 0),
glm::vec2(0, 1),
glm::vec2(1, 1)
};
meshindice_t indices[CUBE_INDICE_COUNT] = {
// Back
verticeStart, verticeStart + 1, verticeStart + 3,
verticeStart, verticeStart + 2, verticeStart + 3,
// Right
verticeStart + 1, verticeStart + 5, verticeStart + 7,
verticeStart + 1, verticeStart + 3, verticeStart + 7,
// Left
verticeStart + 4, verticeStart, verticeStart + 2,
verticeStart + 4, verticeStart + 6, verticeStart + 2,
// Front
verticeStart + 5, verticeStart + 4, verticeStart + 6,
verticeStart + 5, verticeStart + 7, verticeStart + 6,
// Top
verticeStart + 7, verticeStart + 2, verticeStart + 6,
verticeStart + 7, verticeStart + 3, verticeStart + 2,
// Bottom
verticeStart + 1, verticeStart, verticeStart + 4,
verticeStart + 1, verticeStart + 4, verticeStart + 5
};
mesh.bufferPositions(verticeStart, positions, CUBE_VERTICE_COUNT);
mesh.bufferCoordinates(verticeStart, coordinates, CUBE_VERTICE_COUNT);
mesh.bufferIndices(indiceStart, indices, CUBE_INDICE_COUNT);
}

View File

@@ -1,32 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
#define CUBE_VERTICE_COUNT 8
#define CUBE_INDICE_COUNT 36
namespace Dawn {
class CubeMesh {
public:
/**
* Buffers cube mesh vertices onto a mesh.
*
* @param mesh Mesh to buffer onto.
* @param pos Position of the cube.
* @param size Size of the cube.
* @param verticeStart Starting vertice index.
* @param indiceStart Starting indice index.
*/
static void buffer(
Mesh &mesh,
const glm::vec3 pos,
const glm::vec3 size,
const int32_t verticeStart,
const int32_t indiceStart
);
};
}

View File

@@ -1,92 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "QuadMesh.hpp"
using namespace Dawn;
void QuadMesh::bufferQuadMeshWithZ(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 uv0,
const glm::vec2 xy1,
const glm::vec2 uv1,
const float_t z,
const int32_t verticeStart,
const int32_t indiceStart
) {
glm::vec3 positions[QUAD_VERTICE_COUNT] = {
glm::vec3(xy0, z),
glm::vec3(xy1.x, xy0.y, z),
glm::vec3(xy0.x, xy1.y, z),
glm::vec3(xy1, z)
};
glm::vec2 coordinates[QUAD_VERTICE_COUNT] = {
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
};
meshindice_t indices[QUAD_INDICE_COUNT] = {
verticeStart, verticeStart + 1, verticeStart + 2,
verticeStart + 1, verticeStart + 2, verticeStart + 3
};
mesh.bufferPositions(verticeStart, positions, QUAD_VERTICE_COUNT);
mesh.bufferCoordinates(verticeStart, coordinates, QUAD_VERTICE_COUNT);
mesh.bufferIndices(indiceStart, indices, QUAD_INDICE_COUNT);
}
void QuadMesh::bufferQuadMesh(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 uv0,
const glm::vec2 xy1,
const glm::vec2 uv1,
const int32_t verticeStart,
const int32_t indiceStart
) {
QuadMesh::bufferQuadMeshWithZ(
mesh, xy0, uv0, xy1, uv1, 0, verticeStart, indiceStart
);
}
void QuadMesh::bufferCoordinates(
Mesh &mesh,
const glm::vec2 uv0,
const glm::vec2 uv1,
const int32_t verticeStart
) {
glm::vec2 coordinates[QUAD_VERTICE_COUNT] = {
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
};
mesh.bufferCoordinates(verticeStart, coordinates, QUAD_VERTICE_COUNT);
}
void QuadMesh::bufferPositions(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 xy1,
const int32_t verticeStart
) {
glm::vec3 positions[QUAD_VERTICE_COUNT] = {
glm::vec3(xy0, 0),
glm::vec3(xy1.x, xy0.y, 0),
glm::vec3(xy0.x, xy1.y, 0),
glm::vec3(xy1, 0)
};
mesh.bufferPositions(verticeStart, positions, QUAD_VERTICE_COUNT);
}
void QuadMesh::initQuadMesh(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 uv0,
const glm::vec2 xy1,
const glm::vec2 uv1,
const float_t z
) {
mesh.createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
QuadMesh::bufferQuadMeshWithZ(mesh, xy0, uv0, xy1, uv1, z, 0, 0);
}

View File

@@ -1,109 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
#define QUAD_VERTICE_COUNT 4
#define QUAD_INDICE_COUNT 6
#define QUAD_INDICE_PER_QUAD 2
namespace Dawn {
class QuadMesh {
public:
/**
* Buffers the vertices of a quad onto a primitive.
*
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param uv0 The lower Xand Y texture coordinate.
* @param xy1 The higher X and Y coordinate.
* @param uv1 The higher X and Y texture coordinate.
* @param z The Z position of the coordinates.
* @param verticeStart Start vertice to buffer to.
* @param indiceStart Start indice to buffer to.
*/
static void bufferQuadMeshWithZ(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 uv0,
const glm::vec2 xy1,
const glm::vec2 uv1,
const float_t z,
const int32_t verticeStart,
const int32_t indiceStart
);
/**
* Buffers the vertices of a quad onto a primitive.
*
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param uv0 The lower Xand Y texture coordinate.
* @param xy1 The higher X and Y coordinate.
* @param uv1 The higher X and Y texture coordinate.
* @param verticeStart Start vertice to buffer to.
* @param indiceStart Start indice to buffer to.
*/
static void bufferQuadMesh(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 uv0,
const glm::vec2 xy1,
const glm::vec2 uv1,
const int32_t verticeStart,
const int32_t indiceStart
);
/**
* Buffers texture coordinates on to an already initialized quad mesh.
*
* @param mesh Mesh to buffer the texture coordinates on to.
* @param uv0 Lower X and Y coordinates.
* @param uv1 Upper X and Y coordinates.
* @param verticeStart Start vertice to buffer in to.
*/
static void bufferCoordinates(
Mesh &mesh,
const glm::vec2 uv0,
const glm::vec2 uv1,
const int32_t verticeStart
);
/**
* Buffers the positions of a quad onto a primitive.
*
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param xy1 The higher X and Y coordinate.
* @param verticeStart Start vertice to buffer to.
*/
static void bufferPositions(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 xy1,
const int32_t verticeStart
);
/**
* Initializes a mesh to be a single quad.
*
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param uv0 The lower Xand Y texture coordinate.
* @param xy1 The higher X and Y coordinate.
* @param uv1 The higher X and Y texture coordinate.
* @param z The Z position of the coordinates.
*/
static void initQuadMesh(
Mesh &mesh,
const glm::vec2 xy0,
const glm::vec2 uv0,
const glm::vec2 xy1,
const glm::vec2 uv1,
const float_t z
);
};
}

View File

@@ -1,60 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SphereMesh.hpp"
#include "util/mathutils.hpp"
using namespace Dawn;
void SphereMesh::createSphere(
Mesh &mesh,
const float_t radius,
const int32_t slices,
const int32_t stacks
) {
std::vector<glm::vec3> positions;
// Create vertices
int32_t c = 0;
for (int32_t i = 0; i <= stacks; i++) {
float_t phi = MATH_PI * i / stacks;
float_t cosPhi = cos(phi);
float_t sinPhi = sin(phi);
for (int32_t j = 0; j <= slices; j++) {
float_t theta = 2 * MATH_PI * j / slices;
float_t cosTheta = cos(theta);
float_t sinTheta = sin(theta);
glm::vec3 v;
v.x = radius * sinPhi * cosTheta;
v.y = radius * sinPhi * sinTheta;
v.z = radius * cosPhi;
positions.push_back(v);
}
}
// Create indices
std::vector<meshindice_t> indices;
for (int32_t i = 0; i < stacks; i++) {
for (int32_t j = 0; j < slices; j++) {
meshindice_t p1 = i * (slices + 1) + j;
meshindice_t p2 = p1 + slices + 1;
indices.push_back(p1);
indices.push_back(p2);
indices.push_back(p1 + 1);
indices.push_back(p1 + 1);
indices.push_back(p2);
indices.push_back(p2 + 1);
}
}
mesh.createBuffers(positions.size(), indices.size());
mesh.bufferPositions(0, positions.data(), positions.size());
mesh.bufferIndices(0, indices.data(), indices.size());
}

View File

@@ -1,27 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class SphereMesh {
public:
/**
* Creates a sphere mesh.
*
* @param mesh Mesh to instanciate.
* @param radius Radius of the sphere.
* @param slices How many horizontal slices to make.
* @param stacks How many vertical stacks to make.
*/
static void createSphere(
Mesh &mesh,
const float_t radius,
const int32_t slices,
const int32_t stacks
);
};
}

View File

@@ -1,31 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/mesh/TriangleMesh.hpp"
using namespace Dawn;
void TriangleMesh::createTriangleMesh(Mesh &mesh) {
glm::vec3 positions[3] = {
glm::vec3(-0.5f, -0.5f, 0),
glm::vec3(0.5f, -0.5f, 0),
glm::vec3(0, 0.5f, 0)
};
glm::vec2 coordinates[3] = {
glm::vec2(0, 0),
glm::vec2(0, 1),
glm::vec2(1, 0)
};
meshindice_t indices[3] = {
0, 1, 2
};
mesh.createBuffers(3, 3);
mesh.bufferPositions(0, positions, 3);
mesh.bufferCoordinates(0, coordinates, 3);
mesh.bufferIndices(0, indices, 3);
}

View File

@@ -1,19 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class TriangleMesh {
public:
/**
* Initializes a mesh to hold a single triangle.
*
* @param mesh Mesh to initialize as a triangle.
*/
static void createTriangleMesh(Mesh &mesh);
};
}