#include #include #include #include "Angel.h" #include #include "keys.h" using namespace std; // globals vector data; vector colors; GLuint loc[2]; GLenum DRAWING_MODE; // Constants const int POINTS = 60; vec3 Color(float theta) { vec3 tmpColor; if(theta < M_PI/2.0) { tmpColor[0] = 1.0; tmpColor[1] = theta/(M_PI/2.0); tmpColor[2] = 0; } else if (theta < M_PI) { tmpColor[0] = theta/(M_PI/2.0) - 1.0; tmpColor[1] = 1.0; tmpColor[2] = 0; } else if (theta < 1.5*M_PI) { tmpColor[0] = 0; tmpColor[1] = theta/(M_PI/2.0) - 2; tmpColor[2] = 1.0; } else { tmpColor[0] = 4- theta/(M_PI/2.0) ; tmpColor[1] = theta/(M_PI/2.0) - 3; tmpColor[2] = 1.0; } return tmpColor; } void Init(void) { vec3 tmp; vec3 tmpColor; float theta; srand(time(NULL)); // background clear color glClearColor(1.0,1.0,1.0,1.0); // I want to be able to see what is happening with polygons glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // generate the data on a circle for( theta=0;theta<2*M_PI;theta+= 2*M_PI/POINTS) { tmp.x = .75*cos(theta); tmp.y = .75*sin(theta); tmp.z = 0; data.push_back(tmp); data.push_back(Color(theta)); } // a default mode for drawing things DRAWING_MODE = GL_POINTS; glEnable(GL_PROGRAM_POINT_SIZE); // first we need to load shaders GLuint program = InitShader( "vshader2.glsl", "fshader2.glsl" ); // now a vertex array object GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao); // now a buffer to hold our data. GLuint buffer[1]; glGenBuffers(1, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(vec3) , data.data() , GL_STATIC_DRAW); loc[0] = glGetAttribLocation( program, "vPosition" ); glVertexAttribPointer(loc[0],3,GL_FLOAT, GL_FALSE,2*3*sizeof(GLfloat) , BUFFER_OFFSET(0)); loc[1] = glGetAttribLocation( program, "vColor" ); glEnableVertexAttribArray( loc[1] ); glVertexAttribPointer(loc[1],3,GL_FLOAT, GL_FALSE, 2*3*sizeof(GLfloat), BUFFER_OFFSET(sizeof(vec3))); return; } void display(void) { glClear( GL_COLOR_BUFFER_BIT ); glEnableVertexAttribArray( loc[0] ); glEnableVertexAttribArray( loc[1] ); glDrawArrays(DRAWING_MODE, 0, data.size()/2); glutSwapBuffers(); return; } int main( int argc, char **argv ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); glutInitWindowSize( 512, 512 ); glutCreateWindow( "Primitive Modes, New Code" ); GLenum err = glewInit(); if (GLEW_OK != err) { cout << "glewInit failed, aborting." << endl; exit (1); } Init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }