Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <vector>
- #include <GL/glew.h>
- #include <GL/freeglut.h>
- #include <GL/gl.h>
- #include <glm/glm.hpp>
- #include "OpenGLError.hpp"
- #define BUFFER_OFFSET(i) ((char *)NULL + (i))
- struct vertex{
- public:
- glm::vec3 position;
- };
- struct shape{
- public:
- shape():
- numVerts(0), numIndices(0), center(0.0f, 0.0f, 0.0f) {}
- shape(float x, float y, float z):
- numVerts(0), numIndices(0), center(x, y, z) {}
- std::vector<vertex> vertices; //Vertex list of this 3D shape
- GLuint numVerts;
- std::vector<GLuint> indices; //OpenGL indices.
- GLuint numIndices;
- glm::vec3 center;
- //Enables us to easily get the buffer size required.
- GLsizeiptr vertexBufferSize() const
- {
- return numVerts * sizeof(vertex);
- }
- GLsizeiptr indexBufferSize() const
- {
- return numIndices * sizeof(GLuint);
- }
- };
- shape makeSierpinski(GLint recursions, bool threeD)
- {
- shape retShape;
- retShape.indices.clear();
- retShape.vertices.clear();
- //The original triangle
- vertex temp[] = {
- glm::vec3(0.0f, 1.0f, -1.0f),
- glm::vec3(-1.0f, -1.0f, -1.0f),
- glm::vec3(1.0f, -1.0f, -1.0f),
- };
- retShape.vertices.push_back(temp[0]);
- retShape.vertices.push_back(temp[1]);
- retShape.vertices.push_back(temp[2]);
- retShape.indices.push_back(0);
- retShape.indices.push_back(1);
- retShape.indices.push_back(2);
- std::vector<GLuint> newIndices;
- for (int j = 0; j < recursions; j++)
- {
- for (GLuint i = 0; i < retShape.indices.size(); i+=3)
- {
- vertex oldVert1 = retShape.vertices[retShape.indices[i]];
- vertex oldVert2 = retShape.vertices[retShape.indices[i+1]];
- vertex oldVert3 = retShape.vertices[retShape.indices[i+2]];
- vertex newVert1;
- vertex newVert2;
- vertex newVert3;
- //Calculate the vector representing the edge between two vertices
- glm::vec3 v1Tov2 = oldVert2.position - oldVert1.position;
- glm::vec3 v2Tov3 = oldVert3.position - oldVert2.position;
- glm::vec3 v3Tov1 = oldVert1.position - oldVert3.position;
- //Halving it to point to the center point.
- v1Tov2 = v1Tov2 * 0.5f;
- v2Tov3 = v2Tov3 * 0.5f;
- v3Tov1 = v3Tov1 * 0.5f;
- //Set the position of each new vertex
- newVert1.position = oldVert1.position + v1Tov2;
- newVert2.position = oldVert2.position + v2Tov3;
- newVert3.position = oldVert3.position + v3Tov1;
- //Add the new vertices and store the new indices
- retShape.vertices.push_back(newVert1);
- GLuint newVertIndex1 = (GLuint)retShape.vertices.size() - 1;
- retShape.vertices.push_back(newVert2);
- GLuint newVertIndex2 = newVertIndex1 + 1;
- retShape.vertices.push_back(newVert3);
- GLuint newVertIndex3 = newVertIndex2 + 1;
- //Calculate and add the new triangles; TRIFORCE!!!!
- // /\
- // /__\
- // /\ /\
- // /__\/__\
- //Top
- newIndices.push_back(retShape.indices[i]);
- newIndices.push_back(newVertIndex1);
- newIndices.push_back(newVertIndex3);
- //Bottom left
- newIndices.push_back(retShape.indices[i+1]);
- newIndices.push_back(newVertIndex2);
- newIndices.push_back(newVertIndex1);
- //Bottom right
- newIndices.push_back(retShape.indices[i+2]);
- newIndices.push_back(newVertIndex3);
- newIndices.push_back(newVertIndex2);
- }
- retShape.numVerts = retShape.vertices.size();
- retShape.indices.swap(newIndices);
- newIndices.clear();
- retShape.numIndices = retShape.indices.size();
- }
- if(threeD)
- {
- GLfloat thickness = 3.0f;
- //Copy the current vertices one unit backwards
- int vertEnd = retShape.vertices.size();
- for (int i = 0; i < vertEnd; i++)
- {
- vertex behindVert = retShape.vertices[i];
- behindVert.position = behindVert.position + glm::vec3(0.0f, 0.0f, -thickness);
- retShape.vertices.push_back(behindVert);
- }
- int indEnd = retShape.indices.size();
- for (int i = 0; i < indEnd; i+=3)
- {
- retShape.indices.push_back(retShape.indices[i] + vertEnd);
- retShape.indices.push_back(retShape.indices[i+2] + vertEnd);
- retShape.indices.push_back(retShape.indices[i+1] + vertEnd);
- }
- //The walls
- for (int i = 0; i < indEnd; i+=3)
- {
- //Current indices
- //right
- int rightTopLeft = i;
- int rightTopRight = rightTopLeft + indEnd;
- int rightBottomLeft = rightTopLeft + 2;
- int rightBottomRight = rightTopRight + 1;
- retShape.indices.push_back(retShape.indices[rightTopLeft]);
- retShape.indices.push_back(retShape.indices[rightBottomLeft]);
- retShape.indices.push_back(retShape.indices[rightTopRight]);
- retShape.indices.push_back(retShape.indices[rightTopRight]);
- retShape.indices.push_back(retShape.indices[rightBottomLeft]);
- retShape.indices.push_back(retShape.indices[rightBottomRight]);
- //left
- int leftTopLeft = i + indEnd;
- int leftTopRight = i;
- int leftBottomLeft = leftTopLeft + 2;
- int leftBottomRight = leftTopRight + 1;
- retShape.indices.push_back(retShape.indices[leftTopLeft]);
- retShape.indices.push_back(retShape.indices[leftBottomLeft]);
- retShape.indices.push_back(retShape.indices[leftTopRight]);
- retShape.indices.push_back(retShape.indices[leftTopRight]);
- retShape.indices.push_back(retShape.indices[leftBottomLeft]);
- retShape.indices.push_back(retShape.indices[leftBottomRight]);
- //bottom
- retShape.indices.push_back(retShape.indices[leftBottomLeft]);
- retShape.indices.push_back(retShape.indices[rightBottomRight]);
- retShape.indices.push_back(retShape.indices[leftBottomRight]);
- retShape.indices.push_back(retShape.indices[leftBottomRight]);
- retShape.indices.push_back(retShape.indices[rightBottomRight]);
- retShape.indices.push_back(retShape.indices[rightBottomLeft]);
- }
- retShape.numVerts = retShape.vertices.size();
- retShape.numIndices = retShape.indices.size();
- }
- return retShape;
- }
- // Global variables
- static int level = 0;
- // Vertex buffer object IDs
- GLuint vbo;
- GLuint ibo;
- shape shape;
- void rebuildGasket() {
- shape = makeSierpinski(1, false);
- }
- void bindDataToBuffers() {
- // Bind VBOs and provide data to them
- // ... insert your code here ...
- // ... end of your code ...
- glGenBuffers(1, &vbo); //Maybe I should gen all the buffers at once?
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, shape.vertexBufferSize(), &shape.vertices[0], GL_STATIC_DRAW);
- glGenBuffers(1, &ibo);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,
- shape.indexBufferSize(), &shape.indices[0], GL_DYNAMIC_DRAW);
- // Unbind the VBO's after we are done
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- //CHECK_OPENGL;
- }
Advertisement
Add Comment
Please, Sign In to add comment