Advertisement
Guest User

Untitled

a guest
Jan 25th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.79 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/glut.h>
  3.  
  4. /* Using the standard output for fprintf */
  5. #include <iostream>
  6. #include <functional>
  7. #include <memory>
  8.  
  9. #include "Shader.hpp"
  10. #include "Program.hpp"
  11.  
  12. #include <map>
  13. #include <vector>
  14. #include <cmath>
  15. #include <algorithm>
  16. #include <iterator>
  17. #include <time.h>
  18.  
  19.  
  20.  
  21. GLint attribute_coord3d, attribute_v_color;
  22. GLuint vbo_triangle, vbo_triangle_colors;
  23. std::shared_ptr<cs5400::Program> program;
  24. const int NUM_ITERATIONS = 5;
  25. const int NUM_TRIANGLES = pow(4.0,NUM_ITERATIONS);
  26. int modelDrawn=0;
  27.  
  28. struct vec3
  29. {
  30.     float x;
  31.     float y;
  32.     float z;
  33.  
  34.     vec3(float x1, float y1, float z1): x(x1), y(y1), z(z1)
  35.     {}
  36.     vec3():x(0),y(0),z(0)
  37.     {}
  38.  
  39.     vec3& operator+=(const vec3& other)
  40.     {
  41.         x+=other.x;
  42.         y+=other.y;
  43.         z+=other.z;
  44.     }
  45.     vec3 operator/=(const float)
  46.     {
  47.  
  48.     }
  49.     vec3 operator+(const vec3& other) const
  50.     {
  51.         return vec3(x+other.x, y+other.y, z+other.z);
  52.     }  
  53.     vec3 operator/(const float &denom) const
  54.     {
  55.         return vec3(x/denom, y/denom, z/denom);
  56.     }  
  57.     vec3 operator*(const float mult)
  58.     {
  59.         return vec3(x*mult, y*mult, z*mult);
  60.     }
  61.     bool operator<( const vec3 & n ) const
  62.     {
  63.         if(x != n.x)
  64.             return x < n.x;
  65.         else if(y != n.y)
  66.             return y < n.y;
  67.         else if(z != n.z)
  68.             return z < n.z;
  69.         return length() < n.length();  
  70.     }
  71.     bool operator==( const vec3 & n ) const
  72.     {
  73.         return x==n.x&&y==n.y&&z==n.z;  
  74.     }
  75.     float length() const
  76.     {
  77.         return sqrt(pow(x,2)+pow(y,2)+pow(z,2));
  78.     }
  79.     void Print() const
  80.     {
  81.         std::cout<<"("<<x<<","<<y<<","<<z<<")"<<std::endl;
  82.     }
  83. };
  84.  
  85. //container for gasket verts
  86. std::vector<vec3> triangle_vertices;
  87. std::vector<vec3> triangle_colors_vec;
  88.  
  89.  
  90. float getRandom(float max, float min)
  91. {
  92.     return((float(rand()) / float(RAND_MAX)) * (max - (min))) + (min);
  93. }
  94.  
  95. float getVectorDistance(vec3 a, vec3 b)
  96. {
  97.     return sqrt(pow((a.x-b.x),2) + pow((a.y-b.y),2) + pow((a.z-b.z),2));
  98. }
  99.  
  100. vec3 randVector();
  101.  
  102. struct Triangle
  103. {
  104.     vec3 a;
  105.     vec3 b;
  106.     vec3 c;
  107.  
  108.     Triangle(vec3 a1, vec3 b1, vec3 c1):a(a1),b(b1),c(c1)
  109.     {}
  110. };
  111.  
  112. vec3 getMidpoint(const vec3& a, const vec3& b)
  113. {
  114.     const float SCALE = 0.1;
  115.  
  116.     static std::map<std::pair<vec3,vec3>,vec3> memo;
  117.    
  118.  
  119.     auto foundResult = memo.find(std::make_pair(a,b));
  120.     if (foundResult!=memo.end())
  121.     {
  122.         return foundResult->second;
  123.     }
  124.  
  125.     auto result = (a+b)/2;
  126.     auto variance = 0.1 * getVectorDistance(a,b);
  127.     auto randX = getRandom(result.x + variance, result.x - variance);
  128.     auto randY = getRandom(result.y + variance, result.y - variance);
  129.     auto randZ = getRandom(result.z + variance, result.z - variance);
  130.    
  131.     result = vec3(randX,randY,randZ);
  132.     memo.insert(std::make_pair(std::make_pair(a,b),result));
  133.     memo.insert(std::make_pair(std::make_pair(b,a),result));
  134.     return result;
  135.  
  136. }
  137.  
  138. void makeGasket(Triangle t, int count)
  139. {
  140.     if(count > 0)
  141.     {
  142.         auto d = getMidpoint(t.a,t.b);
  143.         auto e = getMidpoint(t.b,t.c);
  144.         auto f = getMidpoint(t.a,t.c);
  145.    
  146.  
  147.         makeGasket(Triangle(t.a,d,f), count - 1);
  148.         makeGasket(Triangle(d,t.b,e), count - 1);
  149.         makeGasket(Triangle(f,d,e), count - 1);
  150.         makeGasket(Triangle(f,e,t.c), count - 1);
  151.     }
  152.     else
  153.     {
  154.         triangle_vertices.push_back(t.a);
  155.         triangle_vertices.push_back(t.b);
  156.         triangle_vertices.push_back(t.c);
  157.  
  158.     }
  159.    
  160. }
  161.  
  162.  
  163. void createColors()
  164. {
  165.     for(int i=0;i<triangle_vertices.size();i++)
  166.     {
  167.         float random = getRandom(0.5,1.0);
  168.         static std::map<vec3,float> randColor;
  169.         auto foundResult = randColor.find(triangle_vertices[i]);
  170.         if (foundResult!=randColor.end())
  171.         {
  172.             random = foundResult->second;
  173.         }
  174.         else
  175.             randColor.insert(std::make_pair(triangle_vertices[i],random));
  176.  
  177.         triangle_colors_vec.push_back(vec3(random,random,random));
  178.     }
  179.  
  180. }
  181.  
  182.  
  183. vec3 triangle_verts[15000];
  184. vec3 triangle_colors[15000];
  185.  
  186. void onDisplay()
  187. {
  188.     srand(time(NULL));
  189.   /* Clear the background as white */
  190.   glClearColor(0.0, 0.0, 0.0, 0.0);
  191.   glEnable(GL_DEPTH_TEST);
  192.   glDepthFunc(GL_LESS);
  193.   glCullFace(GL_FRONT);
  194.   glEnable(GL_CULL_FACE);
  195.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  196.   /* Tell OpenGL which program to use*/
  197.   glUseProgram(program->getHandle());
  198.  
  199.   if(modelDrawn == 0)
  200.   {
  201.         modelDrawn = 1;
  202.         vec3 verts[]=
  203.         {
  204.             vec3(0.0,  1.0, 0.0),
  205.             vec3(0.943,  -0.333, 0.0),
  206.             vec3(-0.471, -0.333, 0.816),
  207.             vec3(-0.471, -0.333, -0.816)
  208.  
  209.         };
  210.  
  211.         Triangle initialTri = Triangle(
  212.           verts[0],
  213.           verts[2],
  214.           verts[1]
  215.           );
  216.         Triangle initialTri2 = Triangle(
  217.           verts[0],
  218.           verts[1],
  219.           verts[3]
  220.          
  221.           );
  222.         Triangle initialTri3 = Triangle(
  223.           verts[0],
  224.           verts[3],
  225.           verts[2]
  226.          
  227.           );
  228.         Triangle initialTri4 = Triangle(
  229.           verts[1],
  230.           verts[2],
  231.           verts[3]
  232.           );
  233.  
  234.         makeGasket(initialTri, NUM_ITERATIONS);
  235.         makeGasket(initialTri2, NUM_ITERATIONS);
  236.         makeGasket(initialTri3, NUM_ITERATIONS);
  237.         makeGasket(initialTri4, NUM_ITERATIONS);
  238.  
  239.         std::copy(triangle_vertices.begin(),triangle_vertices.end(), triangle_verts);
  240.  
  241.         createColors();
  242.  
  243.        
  244.         std::copy(triangle_colors_vec.begin(),triangle_colors_vec.end(), triangle_colors);
  245.  
  246.   }
  247.  
  248.     /* Setup vertex data */
  249.    
  250.    
  251.    
  252.    
  253.     glGenBuffers(1, &vbo_triangle);
  254.     glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
  255.    
  256.     glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_verts), triangle_verts, GL_STATIC_DRAW);
  257.     glEnableVertexAttribArray(attribute_coord3d);
  258.  
  259.    
  260.  
  261.  
  262.   /* Describe our vertices array to OpenGL (it can't guess its format automatically) */
  263.   glVertexAttribPointer(
  264.     attribute_coord3d, // attribute
  265.     3,                 // number of elements per vertex, here (x,y,z)
  266.     GL_FLOAT,          // the type of each element
  267.     GL_FALSE,          // take our values as-is
  268.     0,                 // no extra data between each position
  269.     0 // vec3er to the C array
  270.   );
  271.  
  272.   /* Push each element in buffer_vertices to the vertex shader */
  273.  
  274.  
  275.  
  276.   glGenBuffers(1, &vbo_triangle_colors);
  277.   glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle_colors);
  278.   glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_colors), triangle_colors, GL_STATIC_DRAW);
  279.  
  280.  
  281.   char* attribute_name = "v_color";
  282.   auto attribute_v_color = glGetAttribLocation(program->getHandle(), attribute_name);
  283.   if (attribute_v_color == -1)
  284.     fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
  285.  
  286.  
  287.  
  288.   glEnableVertexAttribArray(attribute_v_color);
  289.   glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle_colors);
  290.   glVertexAttribPointer(
  291.     attribute_v_color, // attribute
  292.     3,                 // number of elements per vertex, here (r,g,b)
  293.     GL_FLOAT,          // the type of each element
  294.     GL_FALSE,          // take our values as-is
  295.     0,                 // no extra data between each position
  296.     0                  // offset of first element
  297.   );
  298.  
  299.   glDrawArrays(GL_TRIANGLES, 0, NUM_TRIANGLES*3*4);
  300.   glDisableVertexAttribArray(attribute_v_color);
  301.   glDisableVertexAttribArray(attribute_coord3d);
  302.  
  303.  
  304.  
  305.   /* Display the result */
  306.   glutSwapBuffers();
  307. }
  308.  
  309. void rotate( int value )
  310. {
  311.    
  312.     //
  313.     float angle = 0.0175; // 1 degree
  314.     //float angle = glutGet(GLUT_ELAPSED_TIME) / 1000.0 * 0.001;
  315.    
  316.     float rotation_matrix[] =
  317.     {
  318.         cosf(angle),0,sinf(angle),
  319.         0,1,0,
  320.         -1.0*sinf(angle),0,cosf(angle),
  321.  
  322.     };
  323.     float rotation_matrix_x[] =
  324.     {
  325.         1,0,0,
  326.         0,cosf(angle),-1.0*sinf(angle),
  327.         0,sinf(angle),cosf(angle),
  328.  
  329.     };
  330.     float rotation_matrix_z[] =
  331.     {
  332.         cosf(angle),-1.0*sin(angle),0,
  333.         sinf(angle),cosf(angle),0,
  334.         0,0,1,
  335.        
  336.  
  337.     };
  338.  
  339.    
  340.  
  341.     vec3 tempVec;
  342.     for(int i=0;i< triangle_vertices.size();i++)
  343.     {
  344.         tempVec.x = rotation_matrix[0] * triangle_verts[i].x + rotation_matrix[1] * triangle_verts[i].y + rotation_matrix[2] * triangle_verts[i].z;
  345.         tempVec.y = rotation_matrix[3] * triangle_verts[i].x + rotation_matrix[4] * triangle_verts[i].y + rotation_matrix[5] * triangle_verts[i].z;
  346.         tempVec.z = rotation_matrix[6] * triangle_verts[i].x + rotation_matrix[7] * triangle_verts[i].y + rotation_matrix[8] * triangle_verts[i].z;
  347.         triangle_verts[i] = tempVec;
  348.     }
  349.     glutTimerFunc(10, rotate,1);
  350.     glutPostRedisplay();
  351. }
  352.  
  353.  
  354.  
  355. int main(int argc, char* argv[])
  356. {
  357.   /* Glut-related initialising functions */
  358.   glutInit(&argc, argv);
  359.   glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
  360.   glutInitWindowSize(640, 480);
  361.   glutCreateWindow("Hello Triangle!");
  362.  
  363.  
  364.  
  365.   /* Extension wrangler initialising */
  366.   GLenum glew_status = glewInit();
  367.   if (glew_status != GLEW_OK)
  368.   {
  369.     fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status));
  370.     return EXIT_FAILURE;
  371.   }
  372.  
  373.   /* When all init functions runs without errors,
  374.   the program can initialise the resources */
  375.   try
  376.   {  
  377.     program = cs5400::make_program
  378.       (
  379.     cs5400::make_vertexShader("vertex.glsl")
  380.       ,cs5400::make_fragmentShader("fragment.glsl")  
  381.       );
  382.     glutDisplayFunc(onDisplay);
  383.     //glutIdleFunc( idle );
  384.    
  385.     glutTimerFunc(10, rotate,1);
  386.     glutMainLoop();
  387.   }
  388.   catch(std::exception& e)
  389.   {
  390.     std::cerr << e.what();
  391.   }
  392.   return EXIT_SUCCESS;
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement