/*********************************************************************** * On a CSU CS department Sun workstation, compile with * * gcc -o rotatebox rotatebox.c -I/s/parsons/l/sys/Mesa-3.0/include -L/s/parsons/l/sys/Mesa-3.0/lib/SunOS -lm -lMesaGL -lMesaGLw -lMesaGLU -lglut -lXm -lXt -lX11 -lXi * * To run this on a Sun, you may need to define your LD_LIBRARY_PATH * environmental variable. You can do this by placing this line in your * .cshrc file in your home directory: * * setenv LD_LIBRARY_PATH /s/parsons/l/sys/Mesa-3.0/lib/SunOS * * On a CSU CS department Linux workstation, compile with * * gcc -o rotatebox rotatebox.c -L/usr/X11R6/LessTif/Motif1.2/lib -L/usr/X11R6/lib -lm -lMesaGLw -lglut -lGLU -lGL -lXm -lXt -lX11 -lXi * * To run this on a Linux machine, you may need to define your LD_LIBRARY_PATH * environmental variable. You can do this by placing this line in your * .cshrc file in your home directory: * * setenv LD_LIBRARY_PATH /s/parsons/l/sys/Mesa-3.0/lib/Linux **********************************************************************/ /* Normal Includes */ #include #include /* OpenGL Includes */ #include #include #include int MouseX,MouseY; double Phi=0, Theta=0; void mouseclick(int b, int s, int x, int y); void mousemove(int x, int y); void key(unsigned char c, int x, int y); void display(); main(int argc, char **argv) { /* Initialize OpenGL */ glutInit(&argc,argv); /* We want a double buffered display w/ RGB Color and a Depth Buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); /* How big of a window do we want? */ glutInitWindowSize(200,200); /* Where do we want the window? */ glutInitWindowPosition(100,100); /* Make the window, call it whatever the name of our program is */ glutCreateWindow(argv[0]); /* What function should be notified of mouse events? */ glutMouseFunc(mouseclick); /* What function should be notified of mouse events? */ glutMotionFunc(mousemove); /* What function should be notified of Keyboard Events? */ glutKeyboardFunc(key); /* What function gets called to redraw the screen? */ glutDisplayFunc(display); /* What color is the background? */ glClearColor(0,0,0,0); /* Lets set up the Projection Matrix */ glMatrixMode(GL_PROJECTION); /* Reset it */ glLoadIdentity(); /* Set up the projection Matrix for Orthographic or Perspective: */ /* glOrtho(-10,10,-10,10,1,100); */ gluPerspective(75,1,1,100); /* Choose a Shading Model: */ glShadeModel(GL_FLAT); /* glShadeModel(GL_SMOOTH); */ /* Enable the Z-Buffer */ glEnable(GL_DEPTH_TEST); /* Let the program begin! */ glutMainLoop(); } /* Mouse Click handler */ void mouseclick(int b, int s, int x, int y) { MouseX = x; MouseY = y; printf("MOUSE CLICK: Button: %d Status: %d Location: (%d,%d)\n",b,s,x,y); } /* Mouse Move handler */ void mousemove(int x, int y) { Theta += .02 * (MouseX - x); Phi -= .02 * (MouseY - y); if (Phi >= M_PI_2 || Phi <= -M_PI_2) Phi += .02 * (MouseY - y); printf("MOUSE MOVE: Location: (%d,%d)\n",x,y); MouseX = x; MouseY = y; /* Tell GL we need to Re Display */ glutPostRedisplay(); } /* Keyboard Handler */ void key(unsigned char c, int x, int y) { printf("KEY: %c (%d) Location (%d,%d)\n",c,c,x,y); } /* Redraw Handler */ void display() { double Camx, Camy, Camz; /* Lets set up the modelview matrix: */ glMatrixMode(GL_MODELVIEW); /* Reset it */ glLoadIdentity(); /* Compute the Camera Location */ Camx=cos(Phi)*sin(Theta) * 5; Camy=sin(Phi) * 5; Camz=cos(Theta)*cos(Phi) * 5; /* Tell it where we are, where we are looking, and where is up */ gluLookAt(Camx,Camy,Camz, 0,0,0, 0,1,0); /* Clear the screen */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Begin Drawing Quadrilaterals */ glBegin(GL_QUADS); /* Quad 1 */ glColor3f(0.0,0.7,0.1); /* Greenish */ glVertex3f(-1.0,1.0,1.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(-1.0,-1.0,1.0); /* Quad 2 */ glColor3f(0.9,1.0,0.1); /* Yellowish */ glVertex3f(-1.0,1.0,-1.0); glVertex3f(1.0,1.0,-1.0); glVertex3f(1.0,-1.0,-1.0); glVertex3f(-1.0,-1.0,-1.0); /* Quad 3 */ glColor3f(0.2,0.2,1.0); /* Blueish */ glVertex3f(-1.0,1.0,1.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,1.0,-1.0); glVertex3f(-1.0,1.0,-1.0); /* Quad 4 */ glColor3f(0.7,0.0,0.1); /* Reddish */ glVertex3f(-1.0,-1.0,1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(1.0,-1.0,-1.0); glVertex3f(-1.0,-1.0,-1.0); /* Done Drawing Quadrilaterals */ glEnd(); /* Display to the screen what we have drawn */ glutSwapBuffers(); }