Starting work on capsules.
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
CapsuleMesh.cpp
|
||||
CubeMesh.cpp
|
||||
TriangleMesh.cpp
|
||||
QuadMesh.cpp
|
||||
|
99
src/dawn/display/mesh/CapsuleMesh.cpp
Normal file
99
src/dawn/display/mesh/CapsuleMesh.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "CapsuleMesh.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void CapsuleMesh::calculateRing(
|
||||
int32_t segments,
|
||||
float_t height,
|
||||
float_t radius,
|
||||
float_t dr,
|
||||
float_t y,
|
||||
float_t dy,
|
||||
std::vector<glm::vec3> *positions
|
||||
) {
|
||||
assertNotNull(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,
|
||||
float_t radius,
|
||||
float_t height
|
||||
) {
|
||||
assertNotNull(mesh);
|
||||
|
||||
std::vector<glm::vec3> positions;
|
||||
std::vector<meshindice_t> indices;
|
||||
|
||||
int32_t slices = 12;
|
||||
int32_t segments = 12;
|
||||
int32_t ringsBody = slices + 1;
|
||||
int32_t ringsTotal = slices + ringsBody;
|
||||
|
||||
positions.reserve(segments * ringsTotal);
|
||||
indices.reserve((segments - 1) * (ringsTotal - 1) * 6);
|
||||
|
||||
float_t bodyIncr = 1.0f / (float_t)(ringsBody - 1);
|
||||
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());
|
||||
}
|
30
src/dawn/display/mesh/CapsuleMesh.hpp
Normal file
30
src/dawn/display/mesh/CapsuleMesh.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
// 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"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class CapsuleMesh {
|
||||
protected:
|
||||
static void calculateRing(
|
||||
int32_t segments,
|
||||
float_t height,
|
||||
float_t radius,
|
||||
float_t dr,
|
||||
float_t y,
|
||||
float_t dy,
|
||||
std::vector<glm::vec3> *positions
|
||||
);
|
||||
|
||||
public:
|
||||
static void create(
|
||||
Mesh *mesh,
|
||||
float_t radius,
|
||||
float_t height
|
||||
);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user