#include <stdlib.h>
#include <iostream.h>
#include <time.h>
#include "glut.h"
GLfloat r = 1.0, g = 0.0, b = 0.0; /* drawing color */static GLfloat angle = 0.0;    //used for rotation of clock
float x=0.0;                   //axis of rotation is (x, y, 0)
float y=0.0;GLuint list;                   //list of clock components   
//===============================================================
// initialize lighting
//===============================================================
 
void init(void)
{}//===============================================================
// create clock components
//===============================================================
void clockComponents(void)

GLUquadricObj *clock;
list = glGenLists(1);
clock = gluNewQuadric();

gluQuadricDrawStyle(clock, GLU_FILL); /* smooth shaded */
// gluQuadricDrawStyle(clock, GLU_LINE); /* smooth shaded */
//gluQuadricNormals(clock, GLU_SMOOTH);
glNewList(list, GL_COMPILE); //small ball
gluSphere(clock, 0.07, 10, 4);
glEndList();

// gluQuadricDrawStyle(clock, GLU_FILL); /* flat shaded */
gluQuadricNormals(clock, GLU_FLAT);    
glNewList(list+1, GL_COMPILE);
gluCylinder(clock, 1.55, 1.55, 1, 50, 4);
glEndList();
// gluQuadricDrawStyle(clock, GLU_FILL); /* flat shaded */
gluQuadricNormals(clock, GLU_FLAT);
glNewList(list+2, GL_COMPILE);
gluDisk(clock, 0.0, 1.55, 50, 4);
glEndList();
glNewList(list+3, GL_COMPILE);
gluCylinder(clock, 0.03, 0.03, 1.4, 50, 1);
glEndList();
glNewList(list+4, GL_COMPILE);
gluCylinder(clock, 0.06, 0.05, 1.1, 50, 1);
glEndList(); glNewList(list+5, GL_COMPILE);
gluCylinder(clock, 0.1, 0.05, 0.9, 50, 1);
glEndList();
// gluQuadricDrawStyle(clock, GLU_FILL); /* flat shaded */
gluQuadricNormals(clock, GLU_FLAT);
glNewList(list+6, GL_COMPILE);
gluDisk(clock, 0.0, 1.65, 50, 4);
glEndList();}
void display(void)
{//===============================================================
//display the clock (arrange components in right places) 
//=============================================================== glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();
glColor3f(1, 1, 0);
glCallList(list+2);
glPopMatrix(); glPushMatrix();
glColor3f(1, 0, 1);
glTranslatef(0.0, 0.0, -1.0); 
glCallList(list+1);
glPopMatrix();
glPushMatrix();
glColor3f(0, 1, 1);
glTranslatef(0.0, 0.0, -1.0); 
glCallList(list+6);
glPopMatrix();
glPushMatrix();
glColor3f(0, 0, 0);
glTranslatef(0.0, 0.0, 0.1); 
glCallList(list);
glPopMatrix();    glPushMatrix();
      glColor3f(0, 1, 0);
glTranslatef(1.4, 0.0, 0.0); 
glCallList(list);
glPopMatrix();    glPushMatrix();
        glTranslatef(1.21, -0.7, 0.0); 
glCallList(list);
glPopMatrix(); glPushMatrix();
        glTranslatef(0.7, -1.21, 0.0); 
glCallList(list);
glPopMatrix();    glPushMatrix();
        glTranslatef(0.0, -1.4, 0.0); 
glCallList(list);
glPopMatrix(); glPushMatrix();
        glTranslatef(-0.7, -1.21, 0.0); 
glCallList(list);
glPopMatrix(); glPushMatrix();
        glTranslatef(-1.21, -0.7, 0.0); 
glCallList(list);
glPopMatrix(); glPushMatrix();
        glTranslatef(-1.4, 0.0, 0.0); 
glCallList(list);
glPopMatrix();
glPushMatrix();
        glTranslatef(-1.21, 0.7, 0.0); 
glCallList(list);
glPopMatrix(); glPushMatrix();
        glTranslatef(-0.7, 1.21, 0.0); 
glCallList(list);
glPopMatrix();
glPushMatrix();
        glTranslatef(0.0, 1.4, 0.0); 
glCallList(list);
glPopMatrix(); glPushMatrix();
        glTranslatef(0.7, 1.21, 0.0); 
glCallList(list);
glPopMatrix(); glPushMatrix();
        glTranslatef(1.21, 0.7, 0.0); 
glCallList(list);
glPopMatrix();
struct tm*currentTime;
time_t longTime;
float n,m,s;
time(&longTime);
    currentTime=localtime(&longTime);
n=currentTime->tm_hour;
    m=currentTime->tm_min;
    s=currentTime->tm_sec;
glPushMatrix();
    glColor3f(0, 0, 1);
glRotatef(90, 0.0, 1.0, 0.0);
glRotatef(-90, 1.0, 0.0, 0.0);
        glRotatef(s*6, 1.0, 0.0, 0.0);
glCallList(list+3);
glPopMatrix();
glPushMatrix();
    glColor3f(0, 0, 1);
glRotatef(90, 0.0, 1.0, 0.0);
glRotatef(-90, 1.0, 0.0, 0.0);
        glRotatef(m*6, 1.0, 0.0, 0.0);
glCallList(list+4);
glPopMatrix(); glPushMatrix();
    glColor3f(0, 0, 1);
glRotatef(90, 0.0, 1.0, 0.0);
glRotatef(-90, 1.0, 0.0, 0.0);
        glRotatef(n*30, 1.0, 0.0, 0.0);
glCallList(list+5);
glPopMatrix(); glFlush();
glutSwapBuffers();

}
void myReshape(int w, int h)
{
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 right_menu(int id)
{
  
}void spinCube(int id)
{
angle += 0.05;
if( angle > 360.0 ) angle -= 360.0;
display();
if (id==1) {glRotatef(angle, 0, 1, 0);}
if (id==2) {glRotatef(angle, 1, 0, 0);}}void mouse(int btn, int state, int x, int y)
{/* mouse callback, selects an axis about which to rotate */ if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) {spinCube(1);}
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {spinCube(2);}

}void rotation_menu(int id)
{ if(id == 1 ) {display();}
if(id == 2 ) {display(); glRotatef(45, 0, 1, 0);}
if(id == 3 ) {display(); glRotatef(90, 0, 1, 0);}}
void main(int argc, char **argv)
{
int r_menu;
glutInit(&argc, argv);
/* need both double buffering and z buffer */
glutInitDisplayMode(GLUT_DOUBLE  | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Clock Demo"); 
init();
clockComponents();
glutReshapeFunc(myReshape);
glutMouseFunc(mouse);
glutDisplayFunc(display);    //r_menu = glutCreateMenu(rotation_menu); //rotate submenu
//glutAddMenuEntry("reset",1);
    //glutAddMenuEntry("Upward continuously",2);
//glutAddMenuEntry("domnward continuously",3);   // glutCreateMenu(right_menu);               //main menu
   // glutAddSubMenu("Rotate", r_menu);
   // glutAttachMenu(GLUT_RIGHT_BUTTON); glEnable(GL_DEPTH_TEST); /* Enable hidden-surface-removal */
glutMainLoop();} 

解决方案 »

  1.   

    gluQuadricTexture
    The gluQuadricTexture function specifies whether quadrics are to be textured.void gluQuadricTexture(
      GLUquadricObj * quadObject,   
      GLboolean textureCoords       
    );
     
    Parameters
    quadObject 
    The quadric object (created with gluNewQuadric). 
    textureCoords 
    A flag indicating whether texture coordinates are to be generated. The following values are valid. Value Meaning 
    GL_TRUE Generate texture coordinates. 
    GL_FALSE Do not generate texture coordinates. This is the default value. 
    Res
    The gluQuadricTexture function specifies whether texture coordinates are to be generated for quadrics rendered with quadObject. The manner in which texture coordinates are generated depends upon the specific quadric rendered.QuickInfo
      Windows NT: Use version 3.5 and later.
      Windows: Use Windows 95 and later.
      Windows CE: Unsupported.
      Header: Declared in glu.h.
      Import Library: Link with glu32.lib.See Also
    gluNewQuadric, gluQuadricDrawStyle, gluQuadricNormals