1. 程式人生 > >OpenGl 畫一個專屬的鐘表

OpenGl 畫一個專屬的鐘表

效果顯示:

#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)<