/***************************************************** * * TwoCube.c * * Jason Ng 100383886 * * Question 1b - Assignment 1 * Displays 2 cubes rotating in opposite directions * *****************************************************/ // standard include files required by all GLUT programs #include #include #include #include #include int window; // the window id of the window in our program float translateCube = -40.0; float fov = 45.0; void gfxinit() { // enable the depth buffer glEnable(GL_DEPTH_TEST); // set the projection to perspective glMatrixMode(GL_PROJECTION); gluPerspective(1.0*fov, 1.0, 1.0, 60.0); // set up the viewing transformation // the view is at (0, 3, 0) and they are looking at (0,0,0) // the up direction is (0, 0, 1) - the z axis glMatrixMode(GL_MODELVIEW); gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glColor3f(1, 1, 1); //glutSolidCube(1.0); for (int count = 0; count < 20; count++) { float r = (1.0*(rand()%11))/10; float g = (1.0*(rand()%256))/256.0; float b = (1.0*(rand()%256))/256.0; translateCube = translateCube + 2.5; glTranslatef(translateCube, 0.0, 0.0); glColor3f(r, g, b); glutSolidCube(1.0); translateCube = 0.0; } glutSwapBuffers(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'z': fov -= 5.0; if (fov <= 10.0) { fov = 10.0; } printf ("Field Of View is now %4.1f\n", fov); break; case 'x': fov += 5.0; if (fov >= 120.0) fov = 120.0; printf ("Field Of View is now %4.1f\n", fov); break; case 'q': exit(0); break; default: break; } } /* * The main function */ int main(int argc, char **argv) { // Initialize GLUT and process its command line arguments glutInit(&argc,argv); // Set the display mode, double buffered, RGB colour and depth buffer glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(600, 600); // Create the display window window = glutCreateWindow("Assignment 1"); // Set the display function glutDisplayFunc(display); glutKeyboardFunc(keyboard); // Set the Idle function, called when GLUT isn't busy // Initialize OpenGL gfxinit(); // Pass control to GLUT to run the application glutMainLoop(); }