skein/src/icosphere.hpp

68 lines
1.5 KiB
C++

#pragma once
#include <GL/glew.h>
#include <vector>
#include <string>
#include <map>
#include "glm/glm.hpp"
class Icosphere
{
public:
Icosphere(float radius, int subdivisions, GLuint shaderProgram);
void render(const float time);
void setPosition(glm::vec3 position);
~Icosphere();
private:
GLuint _vbo;
GLuint _vao;
GLuint _ebo;
GLuint _shaderProgram;
std::vector<float> _vertices;
std::vector<unsigned int> _indices;
glm::vec3 _position;
// Generating an isosphere
// https://schneide.blog/2016/07/15/generating-an-icosphere-in-c/
struct Triangle
{
int vertex[3];
};
using TriangleList = std::vector<Triangle>;
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 TriangleList _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}
};
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);
void updateModelMatrix();
};