Starting basic physics, nothing fancy

This commit is contained in:
2023-02-20 00:29:28 -08:00
parent 4bfec16ba7
commit 9606b4dc9b
32 changed files with 779 additions and 286 deletions

View File

@ -1,12 +1,13 @@
# 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
CubeMesh.cpp
TriangleMesh.cpp
QuadMesh.cpp
# 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
CubeMesh.cpp
TriangleMesh.cpp
QuadMesh.cpp
SphereMesh.cpp
)

View File

@ -13,8 +13,8 @@ void CubeMesh::buffer(
int32_t verticeStart, int32_t indiceStart
) {
assertNotNull(mesh);
mesh->bufferPositions(verticeStart, std::array<glm::vec3, CUBE_VERTICE_COUNT>{{
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),
@ -24,9 +24,9 @@ void CubeMesh::buffer(
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
}});
};
mesh->bufferCoordinates(verticeStart,std::array<glm::vec2,CUBE_VERTICE_COUNT>{{
glm::vec2 coordinates[CUBE_VERTICE_COUNT] = {
glm::vec2(0, 0),
glm::vec2(1, 0),
glm::vec2(0, 1),
@ -36,9 +36,9 @@ void CubeMesh::buffer(
glm::vec2(1, 0),
glm::vec2(0, 1),
glm::vec2(1, 1)
}});
};
mesh->bufferIndices(indiceStart, std::array<meshindice_t, CUBE_INDICE_COUNT>{{
meshindice_t indices[CUBE_INDICE_COUNT] = {
// Back
verticeStart, verticeStart + 1, verticeStart + 3,
verticeStart, verticeStart + 2, verticeStart + 3,
@ -62,5 +62,9 @@ void CubeMesh::buffer(
// 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

@ -15,28 +15,24 @@ void QuadMesh::bufferQuadMeshWithZ(
) {
assertNotNull(mesh);
mesh->bufferPositions(
verticeStart, std::array<glm::vec3, 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::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->bufferCoordinates(
verticeStart, std::array<glm::vec2, QUAD_VERTICE_COUNT>{{
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
}}
);
mesh->bufferIndices(
indiceStart, std::array<meshindice_t, 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(
@ -56,10 +52,11 @@ void QuadMesh::bufferCoordinates(
int32_t verticeStart
) {
assertNotNull(mesh);
mesh->bufferCoordinates(verticeStart, std::array<glm::vec2, QUAD_VERTICE_COUNT>{{
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(
@ -67,14 +64,14 @@ void QuadMesh::bufferPositions(
glm::vec2 xy0, glm::vec2 xy1,
int32_t verticeStart
) {
mesh->bufferPositions(
verticeStart, std::array<glm::vec3, 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)
}}
);
assertNotNull(mesh);
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(

View File

@ -0,0 +1,59 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SphereMesh.hpp"
using namespace Dawn;
void SphereMesh::createSphere(
Mesh *mesh,
float_t radius,
int32_t slices,
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 = M_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 * M_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

@ -0,0 +1,19 @@
// 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:
static void createSphere(
Mesh *mesh,
float_t radius,
int32_t slices,
int32_t stacks
);
};
}

View File

@ -9,19 +9,25 @@ using namespace Dawn;
void TriangleMesh::createTriangleMesh(Mesh *mesh) {
assertNotNull(mesh);
mesh->createBuffers(3, 3);
mesh->bufferPositions(0, std::array<glm::vec3, 3>{{
glm::vec3 positions[3] = {
glm::vec3(-0.5f, -0.5f, 0),
glm::vec3(0.5f, -0.5f, 0),
glm::vec3(0, 0.5f, 0)
}});
mesh->bufferCoordinates(0, std::array<glm::vec2, 3>{{
};
glm::vec2 coordinates[3] = {
glm::vec2(0, 0),
glm::vec2(0, 1),
glm::vec2(1, 0)
}});
mesh->bufferIndices(0, std::array<meshindice_t,3>{{
};
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);
}