From 025238c73f809683f63e739e3589e2e3ad16defb Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Wed, 2 Aug 2023 01:52:04 +0200 Subject: [PATCH] feat: render isocahedron --- CMakeLists.txt | 1 + src/hello.cpp | 10 +++++++-- src/icosphere.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++ src/icosphere.hpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/icosphere.cpp create mode 100644 src/icosphere.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c0bea94..2ab45ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(${PROJECT_NAME} src/hello.cpp src/io.cpp src/triangle.cpp + src/icosphere.cpp src/gfx.cpp ) diff --git a/src/hello.cpp b/src/hello.cpp index e0a1dca..e4bacb9 100644 --- a/src/hello.cpp +++ b/src/hello.cpp @@ -44,6 +44,7 @@ #include "gfx.hpp" #include "triangle.hpp" +#include "icosphere.hpp" #include #include "astro/twoBodyMethods.hpp" @@ -96,8 +97,11 @@ int main() if (initGraphics(&window) != 0) return -1; - Triangle triangle(compileShaderProgram()); + GLuint shaderProgram = compileShaderProgram(); + Triangle triangle(shaderProgram); + Icosphere sphere(shaderProgram, 0); + // Wireframe glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Main loop @@ -106,7 +110,9 @@ int main() glClearColor(0.2, 0.3, 0.3, 1.0); glClear(GL_COLOR_BUFFER_BIT); - triangle.render(glfwGetTime()); + float time = glfwGetTime(); + triangle.render(time); + sphere.render(time); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/icosphere.cpp b/src/icosphere.cpp new file mode 100644 index 0000000..d922bb4 --- /dev/null +++ b/src/icosphere.cpp @@ -0,0 +1,55 @@ +#include "icosphere.hpp" + +#include + +Icosphere::Icosphere(GLuint shaderProgram, int subdivisions) + : _shaderProgram(shaderProgram) +{ + glGenVertexArrays(1, &_vao); + glGenBuffers(1, &_vbo); + glGenBuffers(1, &_ebo); + + glBindVertexArray(_vao); + + for (const auto& v : _isocahedronVertices) + { + _vertices.push_back(v[0]); + _vertices.push_back(v[1]); + _vertices.push_back(v[2]); + } + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBufferData(GL_ARRAY_BUFFER, _vertices.size() * sizeof(float), &_vertices[0], GL_STATIC_DRAW); + + for (const auto& tri : _isocahedronTriangles) + { + _indices.push_back(tri.vertex[0]); + _indices.push_back(tri.vertex[1]); + _indices.push_back(tri.vertex[2]); + } + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indices.size() * sizeof(unsigned int), &_indices[0], GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void Icosphere::render(float time) +{ + glUseProgram(_shaderProgram); + + glBindVertexArray(_vao); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo); + + glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0); +} + +Icosphere::~Icosphere() +{ + glDeleteVertexArrays(1, &_vao); + glDeleteBuffers(1, &_vbo); +} diff --git a/src/icosphere.hpp b/src/icosphere.hpp new file mode 100644 index 0000000..b974282 --- /dev/null +++ b/src/icosphere.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include +#include +#include + +#include "glm/glm.hpp" + +class Icosphere +{ +public: + Icosphere(GLuint shaderProgram, int subdividision); + void render(float time); + + ~Icosphere(); + +private: + GLuint _vbo; + GLuint _vao; + GLuint _ebo; + GLuint _shaderProgram; + + std::vector _vertices; + std::vector _indices; + + struct IcoTriangle + { + unsigned int vertex[3]; + }; + + using IcoTriangleList = std::vector; + using VertexList = std::vector; + + const float X=.525731112119133606f; + const float Z=.850650808352039932f; + const float N=0.f; + + const VertexList _isocahedronVertices = + { + {-X,N,Z}, {X,N,Z}, {-X,N,-Z}, {X,N,-Z}, + {N,Z,X}, {N,Z,-X}, {N,-Z,X}, {N,-Z,-X}, + {Z,X,N}, {-Z,X, N}, {Z,-X,N}, {-Z,-X, N} + }; + + const IcoTriangleList _isocahedronTriangles = + { + {0,4,1},{0,9,4},{9,5,4},{4,5,8},{4,8,1}, + {8,10,1},{8,3,10},{5,3,8},{5,2,3},{2,7,3}, + {7,10,3},{7,6,10},{7,11,6},{11,0,6},{0,1,6}, + {6,1,10},{9,0,11},{9,11,2},{9,2,5},{7,2,11} + }; + + GLint getShaderUniformLocation(const std::string& uniformName); +}; \ No newline at end of file