NoobsDeSroobs

OPGL

Sep 4th, 2014
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. #include <GL/glew.h>
  7. #include <GL/freeglut.h>
  8. #include <GL/gl.h>
  9.  
  10. #include <glm/glm.hpp>
  11.  
  12. #include "OpenGLError.hpp"
  13.  
  14. #define BUFFER_OFFSET(i) ((char *)NULL + (i))
  15.  
  16.  
  17. struct vertex{
  18. public:
  19.     glm::vec3 position;
  20. };
  21.  
  22. struct shape{
  23. public:
  24.     shape():
  25.         numVerts(0), numIndices(0), center(0.0f, 0.0f, 0.0f) {}
  26.     shape(float x, float y, float z):
  27.         numVerts(0), numIndices(0), center(x, y, z) {}
  28.  
  29.     std::vector<vertex> vertices; //Vertex list of this 3D shape
  30.     GLuint numVerts;
  31.     std::vector<GLuint> indices; //OpenGL indices.
  32.     GLuint numIndices; 
  33.     glm::vec3 center;
  34.     //Enables us to easily get the buffer size required.
  35.     GLsizeiptr vertexBufferSize() const
  36.     {
  37.         return numVerts * sizeof(vertex);
  38.     }
  39.     GLsizeiptr indexBufferSize() const
  40.     {
  41.         return numIndices * sizeof(GLuint);
  42.     }
  43. };
  44.  
  45. shape makeSierpinski(GLint recursions, bool threeD)
  46. {
  47.     shape retShape;
  48.     retShape.indices.clear();
  49.     retShape.vertices.clear();
  50.     //The original triangle
  51.     vertex temp[] = {
  52.         glm::vec3(0.0f, 1.0f, -1.0f),
  53.         glm::vec3(-1.0f, -1.0f, -1.0f),
  54.         glm::vec3(1.0f,  -1.0f, -1.0f),
  55.     };
  56.     retShape.vertices.push_back(temp[0]);
  57.     retShape.vertices.push_back(temp[1]);
  58.     retShape.vertices.push_back(temp[2]);
  59.  
  60.     retShape.indices.push_back(0);
  61.     retShape.indices.push_back(1);
  62.     retShape.indices.push_back(2);
  63.  
  64.     std::vector<GLuint> newIndices;
  65.  
  66.     for (int j = 0; j < recursions; j++)
  67.     {
  68.         for (GLuint i = 0; i < retShape.indices.size(); i+=3)
  69.         {
  70.             vertex oldVert1 = retShape.vertices[retShape.indices[i]];
  71.             vertex oldVert2 = retShape.vertices[retShape.indices[i+1]];
  72.             vertex oldVert3 = retShape.vertices[retShape.indices[i+2]];
  73.             vertex newVert1;
  74.             vertex newVert2;
  75.             vertex newVert3;
  76.  
  77.             //Calculate the vector representing the edge between two vertices
  78.             glm::vec3 v1Tov2 = oldVert2.position - oldVert1.position;
  79.             glm::vec3 v2Tov3 = oldVert3.position - oldVert2.position;
  80.             glm::vec3 v3Tov1 = oldVert1.position - oldVert3.position;
  81.  
  82.             //Halving it to point to the center point.
  83.             v1Tov2 = v1Tov2 * 0.5f;
  84.             v2Tov3 = v2Tov3 * 0.5f;
  85.             v3Tov1 = v3Tov1 * 0.5f;
  86.  
  87.             //Set the position of each new vertex
  88.             newVert1.position = oldVert1.position + v1Tov2;
  89.             newVert2.position = oldVert2.position + v2Tov3;
  90.             newVert3.position = oldVert3.position + v3Tov1;
  91.  
  92.            
  93.             //Add the new vertices and store the new indices
  94.             retShape.vertices.push_back(newVert1);
  95.             GLuint newVertIndex1 = (GLuint)retShape.vertices.size() - 1;
  96.             retShape.vertices.push_back(newVert2);
  97.             GLuint newVertIndex2 = newVertIndex1 + 1;
  98.             retShape.vertices.push_back(newVert3);
  99.             GLuint newVertIndex3 = newVertIndex2 + 1;
  100.  
  101.             //Calculate and add the new triangles; TRIFORCE!!!!
  102.             //          /\
  103.             //         /__\
  104.             //        /\  /\
  105.             //       /__\/__\
  106.  
  107.             //Top
  108.             newIndices.push_back(retShape.indices[i]);
  109.             newIndices.push_back(newVertIndex1);
  110.             newIndices.push_back(newVertIndex3);
  111.             //Bottom left
  112.             newIndices.push_back(retShape.indices[i+1]);
  113.             newIndices.push_back(newVertIndex2);
  114.             newIndices.push_back(newVertIndex1);
  115.             //Bottom right
  116.             newIndices.push_back(retShape.indices[i+2]);
  117.             newIndices.push_back(newVertIndex3);
  118.             newIndices.push_back(newVertIndex2);
  119.         }
  120.         retShape.numVerts = retShape.vertices.size();
  121.         retShape.indices.swap(newIndices);
  122.         newIndices.clear();
  123.         retShape.numIndices = retShape.indices.size();
  124.     }
  125.  
  126.  
  127.  
  128.     if(threeD)
  129.     {
  130.         GLfloat thickness = 3.0f;
  131.         //Copy the current vertices one unit backwards
  132.         int vertEnd = retShape.vertices.size();
  133.         for (int i = 0; i < vertEnd; i++)
  134.         {
  135.             vertex behindVert = retShape.vertices[i];
  136.             behindVert.position = behindVert.position + glm::vec3(0.0f, 0.0f, -thickness);
  137.             retShape.vertices.push_back(behindVert);
  138.         }
  139.  
  140.         int indEnd = retShape.indices.size();
  141.         for (int i = 0; i < indEnd; i+=3)
  142.         {
  143.             retShape.indices.push_back(retShape.indices[i] + vertEnd);
  144.             retShape.indices.push_back(retShape.indices[i+2] + vertEnd);
  145.             retShape.indices.push_back(retShape.indices[i+1] + vertEnd);           
  146.         }
  147.  
  148.         //The walls
  149.         for (int i = 0; i < indEnd; i+=3)
  150.         {
  151.             //Current indices
  152.            
  153.  
  154.             //right
  155.             int rightTopLeft = i;
  156.             int rightTopRight = rightTopLeft + indEnd;
  157.             int rightBottomLeft =  rightTopLeft + 2;
  158.             int rightBottomRight = rightTopRight + 1;
  159.  
  160.             retShape.indices.push_back(retShape.indices[rightTopLeft]);
  161.             retShape.indices.push_back(retShape.indices[rightBottomLeft]);
  162.             retShape.indices.push_back(retShape.indices[rightTopRight]);
  163.  
  164.             retShape.indices.push_back(retShape.indices[rightTopRight]);
  165.             retShape.indices.push_back(retShape.indices[rightBottomLeft]);
  166.             retShape.indices.push_back(retShape.indices[rightBottomRight]);
  167.  
  168.             //left
  169.             int leftTopLeft = i + indEnd;
  170.             int leftTopRight = i;
  171.             int leftBottomLeft =  leftTopLeft + 2;
  172.             int leftBottomRight = leftTopRight + 1;
  173.  
  174.             retShape.indices.push_back(retShape.indices[leftTopLeft]);
  175.             retShape.indices.push_back(retShape.indices[leftBottomLeft]);
  176.             retShape.indices.push_back(retShape.indices[leftTopRight]);
  177.  
  178.             retShape.indices.push_back(retShape.indices[leftTopRight]);
  179.             retShape.indices.push_back(retShape.indices[leftBottomLeft]);
  180.             retShape.indices.push_back(retShape.indices[leftBottomRight]);
  181.  
  182.             //bottom
  183.            
  184.             retShape.indices.push_back(retShape.indices[leftBottomLeft]);
  185.             retShape.indices.push_back(retShape.indices[rightBottomRight]);
  186.             retShape.indices.push_back(retShape.indices[leftBottomRight]);
  187.  
  188.             retShape.indices.push_back(retShape.indices[leftBottomRight]);
  189.             retShape.indices.push_back(retShape.indices[rightBottomRight]);
  190.             retShape.indices.push_back(retShape.indices[rightBottomLeft]);
  191.  
  192.         }
  193.  
  194.         retShape.numVerts = retShape.vertices.size();
  195.         retShape.numIndices = retShape.indices.size();
  196.     }
  197.  
  198.  
  199.  
  200. return retShape;
  201. }
  202.  
  203. // Global variables
  204. static int level = 0;
  205.  
  206. // Vertex buffer object IDs
  207. GLuint vbo;
  208. GLuint ibo;
  209. shape shape;
  210.  
  211. void rebuildGasket() {
  212.     shape = makeSierpinski(1, false);
  213. }
  214.  
  215. void bindDataToBuffers() {
  216.   // Bind VBOs and provide data to them
  217.   // ... insert your code here ...
  218.   // ... end of your code ...
  219. glGenBuffers(1, &vbo); //Maybe I should gen all the buffers at once?
  220.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  221.     glBufferData(GL_ARRAY_BUFFER, shape.vertexBufferSize(), &shape.vertices[0], GL_STATIC_DRAW);  
  222.  
  223.     glGenBuffers(1, &ibo);
  224.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  225.     glBufferData(GL_ELEMENT_ARRAY_BUFFER,
  226.         shape.indexBufferSize(), &shape.indices[0], GL_DYNAMIC_DRAW);
  227.  
  228.  
  229.   // Unbind the VBO's after we are done
  230.   glBindBuffer(GL_ARRAY_BUFFER, 0);
  231.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  232.  
  233.   //CHECK_OPENGL;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment