#include #include #include /* * skel.c: OpenGL skeleton program * * 1/03 pjf * * written mainly to show the various callbacks * * doesn't do any 3D stuff * */ /* display callback */ void display() { fprintf(stderr,"display.\n"); glClear(GL_COLOR_BUFFER_BIT); glColor3f(drand48(),drand48(),drand48()); /* pick a random color */ glRectf(-1.0,-1.0,1.0,1.0); glFlush(); } /* idle callback: called whenever nothing else is happening. */ /* note: printf commented out because this is called a lot. */ void idle(void) { // fprintf(stderr,"idle.\n"); } /* mouse: called when a mouse button is pressed or released. btn identifies which button; state identifies "press" or "release" */ void mouse(int btn, int state, int x, int y) { fprintf(stderr,"mouse: btn %d, state %d, location (%d,%d).\n",btn,state,x,y); } /* called initially (before first call to display()) and whenever the window is resized */ void reshape(int w, int h) { fprintf(stderr,"reshape: new size %d x %d.\n",w,h); // must update viewport and clipping planes glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } void ticktimer(int value) { fprintf(stderr,"tick (%d)\n",value); } void tocktimer(int value) { fprintf(stderr,"tock (%d)\n",value); } void visibility(int state) { fprintf(stderr,"Visibility now %d.\n",state); } void special(int key,int x,int y) { fprintf(stderr,"Special: key %d, loc (%d %d).\n",key,x,y); } void motion(int x, int y) { fprintf(stderr,"Motion: loc (%d %d).\n",x,y); } /* note: commented out because this is called a lot */ void passivemotion(int x, int y) { fprintf(stderr,"Passive Motion: loc (%d %d).\n",x,y); } void keyboard(int key,int x,int y) { fprintf(stderr,"Keyboard: key %d, loc (%d %d).\n",key,x,y); } void entry(int state) { fprintf(stderr,"Entry: state %d.\n",state); } void init(void) { /* nothing to do! */ } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow(argv[0]); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMouseFunc(mouse); glutTimerFunc(1000,ticktimer,1963); glutTimerFunc(2000,tocktimer,20012); glutVisibilityFunc(visibility); glutSpecialFunc(special); glutMotionFunc(motion); glutPassiveMotionFunc(passivemotion); glutKeyboardFunc(keyboard); glutEntryFunc(entry); init(); glutMainLoop(); return 0; }