/* * stopGo.cpp * * Created on: Mar 1, 2013 * Author: debure */ #include #include #include #include #include using namespace std; #define DTOR 3.14159/180.0 float theta; void myinit(void) { glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 500.0, 0.0); glMatrixMode(GL_MODELVIEW); glEnableClientState(GL_VERTEX_ARRAY); } void display(void) { // square is created at the origin GLfloat sq_vertices[4][2] = {{-1.0,-1.0},{1.0,-1.0},{1.0, 1.0},{-1.0,1.0}}; GLubyte sqIndices[4]={0,3,2,1}; GLfloat colors[8][3] = {{0.0, 0.0, 0.0},{1.0, 0.0, 0.0}, {1.0, 1.0, 0.0},{0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0},{1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}}; glClear(GL_COLOR_BUFFER_BIT); // code for rotating square glVertexPointer(2, GL_FLOAT, 0, sq_vertices); glColorPointer(3, GL_FLOAT, 0, colors); glEnableClientState(GL_COLOR_ARRAY); glPushMatrix(); glLoadIdentity(); glTranslatef(250.0, 250.0, 0.0); glScalef(45.0, 45.0, 0.0); glRotatef(theta*DTOR, 0.0, 0.0, 1.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, sqIndices); glPopMatrix(); glDisableClientState(GL_COLOR_ARRAY); // stop glColor3f(1.0, 0, 0); glPushMatrix(); glLoadIdentity(); glTranslatef(75.0, 75.0, 0.0); glScalef(25.0, 25.0, 0.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, sqIndices); glPopMatrix(); glColor3f(0, 1.0, 0); // go glPushMatrix(); glLoadIdentity(); glTranslatef(135.0, 75.0, 0.0); glScalef(25.0, 25.0, 0.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, sqIndices); glPopMatrix(); glutSwapBuffers(); glFlush(); } void idle(void){ theta += 10; // modify rotation angle that is used in display glutPostRedisplay(); } void mouse(int button, int state, int x, int y){ if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN && x >= 50 && x <= 100 && y >= 50 && y <= 100) // check if inside red sq glutIdleFunc(NULL); if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN && x >= 110 && x <=160 && y >= 50 && y <= 100) // check if inside green sq glutIdleFunc(idle); if (button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) exit(0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB ); glutInitWindowSize(500, 500); glutInitWindowPosition(0,0); glutCreateWindow("Graphics Primitives"); glutDisplayFunc(display); glutMouseFunc(mouse); glutIdleFunc(idle); myinit(); glutMainLoop(); return 0; }