/* * animateSquare.cpp * * Created on: Feb 15, 2013 * Author: debure */ #include #include #define DTOR 3.14159/180.0 // convert degrees to radians float theta; // angle of rotation void myinit(void) { glClearColor(0.0, 0.0, 0.0, 1.0); // black background glColor3f(0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0, 2.0, -2.0, 2.0); // sets up the viewing transform glMatrixMode(GL_MODELVIEW); } void display(void) { glEnableClientState(GL_VERTEX_ARRAY); // must have this set to use glDrawElements // square is created at the origin GLfloat sq_vertices[8][2] = {{-1.0,-1.0},{1.0,-1.0},{1.0, 1.0},{-1.0,1.0}}; GLubyte sqIndices[4]={0,1,2,3}; glVertexPointer(2, GL_FLOAT, 0, sq_vertices); // tells where the array of vertices is located // code for rotating square glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glLoadIdentity(); glRotatef(theta*DTOR, 0.0, 0.0, 1.0); glColor3f(0.0, 1.0, 1.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, sqIndices); // replaces GL_BEGIN..GL_END sequence glPopMatrix(); glutSwapBuffers(); glFlush(); } void idle(void){ theta += 5; // modify rotation angle in degrees that is used in display glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB ); // double buffering needed for animation glutInitWindowSize(600, 600); glutInitWindowPosition(0,0); glutCreateWindow("Rotating Square"); glutDisplayFunc(display); glutIdleFunc(idle); myinit(); glutMainLoop(); return 0; }