Guest User

http://stackoverflow.com/

a guest
Jul 21st, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <X11/X.h>
  4. #include <X11/Xlib.h>
  5. #include <GL/gl.h>
  6. #include <GL/glx.h>
  7. #include <GL/glu.h>
  8.  
  9.  
  10. Display                 *dpy;
  11. Window                  root;
  12. GLint                   att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
  13. XVisualInfo             *vi;
  14. Colormap                cmap;
  15. XSetWindowAttributes    swa;
  16. Window                  win;
  17. GLXContext              glc;
  18. XWindowAttributes       gwa;
  19. XEvent                  xev;
  20.  
  21.  
  22.  
  23. void DrawAQuad() {
  24.  glClearColor(1.0, 1.0, 1.0, 1.0);
  25.  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  26.  
  27.  glMatrixMode(GL_PROJECTION);
  28.  glLoadIdentity();
  29.  glOrtho(-1., 1., -1., 1., 1., 20.);
  30.  
  31.  glMatrixMode(GL_MODELVIEW);
  32.  glLoadIdentity();
  33.  gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);
  34.  
  35.  glBegin(GL_QUADS);
  36.   glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.);
  37.   glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.);
  38.   glColor3f(0., 0., 1.); glVertex3f( .75,  .75, 0.);
  39.   glColor3f(1., 1., 0.); glVertex3f(-.75,  .75, 0.);
  40.  glEnd(); }
  41.  
  42. int main(int argc, char *argv[]) {
  43.  
  44.  dpy = XOpenDisplay(NULL);
  45.  
  46.  if(dpy == NULL) {
  47.     printf("\n\tcannot connect to X server\n\n");
  48.         exit(0); }
  49.  
  50.  root = DefaultRootWindow(dpy);
  51.  
  52.  vi = glXChooseVisual(dpy, 0, att);
  53.  
  54.  if(vi == NULL) {
  55.     printf("\n\tno appropriate visual found\n\n");
  56.         exit(0); }
  57.  else {
  58.     printf("\n\tvisual %p selected\n", (void *)vi->visualid); }/* %p creates hexadecimal output like in glxinfo */
  59.  
  60.  
  61.  cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
  62.  
  63.  swa.colormap = cmap;
  64.  swa.event_mask = ExposureMask | KeyPressMask;
  65.  
  66.  win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
  67.  
  68.  XMapWindow(dpy, win);
  69.  XStoreName(dpy, win, "VERY SIMPLE APPLICATION");
  70.  
  71.  glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
  72.  glXMakeCurrent(dpy, win, glc);
  73.  
  74.  glEnable(GL_DEPTH_TEST);
  75.  
  76.  while(1) {
  77.     XNextEvent(dpy, &xev);
  78.  
  79.         if(xev.type == Expose) {
  80.             XGetWindowAttributes(dpy, win, &gwa);
  81.                 glViewport(0, 0, gwa.width, gwa.height);
  82.             DrawAQuad();
  83.                 glXSwapBuffers(dpy, win); }
  84.  
  85.     else if(xev.type == KeyPress) {
  86.             glXMakeCurrent(dpy, None, NULL);
  87.         glXDestroyContext(dpy, glc);
  88.         XDestroyWindow(dpy, win);
  89.         XCloseDisplay(dpy);
  90.         exit(0); }
  91.     } /* this closes while(1) { */
  92. } /* this is the } which closes int main(int argc, char *argv[]) { */
Add Comment
Please, Sign In to add comment