openGL时钟绘制

#include "stdafx.h"
#include
#include"glut.h"
#include"gl.h"
#include
#include

int width = 500;
int height = 500;
int radius = 100;
float sangle, mangle, hangle;
char timebuffer[9];
int second, minute, hour;
 
void setTime();
void drawCircle();
void plotC(int x, int y, int cx, int cy, GLfloat *color);
void Bresenham_Circle_Algorithm(int cx, int cy, GLfloat *color);
void drawPoint();
void drawHour();
void drawMinute();
void drawSecond();


void display()
{
glClear(GL_COLOR_BUFFER_BIT);//清除颜色缓存

setTime();
drawCircle();
drawPoint();
drawHour();
drawMinute();
drawSecond();
glutSwapBuffers();//交换缓存
}


void timer(int value)
{
setTime();
glutPostRedisplay();
glutTimerFunc(500, timer, 1);

}


void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if (w <= h)
gluOrtho2D(-radius,radius,-radius*h/w,radius*h/w);
else
gluOrtho2D(-radius*w / h, radius*w / h, -radius, radius);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}


void setTime()
{
_strtime(timebuffer);//调用系统时间
hour = ((int)(timebuffer[0] - '0') * 10 + (int)(timebuffer[1] - '0'));
minute = ((int)(timebuffer[3] - '0') * 10 + (int)(timebuffer[4] - '0'));
second = ((int)(timebuffer[6] - '0') * 10 + (int)(timebuffer[7] - '0'));

sangle = (float)(90 - second * 6);
mangle = (float)(90 - minute * 6);
hangle = (float)(90 - (hour%12) * 30);
}
void drawCircle()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();//重绘
GLfloat color[] = { 1.0, 1.0, 1.0 };
glPointSize(2);
Bresenham_Circle_Algorithm(0, 0, color);
glPointSize(1);

}
void plotC(int x, int y, int cx, int cy, GLfloat *color)
{
glColor3fv(color);
glBegin(GL_POINT);
glVertex2f(cx + x, cy + y);
glVertex2f(cx + x, cy - y);
glVertex2f(cx - x, cy + y);
glVertex2f(cx - x, cy - y);

glVertex2f(cx + y, cy + x);
glVertex2f(cx + y, cy - x);
glVertex2f(cx - y, cy + x);
glVertex2f(cx - y, cy - x);
glEnd();

}
void Bresenham_Circle_Algorithm(int cx, int cy, GLfloat *color)
{
    int x, y, d;
y = radius;
d = 1 - radius;
x = 0;
while (x <= y)
{
plotC(x, y, cx, cy, color);
if (d < 0)
d += 2 * x + 3;
else
{
d += 2 * (x + 8) + 5;
y = y - 1;
}
x = x + 1;
}


}
void drawPoint()
{
glBegin(GL_QUADS);
glColor3f(1.0, 0.6, 0.0);
glVertex2f(-1, radius);
glVertex2f(1, radius);
glVertex2f(1, radius-5);
glVertex2f(-1, radius-5);
 
glVertex2f(-1, -(radius));
glVertex2f(1, -(radius));
glVertex2f(1, -(radius - 5));
glVertex2f(-1, -(radius - 5));

glVertex2f(radius, -1);
glVertex2f(radius, 1);
glVertex2f(radius - 5, 1);
glVertex2f(radius - 5, -1);

glVertex2f(-(radius) ,- 1);
glVertex2f(-(radius),1);
glVertex2f(-(radius - 5),1);
glVertex2f(-(radius - 5) ,- 1);

glEnd();

}
void drawHour()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glRotatef(hangle, 0, 0, 1);
glTranslatef(-10, 0.0, 0.0);

glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex2f(0.0, 2.0);
glVertex2f(0.0, -2.0);
glVertex2f(radius/3*2,0.0);
glEnd();
glPopMatrix();

}
void drawMinute()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glRotatef(mangle, 0, 0, 1);
glTranslatef(-10, 0.0, 0.0);


glColor3f(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex2f(0.0, 1.5);
glVertex2f(0.0, -1.5);
glVertex2f(radius / 5* 4, 0.0);
glEnd();
glPopMatrix();


}
void drawSecond()
{

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glRotatef(sangle, 0, 0, 1.0);

glTranslatef(-10, 0.0, 0.0);

glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2f(0.0, 1.0);
glVertex2f(0.0, -1.0);
glVertex2f(radius / 3 * 2, 0.0);
glEnd();
glPopMatrix();

}
int _tmain(int argc, _TCHAR* argv[])
{

glutInit(&argc, (char**)argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("时钟绘制");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(500, timer, 1);
glutMainLoop();//进入循环
return 0;
}

你可能感兴趣的:(openGL时钟绘制)