完整的程序代码如下:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <GL/freeglut.h> // Switch this to glut.h if you're using regular GLUT.
#include <GL/glaux.h>//-----------------------------------------------------------------------------
// SYMBOLIC CONSTANTS
//-----------------------------------------------------------------------------
const float PI     = 3.14159265358979f;
const float TWOPI  = 6.28318530717958f;
const float PIDIV2 = 1.57079632679489f;//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
GLuint g_textureID   = -1;
int     g_nResolution = 320;
GLfloat g_heading     = 0.0f;//-----------------------------------------------------------------------------
// Name: LoadBMP()
// Desc: 
//-----------------------------------------------------------------------------
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File = NULL; if( !Filename )
return NULL; File = fopen(Filename,"r"); if( File )
{
fclose(File);
return auxDIBImageLoad(Filename);
} return NULL;
}//-----------------------------------------------------------------------------
// Name: LoadGLTextures()
// Desc: 
//-----------------------------------------------------------------------------
bool LoadGLTextures()
{
bool bStatus = false;
AUX_RGBImageRec *TextureImage = NULL; // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if( TextureImage = LoadBMP( "earth.bmp" ) )
{
bStatus = true; glGenTextures(1, &g_textureID); // Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, g_textureID);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, 
         TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, 
 TextureImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
} if( TextureImage )
{
if( TextureImage->data )
free(TextureImage->data); free(TextureImage);
} return bStatus;
}//-----------------------------------------------------------------------------
// Name: CreateSphere()
// Desc: Create a sphere centered at c_y, c_x, c_z with radius r, and 
//       precision n
//-----------------------------------------------------------------------------
void RenderSphere( float c_x, float c_y, float c_z, float r, int n )
{
   int i = 0;
   int j = 0;   float theta1 = 0.0;
   float theta2 = 0.0;
   float theta3 = 0.0;   float e_x = 0.0f;
   float e_y = 0.0f;
   float e_z = 0.0f;   float p_x = 0.0f;
   float p_y = 0.0f;
   float p_z = 0.0f;   if( r < 0 )
      r = -r;   if( n < 0 )
      n = -n;   if( n < 4 || r <= 0 ) 
   {
      glBegin(GL_POINTS);
      glVertex3f( c_x, c_y, c_z );
      glEnd();
      return;
   }   for( j = 0; j < n/2; ++j )
   {
      theta1 = j * TWOPI / n - PIDIV2;
      theta2 = (j + 1) * TWOPI / n - PIDIV2;      glBegin(GL_QUAD_STRIP);      for( i = 0; i <= n; i++ ) 
      {
         theta3 = i * TWOPI / n;         e_x = cosf(theta2) * cosf(theta3);
         e_y = sinf(theta2);
         e_z = cosf(theta2) * sinf(theta3);
         p_x = c_x + r * e_x;
         p_y = c_y + r * e_y;
         p_z = c_z + r * e_z;         glNormal3f( e_x, e_y, e_z );
         glTexCoord2f( i/(float)n,2*(j+1)/(float)n );
         glVertex3f( p_x, p_y, p_z );         e_x = cosf(theta1) * cosf(theta3);
         e_y = sinf(theta1);
         e_z = cosf(theta1) * sinf(theta3);
         p_x = c_x + r * e_x;
         p_y = c_y + r * e_y;
         p_z = c_z + r * e_z;         glNormal3f( e_x, e_y, e_z );
         glTexCoord2f( i/(float)n,2*j/(float)n );
         glVertex3f( p_x, p_y, p_z );
      }
      glEnd();
   }
}//-----------------------------------------------------------------------------
// Name: InitGL()
// Desc: 
//-----------------------------------------------------------------------------
void InitGL()
{
    glDisable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST); // enable the z-buffer
    glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
    glShadeModel(GL_SMOOTH); // smooth shading between vertices (vertex coloring)
    glClearColor(0.0, 0.0, 0.0, 0.0); // clear color is black
}//-----------------------------------------------------------------------------
// Name: Render()
// Desc: 
//-----------------------------------------------------------------------------
void Render()
{
    // clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // setup the model-view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -5.0);
    glRotatef( g_heading, 0.0, 1.0, 0.0 ); // rotate about the y-axis
    g_heading += 0.5f;    // Assign texture
    glBindTexture(GL_TEXTURE_2D, g_textureID);    // Render a sphere with texture coordinates
    RenderSphere( 0.0f, 0.0f, 0.0f, 1.5f, g_nResolution );    glFinish();        //make sure everything has been sent
    glutSwapBuffers(); //swap the front and back buffers to display the image
}//-----------------------------------------------------------------------------
// Name: Reshape()
// Desc: 
//-----------------------------------------------------------------------------
void Reshape( int w, int h )
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)w / (GLfloat) h, 1.0f, 20.0f);
}//-----------------------------------------------------------------------------
// Name: IdleAnimate()
// Desc: 
//-----------------------------------------------------------------------------
void IdleAnimate()
{
    glutPostRedisplay();
}//-----------------------------------------------------------------------------
// Name: Keyboard()
// Desc: 
//-----------------------------------------------------------------------------
void Keyboard(unsigned char Key, int x, int y)
{
    switch( Key )
    {
        case '-':
            if( g_nResolution > 5 )
                g_nResolution -= 2;
            break;        case '=':
            if( g_nResolution < 30000 )
                g_nResolution += 2;
            break;
    }
}//-----------------------------------------------------------------------------
// Name: main()
// Desc: 
//-----------------------------------------------------------------------------
int main( int argc, char **argv )
{
    // Initialise the GLUT library.
    glutInit(&argc, argv);    // Set up a suitable window for rendering.
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("OpenGL - Textured Sphere");    // Call our own init() function to set up OpenGL parameters
    InitGL();    // Load our texture
    LoadGLTextures();    // Generate the first sphere (octahedron)
    //GenerateNewSphere();    // Set callback functions. Each time an event occurs, GLUT will
    // call the function specified - for instance, whenever it is time
    // to draw the scene, our Display() function will be called.
    glutDisplayFunc(Render);
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Keyboard);
    glutIdleFunc(IdleAnimate);    // Transfer control to GLUT to handle rendering.
    // After this call our callback functions will begin
    // to be executed.
    glutMainLoop();    glDeleteTextures( 1, &g_textureID );    return 0;
}