From 8c9bacbd04cb865f36e19309db189a0e00377588 Mon Sep 17 00:00:00 2001 From: ktyl Date: Sun, 13 Aug 2023 23:13:44 +0200 Subject: [PATCH] refactor: extract vertex generation from ctor --- src/icosphere.cpp | 57 ++++++++++++++++++++++++++--------------------- src/icosphere.hpp | 1 + 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/icosphere.cpp b/src/icosphere.cpp index 11d92ff..8046176 100644 --- a/src/icosphere.cpp +++ b/src/icosphere.cpp @@ -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) diff --git a/src/icosphere.hpp b/src/icosphere.hpp index 8e9ae88..eaeb143 100644 --- a/src/icosphere.hpp +++ b/src/icosphere.hpp @@ -57,6 +57,7 @@ private: using Lookup = std::map, int>; + void generateVertices(float radius, int subdivisions); int vertexForEdge(Lookup& lookup, VertexList& vertices, int first, int second); TriangleList subdivide(VertexList& vertices, TriangleList triangles);