OpenGl 画一个专属的钟表

版权声明:将来的你一定会感谢现在努力的你!!!! https://blog.csdn.net/qq_37383726/article/details/83030704

效果显示:

#include <bits/stdc++.h>
#include <windows.h>
#include <time.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glext.h>
#define GLUT_DISABLE_ATEXIT_HACK
#define MAX_CHAR 128
using namespace std;

void drawString(const char* str) {
	static int isFirstCall = 1;
	static GLuint lists;
	if (isFirstCall)
	{ // 如果是第一次调用,执行初始化
	  // 为每一个ASCII字符产生一个显示列表
		isFirstCall = 0;
		// 申请MAX_CHAR个连续的显示列表编号
		lists = glGenLists(MAX_CHAR);
		// 把每个字符的绘制命令都装到对应的显示列表中
		wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, lists);
	}
	// 调用每个字符对应的显示列表,绘制每个字符
	for (; *str != '\0'; ++str)
		glCallList(lists + *str);
}
void drawCNString(const char* str) {
	int len, i;
	wchar_t* wstring;
	HDC hDC = wglGetCurrentDC();
	GLuint list = glGenLists(1);
	// 计算字符的个数
	// 如果是双字节字符的(比如中文字符),两个字节才算一个字符
	// 否则一个字节算一个字符
	len = 0;
	for (i = 0; str[i] != '\0'; ++i)
	{
		if (IsDBCSLeadByte(str[i]))
			++i;
		++len;
	}

	// 将混合字符转化为宽字符
	wstring = (wchar_t*)malloc((len + 1) * sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstring, len);
	wstring[len] = L'\0';

	// 逐个输出字符
	for (i = 0; i < len; ++i)
	{
		wglUseFontBitmapsW(hDC, wstring[i], 1, list);
		glCallList(list);
	}

	// 回收所有临时资源
	free(wstring);
	glDeleteLists(list, 1);
}

void selectFont(int size, int charset, const char* face) {
	HFONT hFont = CreateFontA(size, 0, 0, 0, FW_MEDIUM, 0, 0, 0,
		charset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, face);
	HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(), hFont);
	DeleteObject(hOldFont);
}

/*
void display(void) {
	glClear(GL_COLOR_BUFFER_BIT);

	selectFont(48, ANSI_CHARSET, "Comic Sans MS");
	glColor3f(1.0f, 0.0f, 0.0f);
	glRasterPos2f(-0.7f, 0.9f); // 字体显示的位置
	drawString("Hello, World!");

	selectFont(48, GB2312_CHARSET, "华文中宋");
	glColor3f(1.0f, 1.0f, 0.0f);
	glRasterPos2f(-0.7f, 0.2f);
	drawCNString("当代的中国汉字");

	selectFont(48, GB2312_CHARSET, "黑体_GB2312");
	//selectFont(48, DEFAULT_CHARSET, "华文仿宋");
	glColor3f(0.0f, 1.0f, 0.0f);
	glRasterPos2f(-0.7f, 0.0f);
	drawCNString("傳統的中國漢字");

	selectFont(48, GB2312_CHARSET, "华文行楷");
	//selectFont(48, DEFAULT_CHARSET, "华文仿宋");
	glColor3f(0.0f, 1.0f, 1.0f);
	glRasterPos2f(-0.7f, -0.3f);
	drawCNString("w不是传统的中国汉字");

	glutSwapBuffers();
}
*/

int winheight = 1024, winwidth = 768;
int cx = 512, cy = 400, radius = 320;
int hradius = 160, mradius = 250, sradius = 300;
float sangle, mangle, hangle;
char timebuffer[9];
char luo[12][10] = { "III", "II", "I","XII", "XI", "X", "IX","VIII","VII", "VI","V", "IV"};
char luoma[13][10] = {"Ⅲ", "Ⅱ", "Ⅰ","Ⅻ", "Ⅺ", "Ⅹ", "Ⅸ", "Ⅷ","Ⅶ", "Ⅵ","Ⅴ", "Ⅳ"};
char luom[13][10] = { "X11","1", "11", "111", "1V", "V", "V1", "V11", "V111", "1X", "X", "X1" };
int second, miniter, hour;
void dotclock(int cx, int cy, int radius);
void clock_print();
void hand(int cx, int cy, int r, float thleta);
void look(int cx, int cy);
void plotC(int x, int y);
void Bresenham_Circle_Algorithm(int cx, int cy, int bradius);

