Cara membuat boneka salju 3D di OpenGL


Berikut merupakan script lengkap membuat boneka salju 3D :

  1. #include <stdlib.h>
  2. #include <glut.h>
  3. int w=480, h=480, z=0;
  4. int x1=0, y1=0, sudut=0, z1=0;
  5.  
  6. void bawah ()
  7. {
  8.      glColor3ub(255, 255, 255);
  9.      glutWireSphere(5,100,100);
  10. }
  11. void tengah()
  12. {
  13.     glColor3ub(255, 255, 255);
  14.     glutWireSphere(4,100,100);
  15. }
  16. void atas()
  17. {
  18.     glColor3ub(255, 255, 255);
  19.     glutWireSphere(3,100,100);
  20. }
  21.  
  22. void matakanan()
  23. {
  24.     glColor3ub(0, 0, 0);
  25.     glutWireSphere(0.5,100,100);
  26. }
  27. void matakiri()
  28. {
  29.     glColor3ub(0, 0, 0);
  30.     glutWireSphere(0.5,100,100);
  31. }
  32. void topi()
  33. {
  34.     glColor3ub(0, 55, 252);
  35.     glutWireCone(3,5,100,100);
  36. }
  37.  
  38. void renderScene(void){
  39.  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  40.  glLoadIdentity();
  41.  
  42.  glPushMatrix();
  43.  glTranslatef(0,-10,z);
  44.  glRotatef(sudut,x1,y1,z1);
  45.  bawah();
  46.  glPopMatrix();
  47.  
  48.  glPushMatrix();
  49.  glTranslatef(0,-5,z);
  50.  glRotatef(sudut,x1,y1,z1);
  51.  tengah();
  52.  glPopMatrix();
  53.  
  54.  glPushMatrix();
  55.  glTranslatef(0,0,z);
  56.  glRotatef(sudut,x1,y1,z1);
  57.  atas();
  58.  glPopMatrix();
  59.  
  60.  glPushMatrix();
  61.  glTranslatef(1.5,0.5,z);
  62.  glRotatef(sudut,x1,y1,z1);
  63.  matakanan();
  64.  glPopMatrix();
  65.  
  66.  glPushMatrix();
  67.  glTranslatef(-1.5,0.5,z);
  68.  glRotatef(sudut,x1,y1,z1);
  69.  matakiri();
  70.  glPopMatrix();
  71.  
  72.  glPushMatrix();
  73.  glTranslatef(0,2,z);
  74.  glRotatef(280,100,y1,z1);
  75.  topi();
  76.  glPopMatrix();
  77.  
  78.  glutSwapBuffers();
  79. }
  80. void resize(int w1, int h1){
  81.  glViewport(0,0,w1,h1);
  82.  glMatrixMode(GL_PROJECTION);
  83.  glLoadIdentity();
  84.  gluPerspective(45.0,(float) w1/(float) h1, 1.0,300.0);
  85.  glMatrixMode(GL_MODELVIEW);
  86.  glLoadIdentity();
  87. }
  88.  
  89. void myKeyboard(unsigned char key, int x, int y)
  90. {
  91.  if (key =='a') z+=5;
  92.  else if (key == 'd') z-=5;
  93. }
  94. void init(){
  95.  glClearColor(0,0,0,1);
  96.  glEnable(GL_DEPTH_TEST);
  97.  glMatrixMode(GL_PROJECTION);
  98.  glLoadIdentity();
  99.  gluPerspective(45.0,(GLdouble) w/(GLdouble) h, 1.0,300.0);
  100.  glMatrixMode(GL_MODELVIEW);
  101. }
  102. void timer(int value){
  103.  glutPostRedisplay();
  104.  glutTimerFunc(50,timer,0);
  105. }
  106. void main (int argc, char **argv){
  107.  glutInit(&argc, argv);
  108.  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
  109.  glutInitWindowPosition(100,100);
  110.  glutInitWindowSize(w,h);
  111.  glutCreateWindow("Manusia Salju");
  112.  gluOrtho2D(-w/2,w/2,-h/2,h/2);
  113.  glutDisplayFunc(renderScene);
  114.  glutReshapeFunc(resize);
  115.  glutKeyboardFunc(myKeyboard);
  116.  glutTimerFunc(1,timer,0);
  117.  init();
  118.  glutMainLoop();
  119. }

Komentar

Postingan populer dari blog ini

Wide Area Network (Pengertian, Fungsi, Kelebihan dan Kekurangan)

Cara membuat Lingkaran dengan di OpenGL