Guest User

Untitled

a guest
May 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <stdarg.h>
  4. #include <math.h>
  5. #include <GL/glut.h>
  6. using namespace std;
  7.  
  8. void display();
  9. void specialKeys();
  10. double rotate_y=0;
  11. double rotate_x=0;
  12. int N = 5;
  13. float de = 0.2;
  14. float d = 0.5;
  15. float D = 0.7;
  16. float P = 0.3;
  17. float h = 0;
  18.  
  19. void drawAxis(){
  20. glBegin(GL_LINES);
  21. glColor3f( 1.0, 0.0, 0.0 );
  22. glVertex3i(-100,0,0);
  23. glVertex3i(100,0,0);
  24. glColor3f( 0.0, 1.0, 0.0 );
  25. glVertex3i(0,-100,0);
  26. glVertex3i(0,100,0);
  27. glColor3f( 0.0, 0.0, 1.0 );
  28. glVertex3i(0,0,-100);
  29. glVertex3i(0,0,100);
  30. glEnd();
  31. }
  32.  
  33. void display(){
  34.  
  35. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  36.  
  37. glLoadIdentity();
  38.  
  39. glRotatef( rotate_x, 1.0, 0.0, 0.0 );
  40. glRotatef( rotate_y, 0.0, 1.0, 0.0 );
  41.  
  42. drawAxis();
  43.  
  44. glPointSize(5);
  45. glLineWidth(2);
  46.  
  47. glBegin(GL_LINES);
  48.  
  49. h = D - d;
  50. h = h *N / 2000;
  51.  
  52. glColor3f( 1.0, 0.0, 1.0 );
  53. for(int i=0;i<2000;i++){
  54. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  55. float x;
  56. float y;
  57. if (i%(2000/N)==0 ){
  58. x = (D - h* (i%(2000/N))) * cosf(theta);
  59. y = (D - h* (i%(2000/N))) * sinf(theta);
  60. glVertex3f(x,y,-P/2);
  61. x = (D - h* (i%(2000/N))) * cosf(theta);
  62. y = (D - h* (i%(2000/N))) * sinf(theta);
  63. glVertex3f(x,y,P/2);
  64.  
  65. x = (d + 0.02f ) * cosf(2.0f * 3.1415926f * float(i-1) / float(2000));
  66. y = (d + 0.02f ) * sinf(2.0f * 3.1415926f * float(i-1) / float(2000));
  67. glVertex3f(x,y,-P/2);
  68. x = (d + 0.02f ) * cosf(2.0f * 3.1415926f * float(i-1) / float(2000));
  69. y = (d + 0.02f ) * sinf(2.0f * 3.1415926f * float(i-1) / float(2000));
  70. glVertex3f(x,y,P/2);
  71. }
  72. }
  73.  
  74. glEnd();
  75.  
  76. glBegin(GL_LINE_LOOP);
  77.  
  78. h = D - d;
  79. h = h *N / 2000;
  80.  
  81. glColor3f( 1.0, 0.0, 1.0 );
  82. for(int i=0;i<2000;i++){
  83. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  84. float x = (D - h* (i%(2000/N))) * cosf(theta);
  85. float y = (D - h* (i%(2000/N))) * sinf(theta);
  86. glVertex3f(x,y,-P/2);
  87. }
  88.  
  89. glEnd();
  90.  
  91. glBegin(GL_LINE_LOOP);
  92.  
  93. h = D - d;
  94. h = h *N / 2000;
  95.  
  96. glColor3f( 1.0, 0.0, 1.0 );
  97. for(int i=0;i<2000;i++){
  98. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  99. float x = de * cosf(theta);
  100. float y = de * sinf(theta);
  101. glVertex3f(x,y,-P/2);
  102. }
  103.  
  104. glEnd();
  105.  
  106. glBegin(GL_LINE_LOOP);
  107.  
  108. h = D - d;
  109. h = h *N / 2000;
  110.  
  111. glColor3f( 1.0, 0.0, 1.0 );
  112. for(int i=0;i<2000;i++){
  113. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  114. float x = (D - h* (i%(2000/N))) * cosf(theta);
  115. float y = (D - h* (i%(2000/N))) * sinf(theta);
  116. glVertex3f(x,y,P/2);
  117. }
  118.  
  119. glEnd();
  120.  
  121. glBegin(GL_LINE_LOOP);
  122.  
  123. h = D - d;
  124. h = h *N / 2000;
  125.  
  126. glColor3f( 1.0, 0.0, 1.0 );
  127. for(int i=0;i<2000;i++){
  128. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  129. float x = de * cosf(theta);
  130. float y = de * sinf(theta);
  131. glVertex3f(x,y,P/2);
  132. }
  133.  
  134. glEnd();
  135.  
  136. glBegin(GL_QUAD_STRIP);
  137.  
  138. h = D - d;
  139. h = h *N / 2000;
  140.  
  141. glColor3f( 0.0, 1.0, 1.0 );
  142. for(int i=0;i<2000;i++){
  143. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  144. float x = (D - h* (i%(2000/N))) * cosf(theta);
  145. float y = (D - h* (i%(2000/N))) * sinf(theta);
  146. glVertex3f(x,y,-P/2);
  147. x = de * cosf(theta);
  148. y = de * sinf(theta);
  149. glVertex3f(x,y,-P/2);
  150. }
  151.  
  152. for(int i=0;i<2000;i++){
  153. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  154. float x = (D - h* (i%(2000/N))) * cosf(theta);
  155. float y = (D - h* (i%(2000/N))) * sinf(theta);
  156. glVertex3f(x,y,P/2);
  157. x = de * cosf(theta);
  158. y = de * sinf(theta);
  159. glVertex3f(x,y,P/2);
  160. }
  161. for(int i=0;i<2000;i++){
  162. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  163. float x = (D - h* (i%(2000/N))) * cosf(theta);
  164. float y = (D - h* (i%(2000/N))) * sinf(theta);
  165. glVertex3f(x,y,-P/2);
  166. x = (D - h* (i%(2000/N))) * cosf(theta);
  167. y = (D - h* (i%(2000/N))) * sinf(theta);
  168. glVertex3f(x,y,P/2);
  169. }
  170. for(int i=0;i<2000;i++){
  171. float theta = 2.0f * 3.1415926f * float(i) / float(2000);
  172. float x = de * cosf(theta);
  173. float y = de * sinf(theta);
  174. glVertex3f(x,y,-P/2);
  175. x = de * cosf(theta);
  176. y = de * sinf(theta);
  177. glVertex3f(x,y,P/2);
  178. }
  179.  
  180. glEnd();
  181.  
  182. glFlush();
  183. glutSwapBuffers();
  184.  
  185. }
  186.  
  187. void specialKeys( int key, int x, int y ) {
  188.  
  189.  
  190. if (key == GLUT_KEY_RIGHT)
  191. rotate_y += 5;
  192.  
  193. else if (key == GLUT_KEY_LEFT)
  194. rotate_y -= 5;
  195.  
  196. else if (key == GLUT_KEY_UP)
  197. rotate_x += 5;
  198.  
  199. else if (key == GLUT_KEY_DOWN)
  200. rotate_x -= 5;
  201.  
  202. glutPostRedisplay();
  203.  
  204. }
  205.  
  206. int main(int argc, char* argv[]){
  207.  
  208. glutInit(&argc,argv);
  209.  
  210. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  211. glutInitWindowSize(600,600);
  212. glutCreateWindow("Awesome Cube");
  213.  
  214. glEnable(GL_DEPTH_TEST);
  215.  
  216. glutDisplayFunc(display);
  217. glutSpecialFunc(specialKeys);
  218.  
  219. glutMainLoop();
  220.  
  221. return 0;
  222. }
Add Comment
Please, Sign In to add comment