void init(void)
{
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glClearColor(1.0, 1.0, 1.0, 1.0);
	//glClearColor(0.0, 0.0, 0.0, 0.0);
	//strftime(timebuffer, sizeof(timebuffer));
}
void myidle()
{
	//::Sleep(1000);
	time_t t = time(0);
	strftime( timebuffer, sizeof(timebuffer), "%X",localtime(&t) );
 	//for (int i = 0; i < 9; i++) cout<<timebuffer[i];cout<<endl;
 	
	hour = ((int)timebuffer[0] - 48) * 10 + ((int)timebuffer[1] - 48);
	miniter = ((int)timebuffer[3] - 48) * 10 + ((int)timebuffer[4] - 48);
	second = ((int)timebuffer[6] - 48) * 10 + ((int)timebuffer[7] - 48);
	//cout<<hour<<":"<<miniter<<":"<<second<<endl;
	sangle = (float)(3.1415 / 2 - (float)second * 2 * 3.1415 / 60);  //秒钟走的角度
	mangle = (float)3.1415 / 2 - (float)(miniter + (float)second / 60) / 60 * 2 * 3.1415;  //分钟走的角度
	hangle = (float)3.1415 / 2 - (float)(hour + (float)(miniter + (float)second / 60) / 60) * 2 * 3.1415 / 12;  //时钟走的角度
	//cout<<hangle<<":"<<mangle<<":"<<sangle<<endl;
	glutPostRedisplay();
}
int rr;
void hand(int cx, int cy, int r, float thleta)
{
	float harr[2];
	harr[0] = cx + r * cos(thleta);
	harr[1] = cy + r * sin(thleta);

	double PI = acos(-1.0);
	glBegin(GL_LINES);
	glLineWidth(10); glVertex2f(harr[0], harr[1]); glVertex2f(cx, cy);
	glEnd();

	glBegin(GL_LINES);
	glLineWidth(10);  glVertex2f(cx, cy); glVertex2f(cx + rr * cos(thleta + PI), cy + rr * sin(thleta + PI));
	glEnd();
	rr += 5;
}
void display(void)  // 主函数
{
	glClear(GL_COLOR_BUFFER_BIT);

	// 钟表
	glPointSize(2.0);
	//glColor3f(1.0, 0.0, 1.0);
	glRasterPos2i(20, 20);
	clock_print(); // 钟表中的文字
	dotclock(cx, cy, radius);

	glColor3f(0, 0, 0);
	Bresenham_Circle_Algorithm(cx, cy, 345);
	Bresenham_Circle_Algorithm(cx, cy, 295);

	glColor3f(0, 0 ,0.0);
	rr = 15;
	//dotclock(cx, cy, radius + 10);
	hand(cx, cy, sradius - 20, sangle);
	glColor3f(0.0, 0.0, 0.0);
	//glPointSize(4.0);
	hand(cx, cy, mradius - 30, mangle);
	glColor3f(0.0, 0.0, 0.0);
	//glPointSize(8.0);
	hand(cx, cy, hradius - 5, hangle);

	// 数字格式显示当前时间
	glLineWidth(3);
	glColor3f(0.0f, 0.0f, 0.0f);
	glBegin(GL_POLYGON);// GL_POLYGON制定绘制的是多边形
	glBegin(GL_LINES);
		glVertex2i(430, 220); glVertex2i(585, 220);
	glEnd();
	glBegin(GL_LINES);
		glVertex2i(430, 220); glVertex2i(430, 265);
	glEnd();
	glBegin(GL_LINES);
		glVertex2i(430, 265); glVertex2i(585, 265);
	glEnd();
	glBegin(GL_LINES);
		glVertex2i(585, 265); glVertex2i(585, 220);
	glEnd();

	//指定顶点,X=0.25 Y=0.25 Z=0.0

	selectFont(48, ANSI_CHARSET, "Comic Sans MS");
	glColor3f(1.0f, 0.0f, 1.0f);
	glRasterPos2i(440, 230); // 字体显示的位置
	drawString(timebuffer);

	glutSwapBuffers();
}
void print(int id, double x, double y){
	//cout << id << "\n";
	glLineWidth(5);
	/*selectFont(5, ANSI_CHARSET, "Comic Sans MS");
	glColor3f(1.0f, 0.0f, 0.0f);
	glRasterPos2f(x, y); // 字体显示的位置
	drawString(luoma[id]);
	*/
	selectFont(30, GB2312_CHARSET, "华文行楷");
	//selectFont(48, DEFAULT_CHARSET, "华文仿宋");
	glColor3f(1.0f, 0.0f, 0.0f);
	glRasterPos2f(x, y);
	drawCNString(luoma[id]);
}
void clock_print( )
{
	selectFont(48, ANSI_CHARSET, "Comic Sans MS");
	glColor3f(1.0f, 0.0f, 1.0f);
	glRasterPos2i(300, 500); // 字体显示的位置
	drawString("Henan Polytechnic University");
}
void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, (GLdouble)w, 0.0, (GLdouble)h);
	glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
	}
}
void dotclock(int cx, int cy, int radius)
{
	int array[13][2];
	int arr[60][2];
	float angle, angle2;
	angle = 2 * 3.1415 / 12;
	angle2 = 2 * 3.1415 / 60;
	for (int j = 0; j < 60; j++)
	{
		arr[j][0] = cx + radius * cos(j*angle2);
		arr[j][1] = cy + radius * sin(j*angle2);
		glBegin(GL_POINTS);
		glPointSize(2.0);
		glVertex2f(arr[j][0], arr[j][1]);
		glEnd();

	}
	print(0, cx + 320, cy);
	for (int i = 0; i < 12; i++)
	{
		array[i][0] = cx + radius * cos(i*angle);
		array[i][1] = cy + radius * sin(i*angle);
		//if (i == 0) cout << array[i][0] << " " << array[i][1] <<"\n";

		/*
		glPointSize(8.0);
		glBegin(GL_POINTS);
		glVertex2f(array[i][0], array[i][1]);
		glEnd();
		*/
		int dis = 10;
		print(i, array[i][0] - dis, array[i][1] - dis);
	}
}
void plotC(int x, int y, int cx, int cy)
{
	glPointSize(3.0);
	glColor3f(0.0, 0.0, 0.0);
	glBegin(GL_POINTS);
	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, int bradius)
{
	int x, y, d;
	y = bradius;
	d = 3 - 2 * bradius;
	//glPointSize(4.0);
	glColor3f(0.0, 0.0,0.0);
	x = 0;
	while (x <= y)
	{
		plotC(x, y, cx, cy);
		if (d < 0)
			d += 4 * x + 6;
		else
		{
			d += 4 * (x - y) + 10;
			y = y - 1;
		}
		x = x + 1;
	}
}
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(winheight, winwidth);
	glutInitWindowPosition(0, 0);
	glutCreateWindow("My clock!~");
	init();
	glutDisplayFunc(display);
	glutIdleFunc(myidle);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37383726/article/details/83030704