Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 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 <windows.h>
  15. /*
  16. * GLUT Shapes Demo
  17. *
  18. * Written by Nigel Stewart November 2003
  19. *
  20. * This program is test harness for the sphere, cone
  21. * and torus shapes in GLUT.
  22. *
  23. * Spinning wireframe and smooth shaded shapes are
  24. * displayed until the ESC or q key is pressed. The
  25. * number of geometry stacks and slices can be adjusted
  26. * using the + and - keys.
  27. */
  28. #include<windows.h>
  29. #ifdef __APPLE__
  30. #include <GLUT/glut.h>
  31. #else
  32. #include <GL/glut.h>
  33. #endif
  34.  
  35. #include <stdlib.h>
  36.  
  37. static int slices = 16;
  38. static int stacks = 16;
  39. float degree=0.0,deg=0.0,deg2=0.0;
  40.  
  41. /* GLUT callback Handlers */
  42.  
  43. static void resize(int width, int height)
  44. {
  45. const float ar = (float) width / (float) height;
  46.  
  47. glViewport(0, 0, width, height);
  48. glMatrixMode(GL_PROJECTION);
  49. glLoadIdentity();
  50. glFrustum(-ar, ar, -1.0, 4.0, 4.0, 100.0);
  51.  
  52. glMatrixMode(GL_MODELVIEW);
  53. glLoadIdentity() ;
  54. //gluLookAt(1,1,1,1,1,0,0,-1,0);
  55. }
  56.  
  57.  
  58.  
  59.  
  60. void Cube()
  61. {
  62. //Basement
  63. glBegin(GL_QUADS);
  64. glColor3d(1,0,0);
  65. glVertex3f(0.0,2.0,10.0);
  66. glVertex3f(0.0,0.0,10.0);
  67. glVertex3f(10.0,0.0,10.0);
  68. glVertex3f(10.0,2.0,10.0);
  69.  
  70. glColor3d(0,1,0);
  71. glVertex3f(10.0,2.0,10.0);
  72. glVertex3f(10.0,0.0,10.0);
  73. glVertex3f(10.0,0.0,0.0);
  74. glVertex3f(10.0,2.0,0.0);
  75.  
  76. glColor3d(0,0,1);
  77. glVertex3f(0.0,2.0,0.0);
  78. glVertex3f(10.0,2.0,0.0);
  79. glVertex3f(10.0,0.0,0.0);
  80. glVertex3f(0.0,0.0,0.0);
  81.  
  82. glColor3d(1,0,1);
  83. glVertex3f(0.0,2.0,0.0);
  84. glVertex3f(0.0,0.0,0.0);
  85. glVertex3f(0.0,0.0,10.0);
  86. glVertex3f(0.0,2.0,10.0);
  87.  
  88. glColor3d(1,1,0);
  89. glVertex3f(0.0,0.0,0.0);
  90. glVertex3f(10.0,0.0,0.0);
  91. glVertex3f(10.0,0.0,10.0);
  92. glVertex3f(0.0,0.0,10.0);
  93.  
  94.  
  95. glColor3d(0,1,1);
  96. glVertex3f(0.0,2.0,0.0);
  97. glVertex3f(0.0,2.0,10.0);
  98. glVertex3f(10.0,2.0,10.0);
  99. glVertex3f(10.0,2.0,0.0);
  100.  
  101. glEnd();
  102.  
  103. //Upper board
  104.  
  105. glBegin(GL_QUADS);
  106. glColor3d(1,0,0);
  107. glVertex3f(2.5,12.0,7.5);
  108. glVertex3f(2.5,10.0,7.5);
  109. glVertex3f(7.5,10.0,7.5);
  110. glVertex3f(7.5,12.0,7.5);
  111.  
  112. glColor3d(0,1,0);
  113. glVertex3f(7.5,12.0,7.5);
  114. glVertex3f(7.5,10.0,7.5);
  115. glVertex3f(7.5,10.0,2.5);
  116. glVertex3f(7.5,12.0,2.5);
  117.  
  118. glColor3d(0,0,1);
  119. glVertex3f(2.5,12.0,2.5);
  120. glVertex3f(7.5,12.0,2.5);
  121. glVertex3f(7.5,10.0,2.5);
  122. glVertex3f(2.5,10.0,2.5);
  123.  
  124. glColor3d(1,0,1);
  125. glVertex3f(2.5,12.0,2.5);
  126. glVertex3f(2.5,10.0,2.5);
  127. glVertex3f(2.5,10.0,7.5);
  128. glVertex3f(2.5,12.0,7.5);
  129.  
  130. glColor3d(1,1,0);
  131. glVertex3f(2.5,10.0,2.5);
  132. glVertex3f(7.5,10.0,2.5);
  133. glVertex3f(7.5,10.0,7.5);
  134. glVertex3f(2.5,10.0,7.5);
  135.  
  136.  
  137. glColor3d(0,1,1);
  138. glVertex3f(2.5,12.0,2.5);
  139. glVertex3f(2.5,12.0,7.5);
  140. glVertex3f(7.5,12.0,7.5);
  141. glVertex3f(7.5,12.0,2.5);
  142.  
  143. glEnd();
  144.  
  145. //legs 1
  146.  
  147. glBegin(GL_QUADS);
  148. glColor3d(1,0,0);
  149. glVertex3f(0,0,10);
  150. glVertex3f(1,0,10);
  151. glVertex3f(5,20,6);
  152. glVertex3f(4,20,6);
  153.  
  154. glColor3d(0,1,0);
  155. glVertex3f(1,0,10);
  156. glVertex3f(0,0,9);
  157. glVertex3f(4,20,5);
  158. glVertex3f(5,20,6);
  159.  
  160. glColor3d(0,0,1);
  161. glVertex3f(4,20,5);
  162. glVertex3f(0,0,9);
  163. glVertex3f(0,0,10);
  164. glVertex3f(4,20,6);
  165.  
  166. glEnd();
  167.  
  168. //2
  169.  
  170. glBegin(GL_QUADS);
  171. glColor3d(1,0,0);
  172. glVertex3f(5,20,6);
  173. glVertex3f(9,0,10);
  174. glVertex3f(10,0,10);
  175. glVertex3f(6,20,6);
  176.  
  177. glColor3d(0,1,0);
  178. glVertex3f(6,20,6);
  179. glVertex3f(10,0,10);
  180. glVertex3f(10,0,9);
  181. glVertex3f(6,20,5);
  182.  
  183. glColor3d(0,0,1);
  184. glVertex3f(5,20,6);
  185. glVertex3f(6,20,5);
  186. glVertex3f(10,0,9);
  187. glVertex3f(9,0,10);
  188.  
  189. glEnd();
  190.  
  191.  
  192. //3
  193.  
  194. glBegin(GL_QUADS);
  195. glColor3d(1,0,0);
  196. glVertex3f(6,20,5);
  197. glVertex3f(10,0,1);
  198. glVertex3f(10,0,0);
  199. glVertex3f(6,20,4);
  200.  
  201. glColor3d(0,1,0);
  202. glVertex3f(6,20,4);
  203. glVertex3f(10,0,0);
  204. glVertex3f(9,0,0);
  205. glVertex3f(5,20,4);
  206.  
  207. glColor3d(0,0,1);
  208. glVertex3f(6,20,5);
  209. glVertex3f(5,20,4);
  210. glVertex3f(9,0,0);
  211. glVertex3f(10,0,1);
  212.  
  213. glEnd();
  214.  
  215.  
  216. //4
  217.  
  218. glBegin(GL_QUADS);
  219. glColor3d(1,0,0);
  220. glVertex3f(5,20,4);
  221. glVertex3f(4,20,5);
  222. glVertex3f(0,0,1);
  223. glVertex3f(1,0,0);
  224.  
  225. glColor3d(0,1,0);
  226. glVertex3f(4,20,4);
  227. glVertex3f(5,20,4);
  228. glVertex3f(1,0,0);
  229. glVertex3f(0,0,0);
  230.  
  231. glColor3d(0,0,1);
  232. glVertex3f(4,20,5);
  233. glVertex3f(4,20,4);
  234. glVertex3f(0,0,0);
  235. glVertex3f(0,0,1);
  236.  
  237. glEnd();
  238.  
  239.  
  240. //up
  241.  
  242. glBegin(GL_QUADS);
  243.  
  244. glColor3d(1,0,0);
  245. glVertex3f(4,20,8);
  246. glVertex3f(6,20,8);
  247. glVertex3f(6,24,8);
  248. glVertex3f(4,24,8);
  249.  
  250. glColor3d(0,1,0);
  251. glVertex3f(6,24,8);
  252. glVertex3f(6,20,8);
  253. glVertex3f(6,20,4);
  254. glVertex3f(6,24,4);
  255.  
  256. glColor3d(0,0,1);
  257. glVertex3f(6,20,4);
  258. glVertex3f(4,20,4);
  259. glVertex3f(4,24,4);
  260. glVertex3f(6,24,4);
  261.  
  262. glColor3d(1,0,1);
  263. glVertex3f(4,24,4);
  264. glVertex3f(4,20,4);
  265. glVertex3f(4,20,8);
  266. glVertex3f(4,24,8);
  267.  
  268. glColor3d(1,1,0);
  269. glVertex3f(6,24,4);
  270. glVertex3f(4,24,4);
  271. glVertex3f(4,24,8);
  272. glVertex3f(6,24,8);
  273.  
  274.  
  275. glColor3d(0,1,1);
  276. glVertex3f(6,20,8);
  277. glVertex3f(4,20,8);
  278. glVertex3f(4,20,4);
  279. glVertex3f(6,20,4);
  280.  
  281.  
  282. glEnd();
  283.  
  284.  
  285. }
  286. static void display(void)
  287. {
  288. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  289. const double a = t*90.0;
  290.  
  291. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  292. glColor3d(1,0,0);
  293.  
  294.  
  295. glPushMatrix();
  296. glTranslated(-2,-4,-30);
  297. glRotatef(degree,0,1,0);
  298. glRotatef(deg,0,0,1);
  299. glRotatef(deg2,1,0,0);
  300. glScalef(.3, .3, .3);
  301. Cube();
  302. glPopMatrix();
  303.  
  304. glutSwapBuffers();
  305. }
  306.  
  307.  
  308.  
  309. static void key(unsigned char key, int x, int y)
  310. {
  311. switch (key)
  312. {
  313. case 27 :
  314. case 'q':
  315. exit(0);
  316. break;
  317.  
  318. case 'y' :
  319. degree += 3.5;
  320. break;
  321.  
  322. case 'z' :
  323. deg += 3.5;
  324. break;
  325.  
  326. case 'x' :
  327. deg2 += 3.5;
  328. break;
  329.  
  330.  
  331.  
  332. case '+':
  333. slices++;
  334. stacks++;
  335. break;
  336.  
  337. case '-':
  338. if (slices>3 && stacks>3)
  339. {
  340. slices--;
  341. stacks--;
  342. }
  343. break;
  344. }
  345.  
  346. glutPostRedisplay();
  347. }
  348.  
  349. static void idle(void)
  350. {
  351. glutPostRedisplay();
  352. }
  353.  
  354. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  355. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  356. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  357. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  358.  
  359. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  360. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  361. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  362. const GLfloat high_shininess[] = { 100.0f };
  363.  
  364. /* Program entry point */
  365.  
  366. int main(int argc, char *argv[])
  367. {
  368. glutInit(&argc, argv);
  369. glutInitWindowSize(640,480);
  370. glutInitWindowPosition(10,10);
  371. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  372.  
  373. glutCreateWindow("GLUT Shapes");
  374.  
  375. glutReshapeFunc(resize);
  376. glutDisplayFunc(display);
  377. glutKeyboardFunc(key);
  378. glutIdleFunc(idle);
  379.  
  380. glClearColor(1,1,1,1);
  381. glEnable(GL_CULL_FACE);
  382. glCullFace(GL_BACK);
  383.  
  384. glEnable(GL_DEPTH_TEST);
  385. glDepthFunc(GL_LESS);
  386.  
  387. glutMainLoop();
  388.  
  389. return EXIT_SUCCESS;
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement