home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src-glut / glut_overlay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  16.9 KB  |  542 lines

  1. /* Copyright (c) Mark J. Kilgard, 1996.  */
  2.  
  3. /* This program is freely distributable without licensing fees
  4.    and is provided without guarantee or warrantee expressed or
  5.    implied. This program is -not- in the public domain. */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <assert.h>
  11.  
  12. #if !defined(WIN32)
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/Xatom.h>  /* for XA_RGB_DEFAULT_MAP atom */
  16. #if defined (__vms)
  17. #include <X11/StdCmap.h>  /* for XmuLookupStandardColormap */
  18. #else
  19. #include <X11/Xmu/StdCmap.h>  /* for XmuLookupStandardColormap */
  20. #endif
  21. #endif /* !WIN32 */
  22.  
  23. #include <GL/glut.h>
  24. #include "glutint.h"
  25. #include "layerutil.h"
  26.  
  27. static Criterion requiredOverlayCriteria[] =
  28. {
  29.   {LEVEL, EQ, 1},       /* This entry gets poked in
  30.                            determineOverlayVisual. */
  31.   {TRANSPARENT, EQ, 1},
  32.   {XPSEUDOCOLOR, EQ, 1},
  33.   {RGBA, EQ, 0},
  34.   {BUFFER_SIZE, GTE, 1}
  35. };
  36. static int numRequiredOverlayCriteria = sizeof(requiredOverlayCriteria) / sizeof(Criterion);
  37. static int requiredOverlayCriteriaMask =
  38. (1 << LEVEL) | (1 << TRANSPARENT) | (1 << XSTATICGRAY) | (1 << RGBA) | (1 << CI_MODE);
  39.  
  40. static int
  41. checkOverlayAcceptability(XVisualInfo * vi, unsigned int mode)
  42. {
  43.   int value;
  44.  
  45.   /* Must support OpenGL. */
  46.   glXGetConfig(__glutDisplay, vi, GLX_USE_GL, &value);
  47.   if (!value)
  48.     return 1;
  49.  
  50.   /* Must be color index. */
  51.   glXGetConfig(__glutDisplay, vi, GLX_RGBA, &value);
  52.   if (value)
  53.     return 1;
  54.  
  55.   /* Must match single/double buffering request. */
  56.   glXGetConfig(__glutDisplay, vi, GLX_DOUBLEBUFFER, &value);
  57.   if (GLUT_WIND_IS_DOUBLE(mode) != (value != 0))
  58.     return 1;
  59.  
  60.   /* Must match mono/stereo request. */
  61.   glXGetConfig(__glutDisplay, vi, GLX_STEREO, &value);
  62.   if (GLUT_WIND_IS_STEREO(mode) != (value != 0))
  63.     return 1;
  64.  
  65.   /* Alpha and accumulation buffers incompatible with color
  66.      index. */
  67.   if (GLUT_WIND_HAS_ALPHA(mode) || GLUT_WIND_HAS_ACCUM(mode))
  68.     return 1;
  69.  
  70.   /* Look for depth buffer if requested. */
  71.   glXGetConfig(__glutDisplay, vi, GLX_DEPTH_SIZE, &value);
  72.   if (GLUT_WIND_HAS_DEPTH(mode) && (value <= 0))
  73.     return 1;
  74.  
  75.   /* Look for stencil buffer if requested. */
  76.   glXGetConfig(__glutDisplay, vi, GLX_STENCIL_SIZE, &value);
  77.   if (GLUT_WIND_HAS_STENCIL(mode) && (value <= 0))
  78.     return 1;
  79.  
  80. #if defined(GLX_VERSION_1_1) && defined(GLX_SGIS_multisample)
  81.   /* XXX Multisampled overlay color index??  Pretty unlikely. */
  82.   /* Look for multisampling if requested. */
  83.   if (__glutIsSupportedByGLX("GLX_SGIS_multisample"))
  84.     glXGetConfig(__glutDisplay, vi, GLX_SAMPLES_SGIS, &value);
  85.   else
  86.     value = 0;
  87.   if (GLUT_WIND_IS_MULTISAMPLE(mode) && (value <= 0))
  88.     return 1;
  89. #endif
  90.  
  91.   return 0;
  92. }
  93.  
  94. static XVisualInfo *
  95. getOverlayVisualInfoCI(unsigned int mode)
  96. {
  97. #if !defined(WIN32)
  98.   XLayerVisualInfo *vi;
  99.   XLayerVisualInfo template;
  100.   XVisualInfo *goodVisual, *returnVisual;
  101.   int nitems, i, j, bad;
  102.  
  103.   /* The GLX 1.0 glXChooseVisual is does not permit queries
  104.      based on pixel transparency (and GLX_BUFFER_SIZE uses
  105.      "smallest that meets" its requirement instead of "largest
  106.      that meets" that GLUT wants. So, GLUT implements its own
  107.      visual selection routine for color index overlays. */
  108.  
  109.   /* Try three overlay layers. */
  110.   for (i = 1; i <= 3; i++) {
  111.     template.vinfo.screen = __glutScreen;
  112.     template.vinfo.class = PseudoColor;
  113.     template.layer = i;
  114.     template.type = TransparentPixel;
  115.     vi = __glutXGetLayerVisualInfo(__glutDisplay,
  116.       VisualTransparentType | VisualScreenMask | VisualClassMask | VisualLayerMask,
  117.       &template, &nitems);
  118.     if (vi) {
  119.       /* Check list for acceptable visual meeting requirements
  120.          of requested display mode. */
  121.       for (j = 0; j < nitems; j++) {
  122.         bad = checkOverlayAcceptability(&vi[j].vinfo, mode);
  123.         if (bad) {
  124.           /* Set vi[j].vinfo.visual to mark it unacceptable. */
  125.           vi[j].vinfo.visual = NULL;
  126.         }
  127.       }
  128.  
  129.       /* Look through list to find deepest acceptable visual. */
  130.       goodVisual = NULL;
  131.       for (j = 0; j < nitems; j++) {
  132.         if (vi[j].vinfo.visual) {
  133.           if (goodVisual == NULL) {
  134.             goodVisual = &vi[j].vinfo;
  135.           } else {
  136.             if (goodVisual->depth < vi[j].vinfo.depth) {
  137.               goodVisual = &vi[j].vinfo;
  138.             }
  139.           }
  140.         }
  141.       }
  142.  
  143.       /* If a visual is found, clean up and return the visual. */
  144.       if (goodVisual) {
  145.         returnVisual = (XVisualInfo *) malloc(sizeof(XVisualInfo));
  146.         if (returnVisual) {
  147.           *returnVisual = *goodVisual;
  148.         }
  149.         XFree(vi);
  150.         return returnVisual;
  151.       }
  152.       XFree(vi);
  153.     }
  154.   }
  155. #endif /* !WIN32 */
  156.   return NULL;
  157. }
  158.  
  159. /* ARGSUSED */
  160. static XVisualInfo *
  161. getOverlayVisualInfoRGB(unsigned int mode)
  162. {
  163.  
  164.   /* XXX For now, transparent RGBA overlays are not supported
  165.      by GLUT.  RGBA overlays raise difficult questions about
  166.      what the transparent pixel (really color) value should be.
  167.  
  168.      Color index overlay transparency is "easy" because the
  169.      transparent pixel value does not affect displayable colors
  170.      (except for stealing one color cell) since colors are
  171.      determined by indirection through a colormap, and because
  172.      it is uncommon for arbitrary pixel values in color index to
  173.      be "calculated" (as can occur with a host of RGBA operations
  174.      like lighting, blending, etc) so it is easy to avoid the
  175.      transparent pixel value.
  176.  
  177.      Since it is typically easy to avoid the transparent pixel
  178.      value in color index mode, if GLUT tells the programmer what
  179.      pixel is transparent, then most program can easily avoid
  180.      generating that pixel value except when they intend
  181.      transparency.  GLUT returns whatever transparent pixel value
  182.      is provided by the system through glutGet(
  183.      GLUT_TRANSPARENT_INDEX).
  184.  
  185.      Theory versus practice for RGBA overlay transparency: In
  186.      theory, the reasonable thing is enabling overlay transparency
  187.      when an overlay pixel's destination alpha is 0 because this
  188.      allows overlay transparency to be controlled via alpha and all
  189.      visibile colors are permited, but no hardware I am aware of
  190.      supports this practice (and it requires destination alpha which
  191.      is typically optional and quite uncommon for overlay windows!). 
  192.  
  193.      In practice, the choice of  transparent pixel value is typically
  194.      "hardwired" into most graphics hardware to a single pixel value.
  195.      SGI hardware uses true black (0,0,0) without regard for the
  196.      destination alpha.  This is far from ideal because true black (a
  197.      common color that is easy to accidently generate) can not be
  198.      generated in an RGBA overlay. I am not sure what other vendors
  199.      do.
  200.  
  201.      Pragmatically, most of the typical things you want to do in the
  202.      overlays can be done in color index (rubber banding, pop-up
  203.      menus, etc.).  One solution for GLUT would be to simply
  204.      "advertise" what RGB triple (or possibly RGBA quadruple or
  205.      simply A alone) generates transparency.  The problem with this
  206.      approach is that it forces programmers to avoid whatever
  207.      arbitrary color various systems decide is transparent.  This is
  208.      a difficult burden to place on programmers that want to portably
  209.      make use of overlays.
  210.  
  211.      To actually support transparent RGBA overlays, there are really
  212.      two reaonsable options.  ONE: Simply mandate that true black is
  213.      the RGBA overlay transparent color (what IRIS GL did).  This is
  214.      nice for programmers since only one option, nice for existing
  215.      SGI hardware, bad for anyone (including SGI) who wants to
  216.      improve upon "true black" RGB transparency. 
  217.  
  218.      Or TWO: Provide a set of queriable "transparency types" (like
  219.      "true black" or "alpha == 0" or "true white" or even a queriable
  220.      transparent color).  This is harder for programmers, OK for
  221.      existing SGI hardware, and it leaves open the issue of what
  222.      other modes are reasonable.
  223.  
  224.      Option TWO seems the more general approach, but since hardware
  225.      designers will likely only implement a single mode (this is a
  226.      scan out issue where bandwidth is pressing issue), codifying
  227.      multiple speculative approaches nobody may ever implement seems
  228.      silly.  And option ONE fiats a suboptimal solution.
  229.  
  230.      Therefore, I defer any decision of how GLUT should support RGBA
  231.      overlay transparency and leave support for it unimplemented.
  232.      Nobody has been pressing me for RGBA overlay transparency
  233.      (though people have requested color index overlay transparency
  234.      repeatedly).  Geez, if you read this far you are either really
  235.      bored or maybe actually  interested in this topic.  Anyway, if
  236.      you have ideas (particularly if you plan on implementing a
  237.      hardware scheme for RGBA overlay transparency), I'd be
  238.      interested.
  239.  
  240.      For the record, SGI's expiremental Framebufer Configuration
  241.      experimental GLX extension uses option TWO.  Transparency modes
  242.      for "none" and "RGB" are defined (others could be defined
  243.      later).  What RGB value is the transparent one must be queried. 
  244.  
  245.      I was hoping GLUT could have something that required less work
  246.      from the programmer to use portably. -mjk */
  247.  
  248.   __glutWarning("RGBA overlays are not supported by GLUT (for now).");
  249.   return NULL;
  250. }
  251.  
  252. static XVisualInfo *
  253. getOverlayVisualInfo(unsigned int mode)
  254. {
  255.   /* XXX GLUT_LUMINANCE not implemented for GLUT 3.0. */
  256.   if (GLUT_WIND_IS_LUMINANCE(mode))
  257.     return NULL;
  258.  
  259.   if (GLUT_WIND_IS_RGB(mode))
  260.     return getOverlayVisualInfoRGB(mode);
  261.   else
  262.     return getOverlayVisualInfoCI(mode);
  263. }
  264.  
  265. static void
  266. addStaleWindow(GLUTwindow * window, Window win)
  267. {
  268.   GLUTstale *entry;
  269.  
  270.   entry = (GLUTstale *) malloc(sizeof(GLUTstale));
  271.   if (!entry)
  272.     __glutFatalError("out of memory");
  273.   entry->window = window;
  274.   entry->win = win;
  275.   entry->next = __glutStaleWindowList;
  276.   __glutStaleWindowList = entry;
  277. }
  278.  
  279. void 
  280. __glutFreeOverlay(GLUToverlay * overlay)
  281. {
  282.   if (overlay->visAlloced)
  283.     XFree(overlay->vis);
  284.   XDestroyWindow(__glutDisplay, overlay->win);
  285.   glXDestroyContext(__glutDisplay, overlay->ctx);
  286.   if (overlay->colormap) {
  287.     /* Only color index overlays have colormap data structure. */
  288.     __glutFreeColormap(overlay->colormap);
  289.   }
  290.   free(overlay);
  291. }
  292.  
  293. static XVisualInfo *
  294. determineOverlayVisual(int *treatAsSingle, Bool * visAlloced)
  295. {
  296.   if (__glutDisplayString) {
  297.     XVisualInfo *vi;
  298.     int i;
  299.  
  300.     /* __glutDisplayString should be NULL except if
  301.        glutInitDisplayString has been called to register a
  302.        different display string.  Calling glutInitDisplayString
  303.        means using a string instead of an integer mask determine 
  304.        the visual to use. Using the function pointer variable
  305.        __glutDetermineVisualFromString below avoids linking in
  306.        the code for implementing glutInitDisplayString (ie,
  307.        glut_dstr.o) unless glutInitDisplayString gets called by
  308.        the application. */
  309.  
  310.     assert(__glutDetermineVisualFromString);
  311.  
  312.     /* Try three overlay layers. */
  313.     *visAlloced = False;
  314.     for (i = 1; i <= 3; i++) {
  315.       requiredOverlayCriteria[0].value = i;
  316.       vi = __glutDetermineVisualFromString(__glutDisplayString, treatAsSingle,
  317.         requiredOverlayCriteria, numRequiredOverlayCriteria,
  318.     requiredOverlayCriteriaMask);
  319.       if (vi) {
  320.         return vi;
  321.       }
  322.     }
  323.     return NULL;
  324.   } else {
  325.     *visAlloced = True;
  326.     return __glutDetermineVisual(__glutDisplayMode,
  327.       treatAsSingle, getOverlayVisualInfo);
  328.   }
  329. }
  330.  
  331. /* CENTRY */
  332. void APIENTRY 
  333. glutEstablishOverlay(void)
  334. {
  335.   GLUToverlay *overlay;
  336.   GLUTwindow *window;
  337.   XSetWindowAttributes wa;
  338.  
  339.   /* Register a routine to free an overlay with glut_win.c;
  340.      this keeps glut_win.c from pulling in all of
  341.      glut_overlay.c when no overlay functionality is used by
  342.      the application. */
  343.   __glutFreeOverlayFunc = __glutFreeOverlay;
  344.  
  345.   window = __glutCurrentWindow;
  346.  
  347.   /* Allow for an existant overlay to be re-established perhaps
  348.      if you wanted a different display mode. */
  349.   if (window->overlay) {
  350.     addStaleWindow(window, window->overlay->win);
  351.     __glutFreeOverlay(window->overlay);
  352.   }
  353.   overlay = (GLUToverlay *) malloc(sizeof(GLUToverlay));
  354.   if (!overlay)
  355.     __glutFatalError("out of memory.");
  356.  
  357.   overlay->vis = determineOverlayVisual(&overlay->treatAsSingle,
  358.     &overlay->visAlloced);
  359.   if (!overlay->vis) {
  360.     __glutFatalError("lacks overlay support.");
  361.   }
  362.   overlay->ctx = glXCreateContext(__glutDisplay, overlay->vis,
  363.     None, __glutTryDirect);
  364.   overlay->isDirect = glXIsDirect(__glutDisplay, overlay->ctx);
  365.   if (__glutForceDirect) {
  366.     if (!overlay->isDirect) {
  367.       __glutFatalError("direct rendering not possible.");
  368.     }
  369.   }
  370.   __glutSetupColormap(overlay->vis, &overlay->colormap, &overlay->cmap,
  371.     GLUT_WIND_IS_RGB(__glutDisplayMode));
  372.   overlay->transparentPixel = __glutGetTransparentPixel(__glutDisplay,
  373.     overlay->vis);
  374.   wa.colormap = overlay->cmap;
  375.   wa.background_pixel = overlay->transparentPixel;
  376.   wa.event_mask = window->eventMask & GLUT_OVERLAY_EVENT_FILTER_MASK;
  377.   wa.border_pixel = 0;
  378.   overlay->win = XCreateWindow(__glutDisplay,
  379.     window->win,
  380.     0, 0, window->width, window->height, 0,
  381. #if defined(WIN32)
  382.     0, InputOutput, 0,
  383. #else
  384.     overlay->vis->depth, InputOutput, overlay->vis->visual,
  385. #endif
  386.     CWBackPixel | CWBorderPixel | CWEventMask | CWColormap,
  387.     &wa);
  388.   if (window->children) {
  389.     /* Overlay window must be lowered below any GLUT
  390.        subwindows. */
  391.     XLowerWindow(__glutDisplay, overlay->win);
  392.   }
  393.   XMapWindow(__glutDisplay, overlay->win);
  394.   overlay->shownState = 1;
  395.  
  396.   overlay->display = NULL;
  397.  
  398.   /* Make sure a reshape gets delivered. */
  399.   window->forceReshape = True;
  400.  
  401.   __glutPutOnWorkList(__glutToplevelOf(window), GLUT_COLORMAP_WORK);
  402.  
  403.   window->overlay = overlay;
  404.   glutUseLayer(GLUT_OVERLAY);
  405.  
  406.   if (overlay->treatAsSingle) {
  407.     glDrawBuffer(GL_FRONT);
  408.     glReadBuffer(GL_FRONT);
  409.   }
  410. }
  411.  
  412. void APIENTRY 
  413. glutRemoveOverlay(void)
  414. {
  415.   GLUTwindow *window = __glutCurrentWindow;
  416.   GLUToverlay *overlay = __glutCurrentWindow->overlay;
  417.  
  418.   if (!window->overlay)
  419.     return;
  420.  
  421.   /* If using overlay, switch to the normal layer. */
  422.   if (window->renderWin == overlay->win) {
  423.     glutUseLayer(GLUT_NORMAL);
  424.   }
  425.   addStaleWindow(window, overlay->win);
  426.   __glutFreeOverlay(overlay);
  427.   window->overlay = NULL;
  428.   __glutPutOnWorkList(__glutToplevelOf(window), GLUT_COLORMAP_WORK);
  429. }
  430.  
  431. void APIENTRY 
  432. glutUseLayer(GLenum layer)
  433. {
  434.   GLUTwindow *window = __glutCurrentWindow;
  435.  
  436.   switch (layer) {
  437.   case GLUT_NORMAL:
  438.     window->renderWin = window->win;
  439.     window->renderCtx = window->ctx;
  440.     break;
  441.   case GLUT_OVERLAY:
  442.     /* Did you crash here?  Calling glutUseLayer(GLUT_OVERLAY)
  443.        without an overlay established is erroneous.  Fix your
  444.        code. */
  445.     window->renderWin = window->overlay->win;
  446.     window->renderCtx = window->overlay->ctx;
  447.     break;
  448.   default:
  449.     __glutWarning("glutUseLayer: unknown layer, %d.", layer);
  450.     break;
  451.   }
  452.   __glutSetWindow(window);
  453. }
  454.  
  455. void APIENTRY 
  456. glutPostOverlayRedisplay(void)
  457. {
  458.   __glutPostRedisplay(__glutCurrentWindow, GLUT_OVERLAY_REDISPLAY_WORK);
  459. }
  460.  
  461. void APIENTRY 
  462. glutOverlayDisplayFunc(GLUTdisplayCB displayFunc)
  463. {
  464.   if (!__glutCurrentWindow->overlay) {
  465.     __glutWarning("glutOverlayDisplayFunc: window has no overlay established");
  466.     return;
  467.   }
  468.   __glutCurrentWindow->overlay->display = displayFunc;
  469. }
  470.  
  471. void APIENTRY 
  472. glutHideOverlay(void)
  473. {
  474.   if (!__glutCurrentWindow->overlay) {
  475.     __glutWarning("glutHideOverlay: window has no overlay established");
  476.     return;
  477.   }
  478.   XUnmapWindow(__glutDisplay, __glutCurrentWindow->overlay->win);
  479.   __glutCurrentWindow->overlay->shownState = 0;
  480. }
  481.  
  482. void APIENTRY 
  483. glutShowOverlay(void)
  484. {
  485.   if (!__glutCurrentWindow->overlay) {
  486.     __glutWarning("glutShowOverlay: window has no overlay established");
  487.     return;
  488.   }
  489.   XMapWindow(__glutDisplay, __glutCurrentWindow->overlay->win);
  490.   __glutCurrentWindow->overlay->shownState = 1;
  491. }
  492.  
  493. int APIENTRY 
  494. glutLayerGet(GLenum param)
  495. {
  496.   switch (param) {
  497.   case GLUT_OVERLAY_POSSIBLE:
  498.     {
  499.       XVisualInfo *vi;
  500.       Bool dummy, visAlloced;
  501.  
  502.       vi = determineOverlayVisual(&dummy, &visAlloced);
  503.       if (vi) {
  504.         if (visAlloced)
  505.           XFree(vi);
  506.         return 1;
  507.       }
  508.       return 0;
  509.     }
  510.   case GLUT_LAYER_IN_USE:
  511.     return __glutCurrentWindow->renderWin != __glutCurrentWindow->win;
  512.   case GLUT_HAS_OVERLAY:
  513.     return __glutCurrentWindow->overlay != NULL;
  514.   case GLUT_TRANSPARENT_INDEX:
  515.     if (__glutCurrentWindow->overlay) {
  516.       return __glutCurrentWindow->overlay->transparentPixel;
  517.     } else {
  518.       return -1;
  519.     }
  520.   case GLUT_NORMAL_DAMAGED:
  521.     /* __glutWindowDamaged is used so the damage state within
  522.        the window (or overlay belwo) can be cleared before
  523.        calling a display callback so on return, the state does
  524.        not have to be cleared (since upon return from the
  525.        callback the window could be destroyed (or layer
  526.        removed). */
  527.     return (__glutCurrentWindow->workMask & GLUT_REPAIR_WORK)
  528.       || __glutWindowDamaged;
  529.   case GLUT_OVERLAY_DAMAGED:
  530.     if (__glutCurrentWindow->overlay) {
  531.       return (__glutCurrentWindow->workMask & GLUT_OVERLAY_REPAIR_WORK)
  532.         || __glutWindowDamaged;
  533.     } else {
  534.       return -1;
  535.     }
  536.   default:
  537.     __glutWarning("invalid glutLayerGet param: %d", param);
  538.     return -1;
  539.   }
  540. }
  541. /* ENDCENTRY */
  542.