home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / demos / glxpixmap.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  4KB  |  155 lines

  1. /* glxpixmap.c */
  2.  
  3.  
  4. /*
  5.  * A demonstration of using the GLXPixmap functions.  This program is in
  6.  * the public domain.
  7.  *
  8.  * Brian Paul
  9.  */
  10.  
  11.  
  12. #include <GL/gl.h>
  13. #include <GL/glx.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17.  
  18.  
  19. static GLXContext ctx;
  20. static XVisualInfo *visinfo;
  21. static GC gc;
  22.  
  23.  
  24.  
  25. static Window make_rgb_window( Display *dpy,
  26.                   unsigned int width, unsigned int height )
  27. {
  28.    int attrib[] = { GLX_RGBA,
  29.             GLX_RED_SIZE, 1,
  30.             GLX_GREEN_SIZE, 1,
  31.             GLX_BLUE_SIZE, 1,
  32.             None };
  33.    int scrnum;
  34.    XSetWindowAttributes attr;
  35.    unsigned long mask;
  36.    Window root;
  37.    Window win;
  38.  
  39.    scrnum = DefaultScreen( dpy );
  40.    root = RootWindow( dpy, scrnum );
  41.  
  42.    visinfo = glXChooseVisual( dpy, scrnum, attrib );
  43.    if (!visinfo) {
  44.       printf("Error: couldn't get an RGB, Double-buffered visual\n");
  45.       exit(1);
  46.    }
  47.  
  48.    /* window attributes */
  49.    attr.background_pixel = 0;
  50.    attr.border_pixel = 0;
  51.    /* TODO: share root colormap if possible */
  52.    attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  53.    attr.event_mask = StructureNotifyMask | ExposureMask;
  54.    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  55.  
  56.    win = XCreateWindow( dpy, root, 0, 0, width, height,
  57.                 0, visinfo->depth, InputOutput,
  58.                 visinfo->visual, mask, &attr );
  59.  
  60.    /* make an X GC so we can do XCopyArea later */
  61.    gc = XCreateGC( dpy, win, 0, NULL );
  62.  
  63.    ctx = glXCreateContext( dpy, visinfo, NULL, True );
  64.  
  65.    return win;
  66. }
  67.  
  68.  
  69. static GLXPixmap make_pixmap( Display *dpy, Window win,
  70.                    unsigned int width, unsigned int height )
  71. {
  72.    Pixmap pm;
  73.    GLXPixmap glxpm;
  74.    XWindowAttributes attr;
  75.  
  76.    pm = XCreatePixmap( dpy, win, width, height, visinfo->depth );
  77.    XGetWindowAttributes( dpy, win, &attr );
  78.  
  79.    /*
  80.     * IMPORTANT:
  81.     *   Use the glXCreateGLXPixmapMESA funtion when using Mesa because
  82.     *   Mesa needs to know the colormap associated with a pixmap in order
  83.     *   to render correctly.  This is because Mesa allows RGB rendering
  84.     *   into any kind of visual, not just TrueColor or DirectColor.
  85.     */
  86. #ifdef GLX_MESA_pixmap_colormap
  87.    glxpm = glXCreateGLXPixmapMESA( dpy, visinfo, pm, attr.colormap );
  88. #else
  89.    /* This will work with Mesa too if the visual is TrueColor or DirectColor */
  90.    glxpm = glXCreateGLXPixmap( dpy, visinfo, pm );
  91. #endif
  92.  
  93.    return glxpm;
  94. }
  95.  
  96.  
  97.  
  98. static void event_loop( Display *dpy, GLXPixmap pm )
  99. {
  100.    XEvent event;
  101.  
  102.    while (1) {
  103.       XNextEvent( dpy, &event );
  104.  
  105.       switch (event.type) {
  106.      case Expose:
  107.         printf("Redraw\n");
  108.         /* copy the image from GLXPixmap to window */
  109.         XCopyArea( dpy, pm, event.xany.window,  /* src, dest */
  110.                gc, 0, 0, 300, 300,          /* gc, src pos, size */
  111.                0, 0 );                      /* dest pos */
  112.         break;
  113.      case ConfigureNotify:
  114.         /* nothing */
  115.         break;
  116.       }
  117.    }
  118. }
  119.  
  120.  
  121.  
  122. main( int argc, char *argv[] )
  123. {
  124.    Display *dpy;
  125.    Window win;
  126.    GLXPixmap pm;
  127.  
  128.    dpy = XOpenDisplay(NULL);
  129.  
  130.    win = make_rgb_window( dpy, 300, 300 );
  131.    pm = make_pixmap( dpy, win, 300, 300 );
  132.  
  133. #ifdef JUNK
  134.    glXMakeCurrent( dpy, win, ctx );  /*to make sure ctx is properly initialized*/
  135. #endif
  136.  
  137.    glXMakeCurrent( dpy, pm, ctx );
  138.  
  139.    /* Render an image into the pixmap */
  140.    glShadeModel( GL_FLAT );
  141.    glClearColor( 0.5, 0.5, 0.5, 1.0 );
  142.    glClear( GL_COLOR_BUFFER_BIT );
  143.    glViewport( 0, 0, 300, 300 );
  144.    glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  145.    glColor3f( 0.0, 1.0, 1.0 );
  146.    glRectf( -0.75, -0.75, 0.75, 0.75 );
  147.    glFlush();
  148.  
  149.    /* when a redraw is needed we'll just copy the pixmap image to the window */
  150.  
  151.    XMapWindow( dpy, win );
  152.  
  153.    event_loop( dpy, pm );
  154. }
  155.