Dawn/src/dawn/display/mesh/SphereMesh.cpp

44 lines
1.3 KiB
C++

// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SphereMesh.hpp"
using namespace Dawn;
void SphereMesh::create(
std::shared_ptr<Mesh> mesh,
const float radius,
const uint32_t segments,
const uint32_t rings
) {
// Create the vertices
std::vector<glm::vec3> vertices;
for(uint32_t r = 0; r < rings; ++r) {
for(uint32_t s = 0; s < segments; ++s) {
float const y = sin(-M_PI_2 + M_PI * r / rings);
float const x = cos(2 * M_PI * s / segments) * sin(M_PI * r / rings);
float const z = sin(2 * M_PI * s / segments) * sin(M_PI * r / rings);
vertices.push_back(glm::vec3(x, y, z) * radius);
}
}
// Create the indices
std::vector<int32_t> indices;
for(uint32_t r = 0; r < rings - 1; ++r) {
for(uint32_t s = 0; s < segments - 1; ++s) {
indices.push_back(r * segments + s);
indices.push_back(r * segments + (s + 1));
indices.push_back((r + 1) * segments + (s + 1));
indices.push_back(r * segments + s);
indices.push_back((r + 1) * segments + (s + 1));
indices.push_back((r + 1) * segments + s);
}
}
mesh->createBuffers(vertices.size(), indices.size());
mesh->bufferPositions(0, vertices.data(), vertices.size());
mesh->bufferIndices(0, indices.data(), indices.size());
}