feat: render isocahedron

This commit is contained in:
Cat Flynn 2023-08-02 01:52:04 +02:00
parent e713ed8123
commit 025238c73f
4 changed files with 118 additions and 2 deletions

View File

@ -28,6 +28,7 @@ add_executable(${PROJECT_NAME}
src/hello.cpp
src/io.cpp
src/triangle.cpp
src/icosphere.cpp
src/gfx.cpp
)

View File

@ -44,6 +44,7 @@
#include "gfx.hpp"
#include "triangle.hpp"
#include "icosphere.hpp"
#include <cmath>
#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();

55
src/icosphere.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "icosphere.hpp"
#include <iostream>
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);
}

54
src/icosphere.hpp Normal file
View File

@ -0,0 +1,54 @@
#pragma once
#include <GL/glew.h>
#include <vector>
#include <string>
#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<float> _vertices;
std::vector<unsigned int> _indices;
struct IcoTriangle
{
unsigned int vertex[3];
};
using IcoTriangleList = std::vector<IcoTriangle>;
using VertexList = std::vector<glm::vec3>;
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);
};