OpenGL draws a simple clock (first test) and opengl draws

Source: Internet
Author: User

OpenGL draws a simple clock (first test) and opengl draws

Editor vs2012
1 # include <windows. h> 2 # include <GL/glut. h> // originally, OpenGL programs generally need to include <GL/gl. h> and <GL/glu. h>, 3 // but the GLUT header file automatically contains the two files and does not need to be included again. 4 # include <math. h> 5 # include <time. h> 6 7 const GLfloat Pi = 3.1415926536; 8 const GLfloat R = 0.8f; 9 const int n = 200; 10 static GLfloat angle = 2 * Pi; 11 12 float Mysecond (struct tm * ptr) 13 {14 return (Pi/2)-(float) ptr-> tm_sec)/60) * 2 * Pi ); 15 16} 17 18 float Mymin (struct tm * ptr) 19 {20 // return (Pi/2)-(ptr-> tm_min + (float) ptr-> tm_sec)/60)/60) * 2 * Pi); 21 return (Pi/2)-(ptr-> tm_min + (Myseco Nd (ptr)/60)/60) * 2 * Pi); 22}; 23 24 float Myhour (struct tm * ptr) 25 {26 if (0 <ptr-> tm_hour & ptr-> tm_hour <12) 27 {28 return (Pi/2)-(float) ptr-> tm_hour + Mymin (ptr)/60.0)/12.0*2 * Pi); 29} else {30 return (Pi/2) -(ptr-> tm_hour-12.0 + Mymin (ptr)/60.0)/12) * 2 * Pi); 31} 32}; 33 void myDisplay (void) 34 35 {36 // glShadeModel (GL_SMOOTH); 37 // obtain the system time 38 struct tm * ptr; 39 time_t it; 40 it = time (NULL); 41 ptr = Localtime (& it); 42 43 glClear (GL_COLOR_BUFFER_BIT); // clear, period indicates that the color is 44 glEnable (GL_POINT_SMOOTH); 45 glable (GL_LINE_SMOOTH); 46 glHint (limit, GL_NICEST); // Make round points, not square points 47 glHint (random, GL_NICEST); // Antialias the lines 48 glable (GL_BLEND); 49 glBlendFunc (GL_SRC_ALPHA, random ); 50 // 51 glColor3f (0.5, 0.5, 0. 5); 52 glBegin (GL_POLYGON); 53 for (int I = 0; I <n; I ++) {54 glVertex2f (R * cos (2 * Pi/n * I), R * sin (2 * Pi/n * I); 55} 56 glEnd (); 57 58 // scale 59 glColor3f (1.0, 1.0, 1.0); 60 glBegin (GL_POINTS); 61 glPointSize (5.0f); 62 for (int j = 0; j <12; j ++) 63 {64 glVertex2f (0.75 * cos (2 * Pi/12 * j), 0.75 * sin (2 * Pi/12 * j )); 65 // for (int k = 66} 67 68 glEnd (); 69 // center 70 glPointSize (5.0f); 71 glColor3f (0.0, 0.0, 0.0); 72 glBegin (GL_POIN TS); 73 glVertex2f (0.0, 0.0); 74 glEnd (); 75 76 // 77 glLineWidth (5.0f); 78 glColor3f (0.0, 0.0, 0.0 ); // red 79 // glRotatef (angle/3600.0), 0.0, 0.0, 1.0); 80 81 glBegin (GL_LINES); 82 glRotatef (angle/3600.0), 0.0, 0.0, 1.0); 83 glVertex2f (0.0, 0.0); 84 glVertex2f (cos (Myhour (ptr) * R * 0.55, sin (Myhour (ptr )) * R * 0.55); 85 glEnd (); 86 87 // split needle 88 glLineWidth (5.0f); 89 glColor3f (0.0, 0.0, 0.0 ); // Green 90 // glRotatef (angl E/60.0), 0.0, 0.0, 1.0); 91 92 glBegin (GL_LINES); 93 glRotatef (angle/60.0), 0.0, 0.0, 1.0); 94 glVertex2f (0.0, 0.0); 95 glVertex2f (cos (Mymin (ptr) * R * 0.65, sin (Mymin (ptr) * R * 0.65); 96 glEnd (); 97 98 // seconds 99 glLineWidth (3.0f); 100 glColor3f (0.0, 0.0, 0.0); // blue 101 // glRotatef (angle, 0.0, 0.0, 1.0 ); 102 103 glBegin (GL_LINES); 104 glRotatef (angle, 0.0, 0.0, 1.0); 105 glVertex2f (0.0, 0.0); 106 glVertex2f (cos (Mysecond (ptr ))* R * 0.85, sin (Mysecond (ptr) * R * 0.85); 107 glEnd (); 108 glFlush (); // glFlush, ensure that the previous OpenGL commands are executed immediately (instead of waiting them in the buffer ). 109 110} 111 112 void myIdle (void) 113 {114 angle-= (2 * Pi)/60); 115 Sleep (1000); 116 if (angle <0.0f) {117 angle = 2 * Pi; 118} 119 myDisplay (); 120} 121 122 int main (int argc, char * argv []) 123 124 {125 126 gluinit (& argc, argv); // gluinit, which is called once before other gluts are used. The format is relatively rigid. You can copy this keyword according to the format of gluinit (& argc, argv. 127 128 gluinitdisplaymode (glu_rgb | glu_single); // you can specify the display mode. In this case, glu_rgb indicates that the RGB color is used, and the corresponding glu_index indicates that the index color is used. Glu_single indicates that a single buffer is used, and the corresponding value is glu_double (dual buffer) 129 130 gluinitwindowposition (300,100); // you can specify the position of the window on the screen. 131 132 glutInitWindowSize (400,400); // you can specify the window size as 133. 134. The parameter is used as the title of the window. Note: The window is not displayed on the screen immediately after it is created. You need to call the glumainloop function to view the window. 135 136 gludisplayfunc (& myDisplay); // call the plotting function 137 glutIdleFunc (& myIdle); 138 139 glumainloop (); // perform a message loop. This function can display the window, and will return 140 141 return 0; 142 143} only after the window is closed}

Effect
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.