//compile = gcc -o animate animate.c -lX11 -lGL -lGLU && ./animate
#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include<GL/gl.h>
#include<GL/glx.h>
#include<GL/glu.h>
#include<GL/glut.h>
Display *dpy;
Window root;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;
float randFloat(float a, float b){
float temp = ((b-a)*((float)rand()/RAND_MAX))+a;
return temp;
}
void DrawAQuad(float xpos, float ypos, float zpos, float scale,float r, float g, float b) {
glBegin(GL_QUADS);// depan
glColor3f(r, g, 0.5);
glVertex3f(xpos + 0, ypos + 0, zpos + 0.);
glVertex3f(xpos + 0, ypos + (.05*scale), zpos + 0.);
glVertex3f( xpos + (.05*scale), ypos + (.05*scale), zpos + 0.);
glVertex3f(xpos + (.05*scale), ypos + 0, zpos + 0.);
glEnd();
glBegin(GL_QUADS);//kiri
glColor3f(r, 0.5, b);
glVertex3f(xpos + (.05*scale), ypos + .0, zpos + 0.);
glVertex3f( xpos + (.05*scale), ypos + (.05*scale), zpos + 0.);
glVertex3f( xpos + (.05*scale), ypos + (.05*scale), zpos + (.05*scale));
glVertex3f(xpos + (.05*scale), ypos + .0, zpos + (.05*scale));
glEnd();
glBegin(GL_QUADS);//atas
glColor3f(0.5, g, b);
glVertex3f(xpos + 0., ypos + (.05*scale), zpos + 0.);
glVertex3f( xpos + .0, ypos + (.05*scale), zpos + (.05*scale));
glVertex3f( xpos + (.05*scale), ypos + (.05*scale), zpos + (.05*scale));
glVertex3f(xpos + (.05*scale), ypos + (.05*scale), zpos + 0.);
glEnd();
glBegin(GL_QUADS);//kanan
glColor3f(0.123, 0.923, 0.523);
glVertex3f(xpos + 0., ypos + 0., zpos + 0.);
glVertex3f( xpos + .0, ypos + (.05*scale), zpos + 0.);
glVertex3f( xpos + .0, ypos + (.05*scale), zpos + (.05*scale));
glVertex3f(xpos + .0, ypos + .0, zpos + (.05*scale));
glEnd();
glBegin(GL_QUADS);//belakang
glColor3f(0.302, 0.212, 0.75);
glVertex3f(xpos + 0., ypos + 0.0, zpos + (.05*scale));
glVertex3f( xpos + .0, ypos + (.05*scale), zpos + (.05*scale));
glVertex3f( xpos + (.05*scale), ypos + (.05*scale), zpos + (.05*scale));
glVertex3f(xpos + (.05*scale), ypos + .0, zpos + (.05*scale));
glEnd();
glBegin(GL_QUADS);//bawah
glColor3f(0.88, 0.77, 0.66);
glVertex3f(xpos + 0., ypos + 0., zpos + 0.);
glVertex3f( xpos + .0, ypos + .0, zpos + (.05*scale));
glVertex3f( xpos + (.05*scale), ypos + .0, zpos + (.05*scale));
glVertex3f(xpos + (.05*scale), ypos + .0, zpos + 0.);
glEnd();
}
int main(int argc, char *argv[]) {
dpy = XOpenDisplay(NULL);
if(dpy == NULL) {
printf("\n\tcannot connect to X server\n\n");
exit(0); }
root = DefaultRootWindow(dpy);
vi = glXChooseVisual(dpy, 0, att);
if(vi == NULL) {
printf("\n\tno appropriate visual found\n\n");
exit(0); }
else {
printf("\n\tvisual %p selected\n", (void *)vi->visualid); }/* %p creates hexadecimal output like in glxinfo */
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;
win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "VERY SIMPLE APPLICATION");
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);
glEnable(GL_DEPTH_TEST);
float tambah = 0.0;
float nilai = 0.01;
int hitung = 0;
int nilai_int = 1;
int batas = 50;
while(1) {
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
// start draw here
glClearColor(.0 - tambah, .5 + tambah, .0 - tambah, .0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 20.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 1.5 +(tambah*10), 5.0 , 5., 0., 0., 0., 0., 1., 0.);
DrawAQuad(.0 + tambah, .0, .0, 5, 0.2,0.098,0.2+tambah);
DrawAQuad(.0 - tambah, .0, .0, 5, 0.2,0.098+tambah,0.2);
DrawAQuad(.0, .0 +tambah, .0,5 , 0.2+tambah,0.098,0.2);
DrawAQuad(.0 , .0 - tambah, .0, 5, 0.2+tambah,0.098+tambah,0.2);
DrawAQuad(.0, .0, .0 + tambah, 5, 0.2,0.098+tambah,0.2+tambah);
DrawAQuad(.0, .0, .0 - tambah, 5, 0.2+tambah,0.098+tambah,0.2+tambah);
tambah += nilai;
hitung += nilai_int;
//printf("tambah %f \n",tambah);
//printf("batas %f \n",batas);
if(hitung == batas){
nilai_int = -(nilai_int);
nilai = -(nilai);
batas = -(batas);
}
// end draw
glXSwapBuffers(dpy, win);
} /* this closes while(1) { */
} /* this is the } which closes int main(int argc, char *argv[]) { */