Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. /*
  2. * GLUT Shapes Demo
  3. *
  4. * Written by Nigel Stewart November 2003
  5. *
  6. * This program is test harness for the sphere, cone
  7. * and torus shapes in GLUT.
  8. *
  9. * Spinning wireframe and smooth shaded shapes are
  10. * displayed until the ESC or q key is pressed. The
  11. * number of geometry stacks and slices can be adjusted
  12. * using the + and - keys.
  13. */
  14. #include<stdio.h>
  15. #include<math.h>
  16. #ifdef __APPLE__
  17. #include <GLUT/glut.h>
  18. #else
  19. #include <GL/glut.h>
  20. #endif
  21.  
  22. #include <stdlib.h>
  23. #include <math.h> /* cos */
  24.  
  25. #define PI 3.14159265
  26. static int slices = 16;
  27. static int stacks = 16;
  28. double xx=0,yy=0,zz=0;
  29. double translate_x=0,translate_y=0,translate_z=0;
  30. double rot_x=0,rot_y=0,rot_z=0;
  31. double zoom_x=0,zoom_y=0,zoom_z=0;
  32. double eye_x = 0;
  33. double eye_y = 0;
  34. double eye_z = 25;
  35. double cx=0,cy=0,cz=0;
  36. void ownscal(double x,double y,double z)
  37. {
  38. GLfloat mat[]=
  39. {
  40. x,0,0,0,
  41. 0,y,0,0,
  42. 0,0,z,0,
  43. 0,0,0,1
  44.  
  45.  
  46. };
  47. glMatrixMode(GL_MODELVIEW);
  48. glMultMatrixf(mat);
  49.  
  50.  
  51. }
  52. /* GLUT callback Handlers */
  53.  
  54. static void resize(int width, int height)
  55. {
  56. const float ar = (float) width / (float) height;
  57.  
  58. glViewport(0, 0, width, height);
  59. glMatrixMode(GL_PROJECTION);
  60. glLoadIdentity();
  61. double ar2=1.8;
  62. glFrustum(-ar2, ar2, -ar2, ar2, 2.0, 100.0);
  63. //glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  64. glMatrixMode(GL_MODELVIEW);
  65. glLoadIdentity() ;
  66. gluLookAt(0, 0, 25, 0, 0, 0, 0, 1, 0);
  67. }
  68.  
  69. void draw_cube()
  70. {
  71.  
  72. glBegin(GL_QUADS);
  73. //1
  74.  
  75. glVertex3f(1,0,1);
  76. glVertex3f(1,1,1);
  77. glVertex3f(1,1,0);
  78. glVertex3f(1,0,0);
  79. //2
  80.  
  81. glVertex3f(0,0,1);
  82. glVertex3f(0,1,1);
  83. glVertex3f(1,1,1);
  84. glVertex3f(1,0,1);
  85. //3
  86.  
  87. glVertex3f(0,1,0);
  88. glVertex3f(0,1,1);
  89. glVertex3f(0,0,1);
  90. glVertex3f(0,0,0);
  91. //4
  92.  
  93. glVertex3f(0,1,0);
  94. glVertex3f(1,1,0);
  95. glVertex3f(1,1,1);
  96. glVertex3f(0,1,1);
  97. //5
  98.  
  99. glVertex3f(1,0,0);
  100. glVertex3f(0,0,0);
  101. glVertex3f(0,0,1);
  102. glVertex3f(1,0,1);
  103. //6
  104. glVertex3f(1,1,0);
  105. glVertex3f(0,1,0);
  106. glVertex3f(0,0,0);
  107. glVertex3f(1,0,0);
  108. glEnd();
  109.  
  110. }
  111. static void blade(GLdouble scaley, GLdouble rot_angle, GLdouble rot_x, GLdouble rot_y, GLdouble rot_z)
  112. {
  113. glPushMatrix();
  114. glRotated(rot_angle, rot_x, rot_y, rot_z);
  115. glRotated(20, 0, 1, 0);
  116. glScaled(0.75, scaley, 0.1);
  117. glTranslated(0, 1, 0);
  118. draw_cube();
  119. glPopMatrix();
  120. }
  121. static void fan()
  122. {
  123. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  124. blade(-3, 0+t*250, 0,0 , 1);
  125. blade(-3, 120+t*250, 0, 0, 1);
  126. blade(-3, 240+t*250, 0, 0, 1);
  127. }
  128.  
  129. void cube(double translate_x,double translate_y,double translate_z,double rotate_x,double rotate_y,double rotate_z,double scale_x,double scale_y,double scale_z)
  130. {
  131. glPushMatrix();
  132. glColor3d(0.325, 0.176, 0.823);
  133. glTranslated(translate_x,translate_y,translate_z);
  134. glRotated(rotate_x,1,0,0);
  135. glRotated(rotate_y,0,1,0);
  136. glRotated(rotate_z,0,0,1);
  137. ownscal(scale_x,scale_y,scale_z);
  138. draw_cube();
  139. glPopMatrix();
  140.  
  141. }
  142.  
  143. void circle()
  144. {
  145. float theta=(22.0/7.0)/180.0;
  146. //glTranslated(.2,0,1);
  147.  
  148. glRotated(90,1,0,0);
  149. glBegin(GL_QUAD_STRIP);
  150. for(int i=0;i<360;i++)
  151. {
  152. //glColor3d(1,0,0);
  153. glVertex3f(cos(theta*i),+1,sin(theta*i));
  154. //glColor3d(1,0,0);
  155. glVertex3f(cos(theta*i),-1,sin(theta*i));
  156.  
  157.  
  158. }
  159. glEnd();
  160. float k=0;
  161. for(int i=1;i>=-1;i-=2)
  162. {
  163. glBegin(GL_TRIANGLE_FAN);
  164. //glColor3d(1,0,0);
  165. glVertex3f(0,i,0);
  166. for(k=0;k<360;k++){
  167. //glColor3d(1,0,0);
  168. glVertex3f(i*cos(k*theta),i+.01,sin(k*theta));
  169. glVertex3f(i*cos(k*theta),-i,sin(k*theta));
  170.  
  171. }
  172. glEnd();
  173. }
  174. }
  175.  
  176.  
  177.  
  178. void windmill()
  179. {
  180.  
  181.  
  182.  
  183. cube(0,0,0,0,0,-10,.50,13.80,.5);
  184. cube(2.70,0,0,0,0,0,.50,13.80,.5);
  185. cube(5.40,0,0,0,0,10,.50,13.80,.5);
  186. glTranslated(2.70,13.50,1.20);
  187. glColor3d(1,.3,.3);
  188. glScaled(2.40,3,.20);
  189. circle();
  190. fan();
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. }
  198.  
  199. static void display(void)
  200. {
  201.  
  202.  
  203.  
  204. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  205. //rod
  206. windmill();
  207. glMatrixMode(GL_MODELVIEW);
  208. glLoadIdentity() ;
  209. gluLookAt(eye_x, eye_y, eye_z, cx, cy, cz, 0, 1, 0);
  210.  
  211.  
  212. glutSwapBuffers();
  213. }
  214.  
  215.  
  216. static void key(unsigned char key, int x, int y)
  217. {
  218. double translate=.1;
  219. double rotation=1;
  220. double scale2=.1,lookat=0.5;
  221.  
  222.  
  223. switch (key)
  224. {
  225. case 't':
  226. translate_x+=translate;
  227. printf("translate_x: %.2f ",translate_x);
  228. break;
  229. case 'T':
  230. translate_x-=translate;
  231. printf("translate_x: %.2f ",xx);
  232. break;
  233. case 'y':
  234. translate_y+=translate;
  235. printf("translate_y: %.2f ",translate_y);
  236. break;
  237. case 'Y':
  238. translate_y-=translate;
  239. printf("translate_y: %.2f ",translate_y);
  240. break;
  241. case 'u':
  242. translate_z+=translate;
  243. printf("translate_z: %.2f ",translate_z);
  244. break;
  245. case 'U':
  246. translate_z-=translate;
  247. printf("translate_z: %.2f ",translate_z);
  248. break;
  249.  
  250. case 'r':
  251. rot_x+=rotation;
  252. printf("rot_x: %.2f ",rot_x);
  253. break;
  254. case 'R':
  255. rot_x-=rotation;
  256. printf("rot_x: %.2f ",rot_x);
  257. break;
  258. case 'e':
  259. rot_y+=rotation;
  260. printf("rot_y: %.2f ",rot_y);
  261. break;
  262. case 'E':
  263. rot_y-=rotation;
  264. printf("rot_y: %.2f ",rot_y);
  265. break;
  266. case 'w':
  267. rot_z+=rotation;
  268. printf("rot_z: %.2f ",rot_z);
  269. break;
  270. case 'W':
  271. rot_z-=rotation;
  272. printf("rot_z: %.2f ",rot_z);
  273. break;
  274. case 's':
  275. zoom_x+=scale2;
  276. //printf("xx: %.2f ",zz);
  277. break;
  278. case 'S':
  279. zoom_x-=scale2;
  280. //printf("xx: %.2f ",zz);
  281. break;
  282. case 'd':
  283. zoom_y+=scale2;
  284. //printf("xx: %.2f ",zz);
  285. break;
  286. case 'D':
  287. zoom_y-=scale2;
  288. //printf("xx: %.2f ",zz);
  289. break;
  290. case 'f':
  291. zoom_z+=scale2;
  292. //printf("xx: %.2f ",zz);
  293. break;
  294. case 'F':
  295. zoom_z-=scale2;
  296. //printf("xx: %.2f ",zz);
  297. break;
  298. case 'j':
  299. eye_x+=lookat;
  300. //printf("xx: %.2f ",zz);
  301. break;
  302. case 'J':
  303. eye_x-=lookat;
  304. //printf("xx: %.2f ",zz);
  305. break;
  306. case 'k':
  307. eye_y+=lookat;
  308. //printf("xx: %.2f ",zz);
  309. break;
  310. case 'K':
  311. eye_y-=lookat;
  312. //printf("xx: %.2f ",zz);
  313. break;
  314. case 'l':
  315. eye_z+=lookat;
  316. //printf("xx: %.2f ",zz);
  317. break;
  318. case 'L':
  319. eye_z-=lookat;
  320. //printf("xx: %.2f ",zz);
  321. break;
  322. case 'b':
  323. cx+=lookat;
  324. //printf("xx: %.2f ",zz);
  325. break;
  326. case 'B':
  327. cx-=lookat;
  328. //printf("xx: %.2f ",zz);
  329. break;
  330.  
  331. case 'n':
  332. cy+=lookat;
  333. //printf("xx: %.2f ",zz);
  334. break;
  335. case 'N':
  336. cy-=lookat;
  337. //printf("xx: %.2f ",zz);
  338. break;
  339.  
  340. case 'm':
  341. cz+=lookat;
  342. //printf("xx: %.2f ",zz);
  343. break;
  344. case 'M':
  345. cz-=lookat;
  346. //printf("xx: %.2f ",zz);
  347. break;
  348.  
  349.  
  350. case 27 :
  351. case 'q':
  352. printf("\nzoom_x: %.2f zoom_y: %.2f zoom_z: %.2f",zoom_x,zoom_y,zoom_z);
  353. printf("\ntranslate_x: %.2f translate_y: %.2f translate_z: %.2f",translate_x,translate_y,translate_z);
  354. printf("\nrotate_x: %.2f rotate_y: %.2f rotate_z: %.2f",rot_x,rot_y,rot_z);
  355.  
  356. exit(0);
  357. break;
  358.  
  359. case '+':
  360. slices++;
  361. stacks++;
  362. break;
  363.  
  364. case '-':
  365. if (slices>3 && stacks>3)
  366. {
  367. slices--;
  368. stacks--;
  369. }
  370. break;
  371. }
  372.  
  373. glutPostRedisplay();
  374. }
  375.  
  376. static void idle(void)
  377. {
  378. glutPostRedisplay();
  379. }
  380.  
  381.  
  382.  
  383. int main(int argc, char *argv[])
  384. {
  385. glutInit(&argc, argv);
  386. glutInitWindowSize(640,480);
  387. glutInitWindowPosition(10,10);
  388. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  389.  
  390. glutCreateWindow("GLUT Shapes");
  391.  
  392. glutReshapeFunc(resize);
  393. glutDisplayFunc(display);
  394. glutKeyboardFunc(key);
  395. glutIdleFunc(idle);
  396.  
  397. glClearColor(1,1,1,1);
  398. glEnable(GL_CULL_FACE);
  399. glCullFace(GL_BACK);
  400.  
  401. glEnable(GL_DEPTH_TEST);
  402. glDepthFunc(GL_LESS);
  403.  
  404.  
  405.  
  406. glutMainLoop();
  407.  
  408. return EXIT_SUCCESS;
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement