Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VIII
以下代码用glutPostRedisplay函数代替了原来的空闲函数的回调.
#include <stdio.h> #include <stdlib.h> #include <math.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut.h> #endif // angle of rotation for the camera direction float angle = 0.0f; // actual vector representing the camera's direction float lx=0.0f,lz=-1.0f, ly = 0.0f; // XZ position of the camera float x=0.0f, z=5.0f, y = 1.75f; // the key states. These variables will be zero //when no key is being presses float deltaAngle = 0.0f; float deltaMove = 0; int xOrigin = -1; // width and height of the window int h,w; // variables to compute frames per second int frame; long time, timebase; char s[50]; // variables to hold window identifiers int mainWindow, subWindow1,subWindow2,subWindow3; //border between subwindows int border = 6; void setProjection(int w1, int h1) { float ratio; // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). ratio = 1.0f * w1 / h1; // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Set the viewport to be the entire window glViewport(0, 0, w1, h1); // Set the clipping volume gluPerspective(45,ratio,0.1,1000); glMatrixMode(GL_MODELVIEW); } void changeSize(int w1,int h1) { if(h1 == 0) h1 = 1; // we're keeping these values cause we'll need them latter w = w1; h = h1; // set subwindow 1 as the active window glutSetWindow(subWindow1); // resize and reposition the sub window glutPositionWindow(border,border); glutReshapeWindow(w-2*border, h/2 - border*3/2); setProjection(w-2*border, h/2 - border*3/2); // set subwindow 2 as the active window glutSetWindow(subWindow2); // resize and reposition the sub window glutPositionWindow(border,(h+border)/2); glutReshapeWindow(w/2-border*3/2, h/2 - border*3/2); setProjection(w/2-border*3/2,h/2 - border*3/2); // set subwindow 3 as the active window glutSetWindow(subWindow3); // resize and reposition the sub window glutPositionWindow((w+border)/2,(h+border)/2); glutReshapeWindow(w/2-border*3/2,h/2 - border*3/2); setProjection(w/2-border*3/2,h/2 - border*3/2); } void drawSnowMan() { glColor3f(1.0f, 1.0f, 1.0f); // Draw Body glTranslatef(0.0f ,0.75f, 0.0f); glutSolidSphere(0.75f,20,20); // Draw Head glTranslatef(0.0f, 1.0f, 0.0f); glutSolidSphere(0.25f,20,20); // Draw Eyes glPushMatrix(); glColor3f(0.0f,0.0f,0.0f); glTranslatef(0.05f, 0.10f, 0.18f); glutSolidSphere(0.05f,10,10); glTranslatef(-0.1f, 0.0f, 0.0f); glutSolidSphere(0.05f,10,10); glPopMatrix(); // Draw Nose glColor3f(1.0f, 0.5f, 0.5f); glRotatef(0.0f,1.0f, 0.0f, 0.0f); glutSolidCone(0.08f,0.5f,10,2); glColor3f(1.0f, 1.0f, 1.0f); } void renderBitmapString( float x, float y, float z, void *font, char *string) { char *c; glRasterPos3f(x, y,z); for (c=string; *c != '