home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / xlib / overlay.c.z / overlay.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  5.3 KB  |  212 lines

  1. /*
  2.  * overlay.c - simple double buffered RGBA xlib program which rotates an object.
  3.  *    An overlay window is created as a child of the main window and it is
  4.  *    managed as part of the parent window.  The overlay window/context
  5.  *    creation uses the visual_info extension to find a visual with
  6.  *    a transparent pixel.
  7.  */
  8. /* compile: cc -o overlay overlay.c -lGLU -lGL -lX11 */
  9.  
  10. #include <GL/glx.h>
  11. #include <GL/glu.h>
  12. #include <X11/keysym.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15.  
  16. static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_DOUBLEBUFFER, None };
  17. static int ovAttributeList[] = { GLX_BUFFER_SIZE, 2,
  18.                  GLX_LEVEL, 1,
  19.                  GLX_TRANSPARENT_TYPE_EXT, GLX_TRANSPARENT_INDEX_EXT,
  20.                  None };
  21. static Window win, ovwin;
  22. static GLXContext cx, ovcx;
  23. static Display *dpy;
  24.  
  25. static Bool animate;
  26.  
  27. static void
  28. initialize(void) {
  29.     glShadeModel(GL_FLAT);
  30.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  31.     gluPerspective(40., 3.0/2.0, 0.001, 100000.0);
  32.     glTranslatef(0.0, 0.0, -3.0);
  33.     glClearColor(0.2,0.2,0.2,0.);
  34. }
  35.  
  36. static void
  37. side(void) { /* make a square translated 0.5 in the z direction */
  38.     glPushMatrix();
  39.     glTranslatef(0.0,0.0,0.5);
  40.     glRectf(-0.5,-0.5,0.5,0.5);
  41.     glPopMatrix();
  42. }
  43.  
  44. static void
  45. cube(void) { /* make a cube out of 4 squares */
  46.     glPushMatrix();
  47.     side();
  48.     glRotatef(90.,1.,0.,0.);
  49.     side();
  50.     glRotatef(90.,1.,0.,0.);
  51.     side();
  52.     glRotatef(90.,1.,0.,0.);
  53.     side();
  54.     glPopMatrix();
  55. }
  56.  
  57. static void
  58. draw_scene(void) {
  59.     static float rot = 0.;
  60.  
  61.     glClear(GL_COLOR_BUFFER_BIT);
  62.     glColor3f(.1, .1, .8);
  63.     glPushMatrix();
  64.     if (animate && (rot += 5.) > 360.) rot -= 360.;
  65.     glRotatef(rot,0.,1.,0.);
  66.     cube();
  67.     glScalef(0.3,0.3,0.3);
  68.     glColor3f(.8, .8, .1);
  69.     cube();
  70.     glPopMatrix();
  71. }
  72.  
  73. /*
  74.  * the amount of work being done doesn't justify the need for
  75.  * a separate overlay initialization and redraw routines so we
  76.  * do it all together. XXX doesn't allocate and initialize any
  77.  * color cells.
  78.  */
  79. static void
  80. initialize_ov(void) {
  81.     int i;
  82.     /* simply draw a grid of 10 equally spaced lines */
  83.     glLoadIdentity();
  84.     glOrtho(0., 11., 0., 11., -1., 1.);
  85.     glClearIndex(0.); /* transparent value is always zero */
  86.     glClear(GL_COLOR_BUFFER_BIT);
  87.     glIndexi(1);
  88.     glBegin(GL_LINES);
  89.     for(i = 0; i < 11; i++) {
  90.     glVertex2f(0.5+i, 0.0);
  91.     glVertex2f(0.5+i, 11.0);
  92.     glVertex2f(0.0,  0.5+i);
  93.     glVertex2f(11.0, 0.5+i);
  94.     }
  95.     glEnd();
  96. }
  97.  
  98. static Bool
  99. process_input(void) {
  100.     XEvent event;
  101.     static Bool mapped;
  102.     Bool viewport_ov = 0, redraw_ov = 0, redraw = 0;
  103.     GLsizei w, h;
  104.  
  105.     if(XPending(dpy) || !mapped || !animate) {
  106.     char buf[31];
  107.     KeySym keysym;
  108.  
  109.     XNextEvent(dpy, &event);
  110.     switch(event.type) {
  111.     case Expose:
  112.         redraw_ov = redraw = 1;
  113.         break;
  114.     case ConfigureNotify:
  115.         glViewport(0, 0, w = event.xconfigure.width, h = event.xconfigure.height);
  116.         redraw_ov = redraw = 1;
  117.         viewport_ov = 1;
  118.         break;
  119.     case MapNotify:
  120.         mapped = 1; break;
  121.     case UnmapNotify:
  122.         mapped = 0; break;
  123.     case KeyPress:
  124.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  125.         switch (keysym) {
  126.         case XK_Escape:
  127.         exit(EXIT_SUCCESS);
  128.         case XK_space:
  129.         animate ^= 1; break;
  130.         default:
  131.         break;
  132.         }
  133.     default:
  134.         break;
  135.     }
  136.     }
  137.     if (redraw_ov) {
  138.     glXMakeCurrent(dpy, ovwin, ovcx);
  139.     if (viewport_ov) {
  140.         XResizeWindow(dpy, ovwin, w, h);
  141.         glViewport(0, 0, w, h);
  142.     }
  143.     initialize_ov();
  144.     glXMakeCurrent(dpy, win, cx);
  145.     }
  146.     return redraw | animate;
  147. }
  148.  
  149. int
  150. main(int argc, char **argv) {
  151.     XVisualInfo *vi, *ovi;
  152.     XSetWindowAttributes swa;
  153.     Bool isdirect;
  154.  
  155.     dpy = XOpenDisplay(0);
  156.  
  157.     if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
  158.     fprintf(stderr, "overlay: no suitable RGB visual available\n");
  159.     exit(EXIT_FAILURE);
  160.     }
  161.  
  162.     /* create a GLX context */
  163.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  164.     isdirect = glXIsDirect(dpy, cx);
  165.  
  166.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  167.                                    vi->visual, AllocNone);
  168.  
  169.     swa.border_pixel = 0;
  170.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  171.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300,
  172.             0, vi->depth, InputOutput, vi->visual,
  173.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  174.     XStoreName(dpy, win, "overlay");
  175.     XMapWindow(dpy, win);
  176.  
  177.     glXMakeCurrent(dpy, win, cx);
  178.  
  179.     initialize();
  180.  
  181.     /* now set up overlay window */
  182.  
  183.     if (!(ovi = glXChooseVisual(dpy, DefaultScreen(dpy), ovAttributeList))) {
  184.     fprintf(stderr, "overlay: no suitable overlay index visual available\n");
  185.     exit(EXIT_FAILURE);
  186.     }
  187.     ovcx = glXCreateContext(dpy, ovi, 0, GL_TRUE);
  188.  
  189.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, ovi->screen),
  190.                                    ovi->visual, AllocNone);
  191.     swa.border_pixel = 0;
  192.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  193.     ovwin = XCreateWindow(dpy, win, 0, 0, 300, 300,
  194.             0, ovi->depth, InputOutput, ovi->visual,
  195.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  196.     XMapWindow(dpy, ovwin);
  197.  
  198.     glXMakeCurrent(dpy, ovwin, ovcx);
  199.     initialize_ov();
  200.     glXMakeCurrent(dpy, win, cx);
  201.  
  202.     printf("hit 'spacebar' to toggle animation\n");
  203.  
  204.     while (1) {
  205.     if (process_input()) {
  206.         draw_scene();
  207.         glXSwapBuffers(dpy, win);
  208.         if (!isdirect) glFinish();
  209.     }
  210.     }
  211. }
  212.