Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

GL code

By: a guest on Jun 17th, 2013  |  syntax: C++  |  size: 2.32 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include "Angel.h"
  4.  
  5. typedef vec2 point2;
  6. const int numPoints = 5000;
  7.  
  8. void init(void) {
  9.  
  10.     //Initialize random number generator
  11.     std::srand((uint)std::time(0));
  12.    
  13.     point2 points[numPoints];
  14.    
  15.     //The vertices of a triangle in a plane
  16.     point2 vertices[3] ={point2(-1.0,-1.0),
  17.         point2(0.0,1.0),
  18.         point2(1.0, -1.0)};
  19.    
  20.     //An arbitrary point inside the triangle
  21.     points[0] = point2(0.25, 0.25);
  22.    
  23.     for(int k = 1; k < numPoints; ++k) {
  24.        
  25.         //Picks a random vertex
  26.         int j = rand()%3;
  27.        
  28.         //Computes the point halfway between the
  29.         //selected vertex and the previous point
  30.         points[k] = (points[k-1]+vertices[j])/2.0;
  31.     }
  32.     // Load shaders and use the resulting shader program
  33.     GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
  34.     glUseProgram( program );
  35.     // Create a vertex array object
  36.     GLuint vao;
  37.     glGenVertexArraysAPPLE( 1, &vao );
  38.     glBindVertexArrayAPPLE( vao );
  39.     // Create and initialize a buffer object
  40.     GLuint buffer;
  41.     glGenBuffers( 1, &buffer );
  42.     glBindBuffer( GL_ARRAY_BUFFER, buffer );
  43.     glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );
  44.     // Initialize the vertex position attribute from the vertex shader
  45.     GLuint loc = glGetAttribLocation( program, "vPosition" );
  46.     glEnableVertexAttribArray( loc );
  47.     glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,
  48.                           BUFFER_OFFSET(0) );
  49.     glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
  50. }
  51. //----------------------------------------------------------------------
  52. void display( void ) {
  53.     glClear( GL_COLOR_BUFFER_BIT );     // clear the window
  54.     glDrawArrays( GL_POINTS, 0, numPoints );    // draw the points
  55.     glFlush();
  56. }
  57. //----------------------------------------------------------------------
  58. int main( int argc, char **argv ) {
  59.     glutInit( &argc, argv );
  60.     glutInitDisplayMode( GLUT_RGBA | GLUT_3_2_CORE_PROFILE);
  61.     glutInitWindowSize( 512, 512 );
  62.     // If you are using freeglut, the next two lines will check if
  63.     // the code is truly 3.2. Otherwise, comment them out
  64.     glutCreateWindow( "Sierpinski Gasket" );
  65.     init();
  66.     glutDisplayFunc( display );
  67.     glutMainLoop();
  68.     return 0; }