// 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, const float radius, const uint32_t segments, const uint32_t rings ) { // Create the vertices std::vector 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 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()); }