Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //compile = gcc -o animate animate.c -lX11 -lGL -lGLU && ./animate
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<X11/X.h>
  5. #include<X11/Xlib.h>
  6. #include<GL/gl.h>
  7. #include<GL/glx.h>
  8. #include<GL/glu.h>
  9. #include<GL/glut.h>
  10.  
  11. Display                 *dpy;
  12. Window                  root;
  13. GLint                   att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
  14. XVisualInfo             *vi;
  15. Colormap                cmap;
  16. XSetWindowAttributes    swa;
  17. Window                  win;
  18. GLXContext              glc;
  19. XWindowAttributes       gwa;
  20. XEvent                  xev;
  21.  
  22. float randFloat(float a, float b){
  23.     float temp = ((b-a)*((float)rand()/RAND_MAX))+a;
  24.     return temp;
  25. }
  26.  
  27. void DrawAQuad(float xpos, float ypos, float zpos, float scale,float r, float g, float b) {
  28.    
  29. glBegin(GL_QUADS);// depan
  30.   glColor3f(r, g, 0.5);
  31.   glVertex3f(xpos + 0, ypos + 0, zpos + 0.);
  32.   glVertex3f(xpos +  0, ypos + (.05*scale), zpos + 0.);
  33.   glVertex3f( xpos + (.05*scale),  ypos + (.05*scale), zpos + 0.);
  34.   glVertex3f(xpos + (.05*scale),  ypos + 0, zpos + 0.);
  35.  glEnd();
  36.  
  37.  glBegin(GL_QUADS);//kiri
  38.   glColor3f(r, 0.5, b);
  39.   glVertex3f(xpos + (.05*scale), ypos + .0, zpos + 0.);
  40.   glVertex3f( xpos + (.05*scale), ypos + (.05*scale), zpos + 0.);
  41.   glVertex3f( xpos + (.05*scale),  ypos + (.05*scale), zpos + (.05*scale));
  42.   glVertex3f(xpos + (.05*scale),  ypos + .0, zpos + (.05*scale));
  43.  glEnd();
  44.  
  45.  glBegin(GL_QUADS);//atas
  46.   glColor3f(0.5, g, b);
  47.   glVertex3f(xpos + 0., ypos + (.05*scale), zpos + 0.);
  48.   glVertex3f( xpos + .0, ypos + (.05*scale), zpos + (.05*scale));
  49.   glVertex3f( xpos + (.05*scale),  ypos + (.05*scale), zpos + (.05*scale));
  50.   glVertex3f(xpos + (.05*scale),  ypos + (.05*scale), zpos + 0.);
  51.  glEnd();
  52.  glBegin(GL_QUADS);//kanan
  53.   glColor3f(0.123, 0.923, 0.523);
  54.   glVertex3f(xpos + 0., ypos + 0., zpos + 0.);
  55.   glVertex3f( xpos + .0, ypos + (.05*scale), zpos + 0.);
  56.   glVertex3f( xpos + .0,  ypos + (.05*scale), zpos + (.05*scale));
  57.   glVertex3f(xpos + .0,  ypos + .0, zpos + (.05*scale));
  58.  glEnd();
  59.  glBegin(GL_QUADS);//belakang
  60.   glColor3f(0.302, 0.212, 0.75);
  61.   glVertex3f(xpos + 0., ypos + 0.0, zpos + (.05*scale));
  62.   glVertex3f( xpos + .0, ypos + (.05*scale), zpos + (.05*scale));
  63.   glVertex3f( xpos + (.05*scale),  ypos + (.05*scale), zpos + (.05*scale));
  64.   glVertex3f(xpos + (.05*scale),  ypos + .0, zpos + (.05*scale));
  65.  glEnd();
  66.  glBegin(GL_QUADS);//bawah
  67.   glColor3f(0.88, 0.77, 0.66);
  68.   glVertex3f(xpos + 0., ypos + 0., zpos + 0.);
  69.   glVertex3f( xpos + .0, ypos + .0, zpos + (.05*scale));
  70.   glVertex3f( xpos + (.05*scale),  ypos + .0, zpos + (.05*scale));
  71.   glVertex3f(xpos + (.05*scale),  ypos + .0, zpos + 0.);
  72.  glEnd();
  73.     }
  74.  
  75. int main(int argc, char *argv[]) {
  76.  
  77.  dpy = XOpenDisplay(NULL);
  78.  
  79.  if(dpy == NULL) {
  80.     printf("\n\tcannot connect to X server\n\n");
  81.         exit(0); }
  82.        
  83.  root = DefaultRootWindow(dpy);
  84.  
  85.  vi = glXChooseVisual(dpy, 0, att);
  86.  
  87.  if(vi == NULL) {
  88.     printf("\n\tno appropriate visual found\n\n");
  89.         exit(0); }
  90.  else {
  91.     printf("\n\tvisual %p selected\n", (void *)vi->visualid); }/* %p creates hexadecimal output like in glxinfo */
  92.  
  93.  
  94.  cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
  95.  
  96.  swa.colormap = cmap;
  97.  swa.event_mask = ExposureMask | KeyPressMask;
  98.  
  99.  win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
  100.  
  101.  XMapWindow(dpy, win);
  102.  XStoreName(dpy, win, "VERY SIMPLE APPLICATION");
  103.  
  104.  glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
  105.  glXMakeCurrent(dpy, win, glc);
  106.  
  107.  glEnable(GL_DEPTH_TEST);
  108.  float tambah = 0.0;
  109.  float nilai = 0.01;
  110.  int hitung = 0;
  111.  int nilai_int = 1;
  112.  int batas = 50;
  113.  
  114.  while(1) {
  115.        
  116.         XGetWindowAttributes(dpy, win, &gwa);
  117.         glViewport(0, 0, gwa.width, gwa.height);
  118.         // start draw here
  119.         glClearColor(.0 - tambah, .5 + tambah, .0 - tambah, .0);
  120.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  121.  
  122.         glMatrixMode(GL_PROJECTION);
  123.         glLoadIdentity();
  124.         glOrtho(-1., 1., -1., 1., 1., 20.);
  125.  
  126.         glMatrixMode(GL_MODELVIEW);
  127.         glLoadIdentity();
  128.         gluLookAt( 1.5 +(tambah*10), 5.0 , 5., 0., 0., 0., 0., 1., 0.);
  129.        
  130.         DrawAQuad(.0 + tambah, .0, .0, 5, 0.2,0.098,0.2+tambah);
  131.         DrawAQuad(.0 - tambah, .0, .0, 5, 0.2,0.098+tambah,0.2);
  132.         DrawAQuad(.0, .0 +tambah, .0,5 , 0.2+tambah,0.098,0.2);
  133.         DrawAQuad(.0 , .0 - tambah, .0, 5, 0.2+tambah,0.098+tambah,0.2);
  134.         DrawAQuad(.0, .0, .0 + tambah, 5, 0.2,0.098+tambah,0.2+tambah);
  135.         DrawAQuad(.0, .0, .0 - tambah, 5, 0.2+tambah,0.098+tambah,0.2+tambah);
  136.         tambah += nilai;
  137.         hitung += nilai_int;
  138.         //printf("tambah %f \n",tambah);
  139.         //printf("batas %f \n",batas);
  140.         if(hitung == batas){
  141.             nilai_int = -(nilai_int);
  142.             nilai = -(nilai);
  143.             batas = -(batas);
  144.         }
  145.        
  146.            
  147.         // end draw
  148.         glXSwapBuffers(dpy, win);
  149.                
  150.     } /* this closes while(1) { */
  151. } /* this is the } which closes int main(int argc, char *argv[]) { */