/* Drawing exercise. Allows to translate/rotate when user presses l/r/u/d/x/X,y/Y,z/Z). OpenGL 1.x Laura Toma */ #include #include #include #include //this allows this code to compile both on apple and linux platforms #ifdef __APPLE__ #include #else #include #endif #include using namespace std; /* global variables */ const int WINDOWSIZE = 500; //we predefine some colors for convenience GLfloat red[3] = {1.0, 0.0, 0.0}; GLfloat green[3] = {0.0, 1.0, 0.0}; GLfloat blue[3] = {0.0, 0.0, 1.0}; GLfloat black[3] = {0.0, 0.0, 0.0}; GLfloat white[3] = {1.0, 1.0, 1.0}; GLfloat gray[3] = {0.5, 0.5, 0.5}; GLfloat yellow[3] = {1.0, 1.0, 0.0}; GLfloat magenta[3] = {1.0, 0.0, 1.0}; GLfloat cyan[3] = {0.0, 1.0, 1.0}; //keep track of global translation and rotation GLfloat pos[3] = {0,0,0}; GLfloat theta[3] = {0,0,0}; /* forward declarations of functions */ void display(void); void keypress(unsigned char key, int x, int y); void cube(GLfloat side, int mode); void draw_sphere(float rad); void draw_my_scene(); void draw_circle(float rad); void draw_flower(); int main(int argc, char** argv) { /* open a window and initialize GLUT stuff */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(WINDOWSIZE, WINDOWSIZE); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); /* register callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); /* OpenGL init */ /* set background color black*/ glClearColor(0, 0, 0, 0); glEnable(GL_DEPTH_TEST); /* setup the camera (i.e. the projection transformation) */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, 1 /* aspect */, 1, 10.0); /* the frustrum is from z=-1 to z=-10 */ /* camera is at (0,0,0) looking along negative y axis */ //the points are in z=[-1,1]. initialize the translation to bring //the points in the view frustrum which is [-1, -10] pos[2] = -3; //move it down, look at it from above //pos[1] = -1.3; /* start the event handler */ glutMainLoop(); return 0; } /* this function is called whenever the window needs to be rendered */ void display(void) { //clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear all modeling transformations glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* The default GL window is x=[-1,1], y= [-1,1], z=[-1,1] with the origin in the center. The view frustrum was set up from z=-1 to z=-10. The camera is at (0,0,0) looking along negative z axis. */ /* First we translate/rotate our local reference system with the current translation and rotation that have been accumulated in the globals pos[] and theta[] */ glTranslatef(pos[0], pos[1], pos[2]); glRotatef(theta[0], 1,0,0); //rotate theta[0] around x-axis, etc glRotatef(theta[1], 0,1,0); glRotatef(theta[2], 0,0,1); /* Now we draw the scene. Our local reference system is still [-1,1]x [-1,1] x [-1,1] */ draw_my_scene(); glFlush(); } void draw_my_scene() { //draw a cube of side 1, centered at (0,0,0) glColor3fv(green); cube(1, 0); //glColor3fv(red); // cube(.5); //draw_sphere(.5); //glColor3fv(yellow); //draw_circle(.5); glRotatef(45, 0, 0, 1); draw_flower(); glRotatef(-45, 0, 0, 1); draw_sphere(.6); } void draw_circle(float rad) { int N = 100; float angle = 2* M_PI/N; float x, y; int i; glBegin(GL_POLYGON); for (i=0; i