Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <GL/freeglut.h>
  2. #define _USE_MATH_DEFINES
  3. #include <math.h>
  4.  
  5. double WINDOW_HEIGHT = 500;
  6. double aspect_ratio = 4.0 / 3.0;
  7. double HEIGHT = 10.0;
  8. double WIDTH = HEIGHT * aspect_ratio;
  9.  
  10. void drawCircle(GLfloat x, GLfloat y, GLfloat r) {
  11. const size_t points = 30;
  12.  
  13. glBegin(GL_POLYGON);
  14. for (size_t i = 0; i < points; i++) {
  15. glVertex3f(x + r * cos(2 * i * M_PI / points), y + r * sin(2 * i * M_PI / points), 0.0);
  16. }
  17. glEnd();
  18. }
  19.  
  20. void drawPoint(GLfloat x, GLfloat y) {
  21. glBegin(GL_POINTS);
  22. glVertex3f(x, y, 0.0);
  23. glEnd();
  24. }
  25.  
  26. void drawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {
  27. glBegin(GL_LINES);
  28. glVertex3f(x1, y1, 0.0);
  29. glVertex3f(x2, y2, 0.0);
  30. glEnd();
  31. }
  32.  
  33. GLfloat sunR = 1.0;
  34. GLfloat sunX = WIDTH / 2;
  35. GLfloat sunY = HEIGHT * 0.9 - sunR;
  36. GLfloat sunCenterX = WIDTH / 2, sunCenterY = 0.0, sunRotationAngle = 0.05;
  37.  
  38. void drawSun() {
  39. glColor3f(1.0, 1.0, 0.0);
  40.  
  41. drawCircle(sunX, sunY, sunR);
  42. }
  43.  
  44. GLfloat wmW = 3.0, wmH = 6.0;
  45. GLfloat wmX = WIDTH / 2, wmY = wmH * 0.8, wmRotationAngle = 0.05, wmAngle = 0.0;
  46. GLfloat wmSize = 2.6;
  47.  
  48. void drawWindmill() {
  49. wmAngle += wmRotationAngle;
  50.  
  51. glColor3ub(163, 104, 8);
  52. glRectf(WIDTH / 2 - wmW / 2, 0.0, WIDTH / 2 + wmW / 2, wmH);
  53.  
  54. glColor3ub(201, 201, 201);
  55. for (size_t i = 0; i < 4; i++)
  56. {
  57. glBegin(GL_POLYGON);
  58. glVertex3f(wmX, wmY, 0.0);
  59. glVertex3f(wmX + cos(i * (M_PI / 2) - M_PI / 8 + wmAngle) * wmSize, wmY + sin(i * (M_PI / 2) - M_PI / 8 + wmAngle) * wmSize, 0.0);
  60. glVertex3f(wmX + cos(i * (M_PI / 2) + M_PI / 8 + wmAngle) * wmSize, wmY + sin(i * (M_PI / 2) + M_PI / 8 + wmAngle) * wmSize, 0.0);
  61. glEnd();
  62. }
  63. }
  64.  
  65. void moveSun() {
  66. sunX -= sunCenterX;
  67. sunY -= sunCenterY;
  68.  
  69. GLfloat newX = sunX * cos(sunRotationAngle) - sunY * sin(sunRotationAngle);
  70. GLfloat newY = sunX * sin(sunRotationAngle) + sunY * cos(sunRotationAngle);
  71.  
  72. sunX = newX + sunCenterX;
  73. sunY = newY + sunCenterY;
  74. }
  75.  
  76. void display()
  77. {
  78. glClear(GL_COLOR_BUFFER_BIT);
  79.  
  80. drawSun();
  81. drawWindmill();
  82.  
  83. Sleep(30);
  84.  
  85. glFlush();
  86.  
  87. glutPostRedisplay();
  88. }
  89.  
  90. void onMouseClick(int button, int state, int x, int y) {
  91. switch (button)
  92. {
  93. case GLUT_LEFT_BUTTON:
  94. if (state == GLUT_DOWN) {
  95. glutIdleFunc(moveSun);
  96. }
  97. else {
  98. glutIdleFunc(NULL);
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105.  
  106. void onReshape(int w, int h) {
  107. if (w / h) {
  108. WINDOW_HEIGHT = w / aspect_ratio;
  109. }
  110. else {
  111. WINDOW_HEIGHT = h;
  112. }
  113.  
  114. glViewport(0, 0, WINDOW_HEIGHT * aspect_ratio, WINDOW_HEIGHT);
  115. glMatrixMode(GL_PROJECTION);
  116. glLoadIdentity();
  117. glOrtho(0.0, WIDTH, 0.0, HEIGHT, -1.0, 1.0);
  118. }
  119.  
  120. void init()
  121. {
  122. glClearColor(0.0, 0.0, 0.0, 0.0);
  123. glMatrixMode(GL_PROJECTION);
  124. glLoadIdentity();
  125. glOrtho(0.0, 5.0, 0.0, 5.0, -5.0, 5.0);
  126. }
  127.  
  128. int main(int argc, char** argv)
  129. {
  130. glutInit(&argc, argv);
  131. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  132. glutInitWindowSize(WINDOW_HEIGHT * aspect_ratio, WINDOW_HEIGHT);
  133. glutInitWindowPosition(300, 180);
  134. glutCreateWindow("hello");
  135.  
  136. init();
  137.  
  138. glutDisplayFunc(display);
  139. glutReshapeFunc(onReshape);
  140. glutMouseFunc(onMouseClick);
  141. glutMainLoop();
  142.  
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement