refactor: extract vertex generation from ctor

This commit is contained in:
ktyl 2023-08-13 23:13:44 +02:00
parent ae78f0a399
commit 8c9bacbd04
2 changed files with 32 additions and 26 deletions

View File

@ -10,6 +10,37 @@
Icosphere::Icosphere(float radius, int subdivisions, GLuint shaderProgram, glm::vec3 position) :
_shaderProgram(shaderProgram),
_position(position)
{
generateVertices(radius, subdivisions);
glGenVertexArrays(1, &_vao);
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_ebo);
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
size_t vboBufferSize = _vertices.size() * sizeof(float);
glBufferData(GL_ARRAY_BUFFER, vboBufferSize, &_vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
size_t eboBufferSize = _indices.size() * sizeof(unsigned int);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, eboBufferSize, &_indices[0], GL_STATIC_DRAW);
// Vertex position attribute
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
// Vertex normal attribute
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Icosphere::generateVertices(float radius, int subdivisions)
{
VertexList vertices = _isocahedronVertices;
TriangleList triangles = _isocahedronTriangles;
@ -29,12 +60,6 @@ Icosphere::Icosphere(float radius, int subdivisions, GLuint shaderProgram, glm::
vertices[i] *= radius;
}
glGenVertexArrays(1, &_vao);
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_ebo);
glBindVertexArray(_vao);
for (const auto& tri : triangles)
{
_indices.push_back(tri.vertex[0]);
@ -56,26 +81,6 @@ Icosphere::Icosphere(float radius, int subdivisions, GLuint shaderProgram, glm::
_vertices.push_back(normal[1]);
_vertices.push_back(normal[2]);
}
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
size_t vboBufferSize = _vertices.size() * sizeof(float);
glBufferData(GL_ARRAY_BUFFER, vboBufferSize, &_vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
size_t eboBufferSize = _indices.size() * sizeof(unsigned int);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, eboBufferSize, &_indices[0], GL_STATIC_DRAW);
// Vertex position attribute
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
// Vertex normal attribute
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
int Icosphere::vertexForEdge(Lookup& lookup, VertexList& vertices, int first, int second)

View File

@ -57,6 +57,7 @@ private:
using Lookup = std::map<std::pair<int, int>, int>;
void generateVertices(float radius, int subdivisions);
int vertexForEdge(Lookup& lookup, VertexList& vertices, int first, int second);
TriangleList subdivide(VertexList& vertices, TriangleList triangles);