Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <GL/freeglut.h>
  2. #include <math.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5.  
  6. float const pi = 3.14159;
  7. int Mx = 0, My = 0;
  8.  
  9.  
  10. class Moon
  11. {
  12. public:
  13. float r;
  14. float x, y;
  15. float dist;
  16. float theta;
  17. Moon() {
  18. theta = rand();
  19. }
  20.  
  21. void setVar(float tr,float tdist, float tx, float ty) {
  22. r = tr;
  23. x = tx;
  24. y = ty;
  25. dist = tdist;
  26. }
  27.  
  28. void update() {
  29. theta -= 0.005;
  30. }
  31.  
  32. void display() {
  33. int n = 360;
  34.  
  35. float lx = (dist * cos(theta));
  36. float ly = (dist * sin(theta));
  37. float theta2;
  38. glColor3f(1.0, 0.8, 0.3);
  39. glBegin(GL_POLYGON);
  40. for (int j = 0; j < 360; j++) {
  41. theta2 = (pi * 2.0)*j / n;
  42. glVertex2f(x + lx + (r * cos(theta2)), (y + ly + (r * sin(theta2))));
  43. }
  44. glEnd();
  45. glColor3f(0.0, 0.0, 0.0);
  46. glBegin(GL_LINE_LOOP);
  47. for (int j = 0; j < 360; j++) {
  48. theta2 = (pi * 2.0)*j / n;
  49. glVertex2f(x + lx + (r * cos(theta2)), (y + ly + (r * sin(theta2))));
  50. }
  51. glEnd();
  52. }
  53.  
  54. };
  55.  
  56.  
  57. class Planet
  58. {
  59. public:
  60. float r;
  61. float dist;
  62. float theta;
  63. //float distEtoM = rand()%10+15;
  64. Moon moon;
  65. Planet(float tr,float tdist) {
  66. r = tr;
  67. dist = tdist;
  68. theta = rand();
  69. }
  70.  
  71. void update() {
  72. theta += 0.001;
  73. }
  74.  
  75. void display() {
  76. int n = 360;
  77.  
  78. //theta = rand();
  79. float lx = (dist * cos(theta));
  80. float ly = (dist * sin(theta));
  81. moon.setVar(r / 3, r + 20, lx, ly);
  82. float theta2;
  83. glColor3f(1.0, 0.8, 0.3);
  84. glBegin(GL_POLYGON);
  85. for (int j = 0; j < 360; j++) {
  86. theta2 = (pi * 2.0)*j / n;
  87. glVertex2f(lx + (r * cos(theta2)), (ly + (r * sin(theta2))));
  88. }
  89. glEnd();
  90. moon.display();
  91. moon.update();
  92. glColor3f(0.0, 0.0, 0.0);
  93. glBegin(GL_LINE_LOOP);
  94. for (int j = 0; j < 360; j++) {
  95. theta2 = (pi * 2.0)*j / n;
  96. glVertex2f(lx + (r * cos(theta2)), (ly + (r * sin(theta2))));
  97. }
  98. glEnd();
  99. }
  100.  
  101. };
  102.  
  103. void init(void)
  104. {
  105. glClearColor(0.8, 0.8, 0.8, 1.0);
  106. gluOrtho2D(-500, 500.0, -500.0, 500.0);
  107. }
  108.  
  109.  
  110. Planet a1(20, 160);
  111. Planet a2(15, 200);
  112. Planet a3(25, 250);
  113. Planet a4(30, 310);
  114. Planet a5(20, 370);
  115. Planet a6(50, 430);
  116. //Planet a7(20, 180);
  117. //Planet a8(20, 180);
  118.  
  119. void display()
  120. {
  121. srand(time(0));
  122. glClear(GL_COLOR_BUFFER_BIT);
  123. int x = 0;
  124. int y = 0;
  125. glColor3f(0.3, 0.2, 0.1);
  126. glBegin(GL_POLYGON);
  127. for (int i = 0; i < 360; i++) {
  128. float theta = (pi * 2.0)*i / 360;
  129. glVertex2f((100 * cos(theta)), ((100 * sin(theta))));
  130. }
  131. glEnd();
  132. glColor3f(0.0, 0.0, 0.0);
  133. glBegin(GL_LINE_LOOP);
  134. for (int i = 0; i < 360; i++) {
  135. float theta = (pi * 2.0)*i / 360;
  136. glVertex2f((100 * cos(theta)), ((100 * sin(theta))));
  137. }
  138. glEnd();
  139.  
  140. a1.display();
  141. a1.update();
  142. a2.display();
  143. a2.update();
  144. a3.display();
  145. a3.update();
  146. a4.display();
  147. a4.update();
  148. a5.display();
  149. a5.update();
  150. a6.display();
  151. a6.update();
  152.  
  153. glutPostRedisplay();
  154. //
  155. glFlush();
  156. }
  157.  
  158. int main(int argc, char** argv)
  159. {
  160. glutInit(&argc, argv);
  161. glutInitDisplayMode(GLUT_RGBA);
  162. glutInitWindowSize(800, 800);
  163. glutInitWindowPosition(100, 100);
  164. glutCreateWindow("Test OpenGL");
  165. glutDisplayFunc(display);
  166. glutKeyboardFunc(keyboard);
  167. init();
  168. glutMainLoop();
  169. return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement