绘制钟表(2)

#include <GL\glut.h>
#include <math.h>
#include <stdio.h>
#include <time.h>

void init()
{
	glClearColor (0.5, 0.5, 0.5, 0.0);
	gluOrtho2D(-100,100,-100,100);
}

//刻度
void createScale1()
{

}
void createScale2()
{

}

//时针
void createHourhand(float angle)
{
	glRotatef(angle,0,0,1.0);
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	glColor3f(1.0,1.0,1.0);
	glBegin(GL_POLYGON);
		glVertex3f(-10,0,0);
		glVertex3f(0,4,0);
		glVertex3f(25,0,0);
		glVertex3f(0,-4,0);
	glEnd();
	glRotatef(-angle,0,0,1.0);
}
//分针
void createMinutehand(float angle)
{
	glRotatef(angle,0,0,1.0);
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	glColor3f(1.0,1.0,1.0);
	glBegin(GL_POLYGON);
		glVertex3f(-10,0,0);
		glVertex3f(0,3,0);
		glVertex3f(30,0,0);
		glVertex3f(0,-3,0);
	glEnd();
	glRotatef(-angle,0,0,1.0);
}
//秒针
void createSecondhand(float angle)
{
	//printf("adfadf");
	glRotatef(angle,0,0,1.0);
	glColor3f(1.0,0.0,0.0);
	glLineWidth(2.0);
	glBegin(GL_LINES);
		glVertex3f(-6,0,0);
		glVertex3f(35,0,0);
	glEnd();
	glRotatef(-angle,0,0,1.0);
}

int kedu_x = 10,kedu_y=2;//表盘上8个长方形刻度相关参数

void createDial_kedu(float angle,float offset_x,float offset_y)
{
	//glClear(GL_COLOR_BUFFER_BIT);
	glRotatef(angle,0.0,0.0,1.0);
	glTranslatef(offset_x-kedu_x/2,offset_y,0);
	glBegin(GL_POLYGON);
	glVertex3f(kedu_x/2,kedu_y/2,0);
	glVertex3f(-kedu_x/2,kedu_y/2,0);
	glVertex3f(-kedu_x/2,-kedu_y/2,0);
	glVertex3f(kedu_x/2,-kedu_y/2,0);
	glEnd();
	glTranslatef(-offset_x+kedu_x/2,-offset_y,0);
	glRotatef(-angle,0.0,0.0,1.0);
}

//创建表盘
int D = 50;
float PI = 3.14159;
void createDial()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.7,0.7,0.7);
	int i = 0;
	glBegin(GL_POLYGON);
	for(;i<24;i++)
	{
		glVertex3f(D*cos(i*PI/12.0),D*sin(i*PI/12.0),0);
	}
	glEnd();
	
	glLineWidth(3.0);
	glColor3f(0.0,0.0,0.0);
	glBegin(GL_LINE_LOOP);
	for(i=0;i<24;i++)
	{
		glVertex3f(D*cos(i*PI/12.0),D*sin(i*PI/12.0),0);
	}
	glEnd();
	glLineWidth(1.0);
	glColor3f(0.0,0.0,0.0);
	glPointSize(8.0);
	glBegin(GL_POINTS);
		glVertex3f(0,D-2.5,0);
		glVertex3f(0,-D+2.5,0);
		glVertex3f(D-2.5,0,0);
		glVertex3f(-D+2.5,0,0);
	glEnd();
	glPointSize(1.0);
	glBegin(GL_LINES);
		glVertex3f(-D,0,0);
		glVertex3f(D,0,0);
		glVertex3f(0,D,0);
		glVertex3f(0,-D,0);
	glEnd();
	createDial_kedu(30,D,0);
	createDial_kedu(60,D,0);
	createDial_kedu(120,D,0);
	createDial_kedu(150,D,0);
	createDial_kedu(-30,D,0);
	createDial_kedu(-60,D,0);
	createDial_kedu(-120,D,0);
	createDial_kedu(-150,D,0);
	//createDial_kedu();
}
//建模变换,组装构件
void createWatch(float hour,float minute,float second)
{
	createDial();
	glRotatef(90,0.0,0.0,1.0);
	createHourhand(-hour/12*360-minute/60*30);
	createMinutehand(-minute/60*360);
	createSecondhand(-second/60*360);
	glRotatef(-90,0.0,0.0,1.0);
	
	//下面代码为画表盘中间的灰色园
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	glColor3f(0.5,0.5,0.5);
	glBegin(GL_POLYGON);
		for(int i=0;i<12;i++)
			glVertex3f(3*cos(PI/6*i),3*sin(PI/6*i),0);
	glEnd();
}

GLuint Hour,Minute,Second;

//控制钟表时针、分针和秒针的运动
void processWatch()
{
	struct tm* localTime;
	time_t curTime;
	time(&curTime);
	localTime = localtime(&curTime);
	if(Second != localTime->tm_sec)
	{
		Hour=localTime->tm_hour;
		Minute=localTime->tm_min;
		Second=localTime->tm_sec;
		glutPostRedisplay();
	}
}

void display()
{
	//createWatch(1,2,40);
	createWatch(Hour,Minute,Second);
	glFlush();
}

void timerProc(int id)
{
	processWatch();
	glutTimerFunc(1000,timerProc,1);
}

int main(int argc,char ** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

	glutInitWindowSize (500, 500);
	glutInitWindowPosition (100, 100);
	glutCreateWindow (argv[0]);
	processWatch();
	glutDisplayFunc(display);
	//glutIdleFunc(processWatch);
	glutTimerFunc(1000,timerProc,1);
	processWatch();

	init();
	glutMainLoop();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值