Advertisement
RayhanReynara31

animasi 3D mobil

Dec 12th, 2019
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <math.h>
  3.  
  4. void cylinder(float alas, float atas, float tinggi);
  5. void kerucut(float ngisor, float nduwur, float dowo);
  6. void blok(float tebal, int ratiol, int ratiop);
  7.  
  8. GLfloat source_light[] = { 0.30,0.30,0.30,0.50 };
  9. double Loop, LoopAll = 0;
  10. bool on = false;
  11.  
  12.  
  13. void Mobil(void)
  14. {
  15. glClearColor(0.0, 1.0, 1.0, 1.0); //background
  16. glShadeModel(GL_SMOOTH);
  17. glMatrixMode(GL_PROJECTION);
  18. glLoadIdentity();
  19. gluPerspective(50.0, 1.5, 10.0, 1000.0);
  20.  
  21. glEnable(GL_DEPTH_TEST);
  22. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  23. glEnable(GL_LIGHTING);
  24. glLightfv(GL_LIGHT7, GL_DIFFUSE, source_light);
  25. glEnable(GL_LIGHT7);
  26. glEnable(GL_COLOR_MATERIAL);
  27. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
  28.  
  29. }
  30.  
  31. void keyboard_s(int key, int x, int y)
  32. {
  33.  
  34. if (key == GLUT_KEY_F1)
  35. {
  36. if (on == false)
  37. {
  38. on = true;
  39. }
  40. else
  41. {
  42. on = false;
  43. }
  44. }
  45.  
  46. }
  47.  
  48.  
  49.  
  50. void display(void)
  51. {
  52.  
  53. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  54. glMatrixMode(GL_MODELVIEW);
  55. glLoadIdentity();
  56.  
  57. glTranslatef(0, 0, -100);// besar kecilnya mobil
  58.  
  59. Loop++;
  60. glRotatef(Loop, 0.9, 4.0, 0.6);//mobil yang berotasi
  61. glPushMatrix();
  62.  
  63. glColor3f(0.1, 0.1,1.0);
  64. blok(10, 3, 2);
  65. glTranslatef(0, 9, 0);
  66. blok(10, 3, 2);
  67. glTranslatef(10, -10, 0);
  68. blok(10, 5.5, 2);
  69. glRotatef(-35, 0, 0, 15);
  70. glTranslatef(0, 7, 0);
  71. blok(10, 2, 2);
  72. glTranslatef(2, 4.9, -2.5);
  73. glColor3f(1.0, 1.0, 1.0);// warna kaca
  74. blok(0.5, 20, 31);
  75.  
  76. glRotatef(180, 45, -45, 0);
  77. /*glTranslatef(0, 10,0);
  78. //blok(3, 2, 5);
  79. cylinder(2,2,30);
  80. glTranslatef(-10, -10,0);
  81. glRotatef(90, 45,-45, 0);
  82. cylinder(2,2,30);
  83. glRotatef(90, -45, 90, 90);
  84. blok(5, 3, 2);*/
  85.  
  86. glPopMatrix();
  87.  
  88.  
  89. glPushMatrix();//roda
  90. glColor3f(0.0, 0.0, 0.0);
  91. glTranslatef(20, -8, -7);
  92. cylinder(5, 5, 3);
  93.  
  94. glColor3f(0.0, 0.0, 0.0);
  95. glTranslatef(-20, 8, 7);
  96. glTranslatef(-5, -8, -7);
  97. cylinder(5, 5, 3);
  98.  
  99. glColor3f(0.0, 0.0, 0.0);
  100. glTranslatef(5, 8, 7);
  101. glRotatef(180, 0, 180, 0);
  102. glTranslatef(3, -8, -17);
  103. cylinder(5, 5, 3);
  104.  
  105. glColor3f(0.0, 0.0, 0.0);
  106. glTranslatef(-3, 8, 17);
  107. glTranslatef(-22, -8, -17);
  108. cylinder(5, 5, 3);
  109.  
  110. glColor3f(1.0, 1.0, 1.0);
  111.  
  112. glRotatef(90, 1, 0, 0);
  113. glTranslatef(8, 2.5, -15);
  114. blok(2, 4, 5);
  115.  
  116.  
  117. glRotatef(90, 0, 1, 0);
  118. glTranslatef(0, -0.2, 7);
  119. blok(2, 4, 8);
  120.  
  121.  
  122. glRotatef(0, 0, 0, 0);
  123. glTranslatef(0, 19.2, 0);
  124. blok(2, 4, 8);
  125.  
  126. glRotatef(90, 0, 1, 0);
  127. glTranslatef(7, 0, -8);
  128. blok(2, 4, 5);
  129.  
  130. glColor3f(9.9, 9.9, 0.0);//lampu
  131. glRotatef(90, 0, 1, 0);
  132. glTranslatef(0, -3, 20);
  133. cylinder(2, 2, 3);
  134.  
  135. glColor3f(9.9, 9.9, 0.0);//lampu
  136. glRotatef(0, 0, 0, 0);
  137. glTranslatef(0, -12, 0);
  138. cylinder(2, 2, 3);
  139.  
  140. glColor3f(1, 0.0, 0.0);//knalpot
  141. glRotatef(0, 0, 0, 0);
  142. glTranslatef(0, 0, -52);
  143. cylinder(1, 1, 3);
  144.  
  145. glColor3f(1.0, 1.0, 1.0);
  146. glRotatef(90, 1, 0, 0);
  147. glTranslatef(-8, 3.5, -12);
  148. blok(2, 4, 8);
  149.  
  150. glColor3f(9.0, 0.0, 0.0);//lampu atas
  151. glRotatef(0, 0, 0, 0);
  152. glTranslatef(-8, 28, 0);
  153. cylinder(1, 1, 12);
  154.  
  155.  
  156. glPopMatrix();
  157.  
  158.  
  159. glFlush();
  160. glutSwapBuffers();
  161.  
  162. }
  163.  
  164. void kerucut(float ngisor, float nduwur, float dowo)
  165. {
  166. float i;
  167. glPushMatrix();
  168. glTranslatef(1.0, 0.0, ngisor / 24);
  169. glutSolidCone(ngisor, 0, 32, 4);
  170. for (i = 0; i <= dowo; i += ngisor / 24)
  171. {
  172. glTranslatef(0.0, 0.0, ngisor / 24);
  173. glutSolidTorus(ngisor / 4, ngisor - ((i * (ngisor - nduwur)) / dowo), 16, 16);
  174. }
  175. glTranslatef(0.0, 0.0, ngisor / 4);
  176. glutSolidCone(nduwur, 0, 20, 1);
  177. glColor3f(0., 1., 1.);
  178. glPopMatrix();
  179. }
  180.  
  181.  
  182. void cylinder(float alas, float atas, float tinggi)
  183. {
  184. float i;
  185. glPushMatrix();
  186. glTranslatef(1.0, 0.0, -alas / 8);
  187. glutSolidCone(alas, 0, 32, 4);
  188. for (i = 0; i <= tinggi; i += alas / 24)
  189. {
  190. glTranslatef(0.0, 0.0, alas / 24);
  191. glutSolidTorus(alas / 4, alas - ((i * (alas - atas)) / tinggi), 16, 16);
  192. }
  193. glTranslatef(0.0, 0.0, alas / 4);
  194. glutSolidCone(atas, 0, 20, 1);
  195. glColor3f(1., 0., 0.);
  196. glPopMatrix();
  197. }
  198.  
  199. void blok(float tebal, int ratiol, int ratiop)
  200. {
  201. float i, j;
  202. glPushMatrix();
  203. for (i = 0; i < ratiop; i++)
  204. {
  205. glTranslatef(-(ratiol + 1) * tebal / 2, 0.0, 0.0);
  206. for (j = 0; j < ratiol; j++)
  207. {
  208. glTranslatef(tebal, 0.0, 0.0);
  209. glutSolidCube(tebal);
  210. }
  211. glTranslatef(-(ratiol - 1) * tebal / 2, 0.0, tebal);
  212. }
  213. glPopMatrix();
  214. }
  215. void tampil()
  216. {
  217. display();
  218. }
  219.  
  220.  
  221. int main(int argc, char** argv)
  222. {
  223.  
  224.  
  225. glutInit(&argc, argv);
  226. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//|GLUT_DEPTH);
  227. glutInitWindowPosition(10, 10);
  228. glutInitWindowSize(1000, 700);
  229. glutCreateWindow("Rayhan 029");
  230. glClearColor(0.5, 0.5, 0.0, 0.0);
  231. glutDisplayFunc(display);
  232. gluOrtho2D(-320., 320., -240.0, 240.0);
  233. glutIdleFunc(tampil);
  234. glutSpecialFunc(keyboard_s);//biar bisa dikendalikan dengan keyboard (F1 untuk mati nyala)
  235. Mobil();
  236. glutMainLoop();
  237. return(0);
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement