// lab-basis.cpp : 定义应用程序的入口点。
#include "stdafx.h"
#include "lab22-basis.h"
#define PI 3.14159
int n = 36, r = 8, R = 10;
// time variable
SYSTEMTIME timeNow;
float hh, mm, ss;
void init(); // 获取系统时间
void Keyboard(unsigned char key,int x, int y);
void Display(void); // 回显函数
void Reshape(int w, int h); // 窗口改变函数
void myidle(int t); // 闲时函数
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
char* argv[] = {"hello ", " "};
int argc = 2;
glutInit(&argc,argv);
glutInitWindowSize(400,400);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("A Rotating Square");
init();
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutTimerFunc(1000,myidle,10);
glutMainLoop();
return 0;
}
void init()
{
GetLocalTime(&timeNow);
hh = timeNow.wHour;
mm = timeNow.wMinute;
ss = timeNow.wSecond;
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
// 画圆
glColor3f(1,1,1);
glBegin(GL_POLYGON);
for (int i = 0; i < n; i++)
{
float f = i*2*PI/n;
glVertex2f(R*cos(f),R*sin(f));
}
glEnd();
// xs ,ys 秒针坐标
// xm , ym 分针坐标
// xh , yh 时针坐标
float xs = r*cos(PI/2.0 - ss/60 * 2.0 * PI);
float ys = r*sin(PI/2.0 - ss/60 * 2.0 * PI);
float xm = (r-1)*cos(PI/2.0 - (mm + ss/60.0)/60.0 * 2.0 * PI);
float ym = (r-1)*sin(PI/2.0 - (mm + ss/60.0)/60.0 * 2.0 * PI);
float xh = (r-3)*cos(PI/2.0 - (hh + (mm + ss/60.0)/60.0)/12.0 * 2.0 * PI);
float yh = (r-3)*sin(PI/2.0 - (hh + (mm + ss/60.0)/60.0)/12.0 * 2.0 * PI);
// 画线
glColor3f(0,0,0);
glBegin(GL_LINES);
glVertex2f(0,0);
glVertex2f(xs,ys);
glEnd();
glColor3f(0,0,0);
glLineWidth(2);
glBegin(GL_LINES);
glVertex2f(0,0);
glVertex2f(xm,ym);
glEnd();
glColor3f(0,0,0);
glLineWidth(3);
glBegin(GL_LINES);
glVertex2f(0,0);
glVertex2f(xh,yh);
glEnd();
glutSwapBuffers();
}
void myidle(int t)
{
GetLocalTime(&timeNow);
hh = timeNow.wHour;
mm = timeNow.wMinute;
ss = timeNow.wSecond;
glutPostRedisplay();
glutTimerFunc(1000,myidle,10);
}
void Reshape(int w,int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.5*R*w/h,1.5*R*w/h,-1.5*R,1.5*R);
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
}
效果图: