/*Simple 2D opengl code to draw two different primitives (circle and square). *Version with a "BUG" to be fixed!!!** Program allows for setting the color of the objects and to set "positions" and "sizes" of those objects through the "Size" and "Move" functions per primitive For use in the high school shadow day - Cal Poly April 2009, base program by Zoe Wood */ #include #include #include #include #include #include #include int GW, GH; /*variables for the position and size of the square */ float s_tx, s_ty, s_sx, s_sy; /*variables for the position and size of the circle */ float c_tx, c_ty, c_sx, c_sy; /*variables for setting the color */ typedef enum {black, white, red, green, blue, yellow, magenta, cyan} color; #define BLACK 0.0, 0.0, 0.0 #define WHITE 1.0, 1.0, 1.0 #define RED 1.0, 0.0, 0.0 #define GREEN 0.0, 1.0, 0.0 #define BLUE 0.0, 0.0, 1.0 #define YELLOW 1.0, 1.0, 0.0 #define MAGENTA 1.0, 0.0, 1.0 #define CYAN 0.0, 1.0, 1.0 #define PI 3.14159265358979 void DrawCircle(float r); void DrawSquare(); void ChooseColor(color); /*Function to "move" the square */ void MoveSquare(float tx, float ty) { c_tx = tx; c_ty = ty; } /*Function to set the size of the square */ void SizeSquare(float sx, float sy) { c_sx = sx; c_sy = sy; } /*Function to "move" the circle */ void MoveCircle(float tx, float ty) { s_tx = tx; s_ty = ty; } /*Function to set the size of the circle */ void SizeCircle(float sx, float sy) { s_sx = sx; s_sy = sy; } /* Do all your drawing here */ void display() { glClear(GL_COLOR_BUFFER_BIT); /* set up and draw a circle */ SizeCircle(0.5, 0.5); MoveCircle(.5, 0); ChooseColor(yellow); DrawCircle(0.5); /*set up and draw a square */ SizeSquare(0.5, 0.5); MoveSquare(-.5, 0); ChooseColor(blue); DrawSquare(); glutSwapBuffers(); } /*Function to draw the square - do not modify */ void DrawSquare() { glPushMatrix(); glTranslatef(s_tx, s_ty, 0); glScalef(s_sx, s_sy, 1); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glPopMatrix(); } /*Function to draw the circle - do not modify */ void DrawCircle(float r) { glPushMatrix(); glTranslatef(c_tx, c_ty, 0); glScalef(c_sx, c_sy, 1); glBegin(GL_TRIANGLE_FAN); glVertex2d(0, 0); for (float phi = 0.0; phi < 2*PI; phi = phi + PI/36) { glVertex2f(r*cos(phi), r*sin(phi)); } glEnd(); glPopMatrix(); } /* Color selecting helper function. */ void ChooseColor(color c) { switch(c) { case black: glColor3f(BLACK); break; case white: glColor3f(WHITE); break; case red: glColor3f(RED); break; case blue: glColor3f(BLUE); break; case green: glColor3f(GREEN); break; case yellow: glColor3f(YELLOW); break; case magenta: glColor3f(MAGENTA); break; case cyan: glColor3f(CYAN); break; default: glColor3f(WHITE); printf("Invalid color: %d\n", c); } } /*function to preserve the aspect ratio of the scene based on the user's sizing of the window */ void reshape(int w, int h) { GW = w; GH = h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0*(float)w/h, 2*(float)w/h, -2, 2); glMatrixMode(GL_MODELVIEW); glViewport(0, 0, w, h); } /* mouse function - not needed for this program, left in, in case you want to play with it */ void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON) { if (state == GLUT_DOWN) { /* if the left button is clicked */ printf("mouse clicked at %d %d\n", x, GH-y-1); } } glutPostRedisplay(); } int main(int argc, char** argv) { //initialize globals GW = 400; GH = 400; s_sx = s_sy = c_sx = c_sy = 1; s_tx = s_ty = c_tx = c_ty = 0; glutInit(&argc, argv); glutInitWindowSize(400, 400); glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); glutCreateWindow("drawing primitives"); glClearColor(1.0, 1.0, 1.0, 1.0); //register callback function */ glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); //start the main loop glutMainLoop(); }