Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 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 cube()
  112. {
  113. glBegin(GL_QUADS);
  114.  
  115. glColor3f(1, 0, 0);
  116. glVertex3f(1, 1, 1);
  117. glVertex3f(-1, 1, 1);
  118. glVertex3f(-1, -1, 1);
  119. glVertex3f(1, -1, 1);
  120.  
  121. glColor3f(0, 1, 0);
  122. glVertex3f(1, 1, -1);
  123. glVertex3f(1, 1, 1);
  124. glVertex3f(1, -1, 1);
  125. glVertex3f(1, -1, -1);
  126.  
  127. glColor3f(0, 0, 1);
  128. glVertex3f(-1, 1, -1);
  129. glVertex3f(-1, 1, 1);
  130. glVertex3f(1, 1, 1);
  131. glVertex3f(1, 1, -1);
  132.  
  133. glColor3f(1, 1, 0);
  134. glVertex3f(-1, 1, 1);
  135. glVertex3f(-1, 1, -1);
  136. glVertex3f(-1, -1, -1);
  137. glVertex3f(-1, -1, 1);
  138.  
  139. glColor3f(1, 0, 1);
  140. glVertex3f(-1, -1, 1);
  141. glVertex3f(-1, -1, -1);
  142. glVertex3f(1, -1, -1);
  143. glVertex3f(1, -1, 1);
  144.  
  145. glColor3f(0, 1, 1);
  146. glVertex3f(-1, 1, -1);
  147. glVertex3f(1, 1, -1);
  148. glVertex3f(1, -1, -1);
  149. glVertex3f(-1, -1, -1);
  150.  
  151. glEnd();
  152. }
  153. static void blade(GLdouble scaley, GLdouble rot_angle, GLdouble rot_x, GLdouble rot_y, GLdouble rot_z)
  154. {
  155. glPushMatrix();
  156. glRotated(rot_angle, rot_x, rot_y, rot_z);
  157. glRotated(20, 0, 1, 0);
  158. glScaled(0.75, scaley, 0.1);
  159. glTranslated(0, 1, 0);
  160. cube();
  161. glPopMatrix();
  162. }
  163. static void fan()
  164. {
  165. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  166. blade(-3, 0+t*250, 0,0 , 1);
  167. blade(-3, 120+t*250, 0, 0, 1);
  168. blade(-3, 240+t*250, 0, 0, 1);
  169. }
  170.  
  171. 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)
  172. {
  173. glPushMatrix();
  174. glColor3d(0.325, 0.176, 0.823);
  175. glTranslated(translate_x,translate_y,translate_z);
  176. glRotated(rotate_x,1,0,0);
  177. glRotated(rotate_y,0,1,0);
  178. glRotated(rotate_z,0,0,1);
  179. ownscal(scale_x,scale_y,scale_z);
  180. draw_cube();
  181. glPopMatrix();
  182.  
  183. }
  184.  
  185. void circle()
  186. {
  187. float theta=(22.0/7.0)/180.0;
  188. //glTranslated(.2,0,1);
  189.  
  190. glRotated(90,1,0,0);
  191. glBegin(GL_QUAD_STRIP);
  192. for(int i=0;i<360;i++)
  193. {
  194. //glColor3d(1,0,0);
  195. glVertex3f(cos(theta*i),+1,sin(theta*i));
  196. //glColor3d(1,0,0);
  197. glVertex3f(cos(theta*i),-1,sin(theta*i));
  198.  
  199.  
  200. }
  201. glEnd();
  202. float k=0;
  203. for(int i=1;i>=-1;i-=2)
  204. {
  205. glBegin(GL_TRIANGLE_FAN);
  206. //glColor3d(1,0,0);
  207. glVertex3f(0,i,0);
  208. for(k=0;k<360;k++){
  209. //glColor3d(1,0,0);
  210. glVertex3f(i*cos(k*theta),i+.01,sin(k*theta));
  211. glVertex3f(i*cos(k*theta),-i,sin(k*theta));
  212.  
  213. }
  214. glEnd();
  215. }
  216. }
  217.  
  218.  
  219.  
  220. void windmill()
  221. {
  222.  
  223.  
  224.  
  225. cube(0,0,0,0,0,-10,.50,13.80,.5);
  226. cube(2.70,0,0,0,0,0,.50,13.80,.5);
  227. cube(5.40,0,0,0,0,10,.50,13.80,.5);
  228. glTranslated(2.70,13.50,1.20);
  229. glColor3d(1,.3,.3);
  230. glScaled(2.40,3,.20);
  231. circle();
  232. fan();
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. }
  240.  
  241. static void display(void)
  242. {
  243.  
  244.  
  245.  
  246. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  247. //rod
  248. windmill();
  249. glMatrixMode(GL_MODELVIEW);
  250. glLoadIdentity() ;
  251. gluLookAt(eye_x, eye_y, eye_z, cx, cy, cz, 0, 1, 0);
  252.  
  253.  
  254. glutSwapBuffers();
  255. }
  256.  
  257.  
  258. static void key(unsigned char key, int x, int y)
  259. {
  260. double translate=.1;
  261. double rotation=1;
  262. double scale2=.1,lookat=0.5;
  263.  
  264.  
  265. switch (key)
  266. {
  267. case 't':
  268. translate_x+=translate;
  269. printf("translate_x: %.2f ",translate_x);
  270. break;
  271. case 'T':
  272. translate_x-=translate;
  273. printf("translate_x: %.2f ",xx);
  274. break;
  275. case 'y':
  276. translate_y+=translate;
  277. printf("translate_y: %.2f ",translate_y);
  278. break;
  279. case 'Y':
  280. translate_y-=translate;
  281. printf("translate_y: %.2f ",translate_y);
  282. break;
  283. case 'u':
  284. translate_z+=translate;
  285. printf("translate_z: %.2f ",translate_z);
  286. break;
  287. case 'U':
  288. translate_z-=translate;
  289. printf("translate_z: %.2f ",translate_z);
  290. break;
  291.  
  292. case 'r':
  293. rot_x+=rotation;
  294. printf("rot_x: %.2f ",rot_x);
  295. break;
  296. case 'R':
  297. rot_x-=rotation;
  298. printf("rot_x: %.2f ",rot_x);
  299. break;
  300. case 'e':
  301. rot_y+=rotation;
  302. printf("rot_y: %.2f ",rot_y);
  303. break;
  304. case 'E':
  305. rot_y-=rotation;
  306. printf("rot_y: %.2f ",rot_y);
  307. break;
  308. case 'w':
  309. rot_z+=rotation;
  310. printf("rot_z: %.2f ",rot_z);
  311. break;
  312. case 'W':
  313. rot_z-=rotation;
  314. printf("rot_z: %.2f ",rot_z);
  315. break;
  316. case 's':
  317. zoom_x+=scale2;
  318. //printf("xx: %.2f ",zz);
  319. break;
  320. case 'S':
  321. zoom_x-=scale2;
  322. //printf("xx: %.2f ",zz);
  323. break;
  324. case 'd':
  325. zoom_y+=scale2;
  326. //printf("xx: %.2f ",zz);
  327. break;
  328. case 'D':
  329. zoom_y-=scale2;
  330. //printf("xx: %.2f ",zz);
  331. break;
  332. case 'f':
  333. zoom_z+=scale2;
  334. //printf("xx: %.2f ",zz);
  335. break;
  336. case 'F':
  337. zoom_z-=scale2;
  338. //printf("xx: %.2f ",zz);
  339. break;
  340. case 'j':
  341. eye_x+=lookat;
  342. //printf("xx: %.2f ",zz);
  343. break;
  344. case 'J':
  345. eye_x-=lookat;
  346. //printf("xx: %.2f ",zz);
  347. break;
  348. case 'k':
  349. eye_y+=lookat;
  350. //printf("xx: %.2f ",zz);
  351. break;
  352. case 'K':
  353. eye_y-=lookat;
  354. //printf("xx: %.2f ",zz);
  355. break;
  356. case 'l':
  357. eye_z+=lookat;
  358. //printf("xx: %.2f ",zz);
  359. break;
  360. case 'L':
  361. eye_z-=lookat;
  362. //printf("xx: %.2f ",zz);
  363. break;
  364. case 'b':
  365. cx+=lookat;
  366. //printf("xx: %.2f ",zz);
  367. break;
  368. case 'B':
  369. cx-=lookat;
  370. //printf("xx: %.2f ",zz);
  371. break;
  372.  
  373. case 'n':
  374. cy+=lookat;
  375. //printf("xx: %.2f ",zz);
  376. break;
  377. case 'N':
  378. cy-=lookat;
  379. //printf("xx: %.2f ",zz);
  380. break;
  381.  
  382. case 'm':
  383. cz+=lookat;
  384. //printf("xx: %.2f ",zz);
  385. break;
  386. case 'M':
  387. cz-=lookat;
  388. //printf("xx: %.2f ",zz);
  389. break;
  390.  
  391.  
  392. case 27 :
  393. case 'q':
  394. printf("\nzoom_x: %.2f zoom_y: %.2f zoom_z: %.2f",zoom_x,zoom_y,zoom_z);
  395. printf("\ntranslate_x: %.2f translate_y: %.2f translate_z: %.2f",translate_x,translate_y,translate_z);
  396. printf("\nrotate_x: %.2f rotate_y: %.2f rotate_z: %.2f",rot_x,rot_y,rot_z);
  397.  
  398. exit(0);
  399. break;
  400.  
  401. case '+':
  402. slices++;
  403. stacks++;
  404. break;
  405.  
  406. case '-':
  407. if (slices>3 && stacks>3)
  408. {
  409. slices--;
  410. stacks--;
  411. }
  412. break;
  413. }
  414.  
  415. glutPostRedisplay();
  416. }
  417.  
  418. static void idle(void)
  419. {
  420. glutPostRedisplay();
  421. }
  422.  
  423.  
  424.  
  425. int main(int argc, char *argv[])
  426. {
  427. glutInit(&argc, argv);
  428. glutInitWindowSize(640,480);
  429. glutInitWindowPosition(10,10);
  430. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  431.  
  432. glutCreateWindow("GLUT Shapes");
  433.  
  434. glutReshapeFunc(resize);
  435. glutDisplayFunc(display);
  436. glutKeyboardFunc(key);
  437. glutIdleFunc(idle);
  438.  
  439. glClearColor(1,1,1,1);
  440. glEnable(GL_CULL_FACE);
  441. glCullFace(GL_BACK);
  442.  
  443. glEnable(GL_DEPTH_TEST);
  444. glDepthFunc(GL_LESS);
  445.  
  446.  
  447.  
  448. glutMainLoop();
  449.  
  450. return EXIT_SUCCESS;
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement