#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <math.h>
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Current Time
static GLfloat h = 0; //小时
static GLfloat m = 0; //分钟
static GLfloat s= 0; //秒
// Light values and coordinates
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//
#define PI 3.141526
#define N 1000
#define R 1
///////////////////////////////////////////////////////////////////////////////
// Reset flags as appropriate in response to menu selections
void DrawBase()
{
int i;
float ang = PI * 2 / N;
glColor3f(1.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
for(i=0; i<N;i++)
glVertex2f( R * cos(i* ang), R * sin(i * ang));
glEnd();
// 几个时间
glBegin(GL_LINES);
glVertex2f( 0,R * 0.9);glVertex2f( 0,R );
glVertex2f( R * 0.9,0);glVertex2f(R ,0 );
glVertex2f( 0,-R * 0.9);glVertex2f( 0,-R );
glVertex2f( -R * 0.9,0);glVertex2f(-R ,0 );
glEnd();
}
void DrawHour()
{
float ang = 360 - 360 * (h + m/60 + s/3600) / 12;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(0.0,0.0,1.0);
glBegin(GL_POLYGON);
glLineWidth(3.0f);
glVertex3f(0,0,0.2);
glVertex3f(-0.1,R*0.2,0.2);
glVertex3f(0,R*0.5,0.2);
// glVertex3f(0.1,R*0.2,0.2);
glEnd();
glPopMatrix();
}
void DrawMinute()
{
float ang = 360 - 360 * (m + s/60) / 60;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(1.0,0,0);
glBegin(GL_LINE_LOOP);
glLineWidth(3.0f);
glVertex3f(0,0,0.3);
glVertex3f(0,R*0.7,0.3);
glEnd();
glPopMatrix();
}
void DrawSecond()
{
float ang = 360 - 360 * s/60;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(0.0,1.0,0);
glBegin(GL_LINE_LOOP);
glLineWidth(3.0f);
glVertex3f(0,0,0.4);
glVertex3f(0,R*0.85,0.4);
glEnd();
glPopMatrix();
}
// Called to draw scene
void RenderScene(void)
{
// Clear the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
DrawHour();
// 表盘
DrawBase();
// 画秒针
DrawSecond();
// 分针
DrawMinute();
// 时针
glPopMatrix();
// Flush drawing commands
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
// Enable Depth Testing
glEnable(GL_DEPTH_TEST);
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a high shine
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT,GL_SHININESS,128);
// Set drawing color to blue
glColor3ub(0, 0, 255);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
//xRot += 5.0f;
h = h+1;
if(key == GLUT_KEY_LEFT)
// yRot -= 5.0f;
m = m+3;
if(key == GLUT_KEY_RIGHT)
//yRot += 5.0f;
s = s+5;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f };
GLfloat nRange = 1.9f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
}
int main(int argc, char* argv[])
{
int nSolidMenu;
int nWireMenu;
int nMainMenu;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("GLUT CLOCK");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
追问感觉好深奥啊 不过还是要谢谢你啊 谢谢