#include<stdlib.h> #include<glut.h> #include<gl\GLU.h> #include<gl\GL.h> #include<math.h> #include<stdio.h> #include<windows.h> float tite = 0.0f; const float PI = 3.14159f; const int W_width = 500; const int W_height = 500; float xc, yc, R, n; float ss, mm, hh; const int S_width = glutGet(GLUT_SCREEN_WIDTH); const int S_height = glutGet(GLUT_SCREEN_HEIGHT); //system void init(); void draw(); void reShape(int ww, int hh); //colck void drawCircle(); void myIdle(); void drawline(); void TimerFunc(int value); void arith_foot(); void drawshortLine(); void TimerFunc(int value); void init(){ glClear(GL_COLOR_BUFFER_BIT); glClearColor(1.0f, 0.5f, 0.8f, 1.0); //colck Afraid of SYSTEMTIME; GetLocalTime(&Timetite); xc = 0.0f, yc = 0.0f, R = 100.0f, n = 20.0f, ss = Timetite.wSecond, mm = Timetite.wMinute, hh = Timetite.wHour; } int main(int agrs, char **args) { glutInit(&agrs, args); init(); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(W_width, W_height); glutInitWindowPosition((S_width - W_width) / 2, (S_height - W_width) / 2); glutCreateWindow("openGL"); //Set the viewport window glClearColor(0.6f, 0.4f, 0.3f, 1.0f); //Code to clear window color glutDisplayFunc(draw); glutReshapeFunc(reShape); glutMainLoop(); return 0; } void draw(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); drawCircle(); arith_foot (); drawshortLine(); // TimerFunc (10); glutTimerFunc(1000, TimerFunc, 10); //drawline(); glutSwapBuffers(); //shapes } void reShape(int ww, int hh) { glViewport(0, 0, ww, hh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (hh == 0) hh = 1; if (ww >= hh) { gluOrtho2D(-500, 500 * (float)ww / hh, -500, 500); } else { gluOrtho2D(-500, 500, -500, 500 * (float)hh / ww); } glMatrixMode(GL_MODELVIEW); } //Using the drawing of basic primitives to realize the function of simple animation (clock) //draw circle void drawCircle() { glBegin(GL_LINE_LOOP); for (int i = 0; i < n; i++) { glVertex2d(xc + R * cos(i * 2 * PI / n), yc + R * sin(i * 2 * PI / n)); } glEnd(); }//Draw the clock circle //Line test void drawline() { glBegin(GL_LINES); glVertex2d(xc, yc); glVertex2d(R*cos(tite), R*sin(tite)); glEnd(); } // idle function void myIdle(void) { small += 0.002f; if (tite >= 2 * PI) { tite = 0.0f; } glutPostRedisplay(); } void TimerFunc(int value) {//Update the value of hours, minutes and seconds int carry1 = 0; ss += 1; if (ss >= 60) { ss = 0; carry1 = 1; } mm += carry1; int carry2 = 0; if (mm >= 60) { carry2 = 1; mm = 0; } hh += carry2; if (hh == 24) hh = 0; glutPostRedisplay();//(callback function) returns the draw method to update the scale values of xs, xm, xh according to ss, mm, hh } //draw hh,ss,mm lines (draw the tick marks of the hour and minute hands) void drawshortLine(){ int lines = 60; for (int i = 0; i < lines;i++){ if (i % 5 == 0){//Hour hand glLineWidth(5); glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINES); glVertex2f(xc + (R-10)*sin(2*PI*i/lines),yc+(R-10)*cos(2*PI*i/lines)); glVertex2f(xc + R*sin(2 * PI*i / lines), yc + R*cos(2 * PI*i / lines)); glEnd(); } else{ glLineWidth(2); glColor3f(0.0,0.0,1.0); glBegin(GL_LINES); glVertex2f(xc+(R-5)*sin(2*PI*i/lines),yc+(R-5)*cos(2*PI*i/lines)); glVertex2f(xc + R*sin(2 * PI*i / lines), yc + R*cos(2 * PI*i / lines)); glEnd(); } } } //arith foot colck (each time the draw method is called, the value of the hour, minute and second pointer will be recalculated according to the different ss, mm, hh at this time) void arith_foot(){ float xs = 0.0f, ys=0.0f, xm=0.0f, ym=0.0f, xh=0.0f, yh=0.0f; glLineWidth(1); xs = xc + (R-10)*sin(ss*2*PI/60);//According to the ratio method, calculate the angle between the second hand and the y-axis to calculate the x end position and the y end position ys = yc + (R - 10)*cos(ss * 2 * PI / 60); glColor3f(1.0f,0.0f,0.0f); glBegin(GL_LINES); glVertex2f(xc,yc); glVertex2f(xs,ys); glEnd(); glLineWidth(3); xm = xm + (R - 25)*sin((mm + ss / 60) * 2 * PI / 60);//The second hand and the minute hand are both 60 copies ym = ym + (R - 25)*cos((mm + ss / 60) * 2 * PI / 60); glColor3f(0.0f, 1.0f, 0.0f); glBegin(GL_LINES); glVertex2f(xc,yc); glVertex2f(xm,ym); glEnd(); glLineWidth(5); xh = xh + (R - 45)*sin((hh + (mm + ss / 60) / 60) * 2 * PI / 12);//The angle of each walk is divided into 12 parts yh = yh + (R - 45)*cos((hh + (mm + ss / 60) / 60) * 2 * PI / 12); glColor3f(0.0f,0.0f,1.0f); glBegin(GL_LINES); glVertex2f(xc,yc); glVertex2f(xh,yh); glEnd(); } //draw a lots of shapes void Shapes(){ //draw points glPointSize(10); glBegin(GL_POINT); //glVertex2d(); //glVertex2d(); //glVertex2d(); glEnd(); }
Do a clock animation with openGL
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324779221&siteId=291194637
Recommended
Ranking