#include #include #include /* This program draws a triangle and a rectangle and provides keyboard input that can be used to move/rotate/scale both of them. 'r','l','d','u' moves them right,left,down and up 't' rotates them by 5 degrees counter clockwise 's','S' scales them down/up */ GLfloat x=0,y=0,sx=1,sy=1,t=0; void display() { glClear(GL_COLOR_BUFFER_BIT); /*we need to set the transformation matrices before we draw the objects This can be done by first clearing the modelview matrix and then calling translate, rotate.... this might not be the best alternative because we want to hold on to earlier transformations that were applied. We can do that by using glPushMatrix() and glPopMatrix() */ glLoadIdentity(); glTranslatef(x,y,0); glRotatef(t,0,0,1); glScalef(sx,sy,1); glColor3f(0.8,0.7,0.9); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glRectf(0.2,0.5,0.4,0.7); glBegin(GL_TRIANGLES); glVertex2f(0.0,0.5); glVertex2f(-0.5,0.0); glVertex2f(0.5,0.0); glEnd(); /* uncomment the following (and comment the above set of draw commands - from glPolygon Mode) to view a polygon with a hole */ /* glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glRectf(-0.5,-0.5,0.5,0.5); glPushMatrix(); glRotatef(45,0,0,1); glTranslatef(0.1,0.1,0.0); glRectf(0,0,0.2,0.2); glPopMatrix(); */ glFlush(); /*try glutSwapBuffers() instead of this*/ //glutSwapBuffers(); } void reshape(int width, int height) { /* this function sets the viewport so that the display is clipped properly. It also set up the projection matrix so that the aspect ration matches that of the window -- it is called whenever there is a change in the size of the window. */ glViewport(0, 0, (GLint) width, (GLint) height); /* You would have to set up the projection and modelview matrix in a typical application -- we don't need it for this simple application*/ /* glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -10.0); you also set up your camera position here using gluLookAt(). This has an effect equivalent to transforming the objects. */ } void init() { /* setting the background color to black*/ glClearColor(0, 0, 0, 0); } void hit(unsigned char i, int a, int b) { /* keyboard input handler*/ switch(i){ case 'r': x+= 0.05; break; case 'l': x-=0.05; break; case 'u': y+= 0.05; break; case 'd': y-=0.05; break; case 't': t+=5; break; case 's': sx-=0.05; sy-=0.05; break; case 'S': sx+=0.05; sy+=0.05; break; case 'q': exit(1); break; } /*redraw the screen using the new inputs -- basically force a call to the display function*/ glutPostRedisplay(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB); /*you will need GLUT_DOUBLE also later for animation*/ //glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("First Program"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(hit); //glutIdleFunc(idle); glutMainLoop(); }