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

  1. #if !defined(FX)
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "gltk.h"
  7. #include "private.h"
  8.  
  9.  
  10. /*
  11.  * Return the index of the first bit set.  I'm using this instead of the
  12.  * ffs() function because I'm not sure it's really portable.
  13.  */
  14. static int my_ffs( unsigned int n )
  15. {
  16.    if (n==0) {
  17.       return 0;
  18.    }
  19.    else {
  20.       unsigned int m = 1;
  21.       int index = 1;
  22.  
  23.       while ((n&m)==0) {
  24.      m <<= 1;
  25.      index++;
  26.       }
  27.       return index;
  28.    }
  29. }
  30.  
  31.  
  32. /******************************************************************************/
  33.  
  34. int tkGetColorMapSize(void)
  35. {
  36.  
  37.     if (!xDisplay) {
  38.     return 0;
  39.     } else {
  40.     return w.vInfoMain->colormap_size;
  41.     }
  42. }
  43.  
  44. /******************************************************************************/
  45.  
  46. void tkGetMouseLoc(int *x, int *y)
  47. {
  48.     int junk;
  49.  
  50.     *x = 0;
  51.     *y = 0;
  52.     XQueryPointer(xDisplay, w.wMain, (Window *)&junk, (Window *)&junk,
  53.           &junk, &junk, x, y, (unsigned int *)&junk);
  54. }
  55.  
  56. /******************************************************************************/
  57.  
  58. void tkGetSystem(TKenum type, void *ptr)
  59. {
  60.  
  61.     switch (type) {
  62.       case TK_X_DISPLAY:
  63.     *(Display **)ptr = xDisplay;
  64.     break;
  65.       case TK_X_WINDOW:
  66.     *(Window *)ptr = w.wMain;
  67.     break;
  68.     }
  69. }
  70.  
  71. /******************************************************************************/
  72.  
  73. void tkSetFogRamp(int density, int startIndex)
  74. {
  75.     XColor c[4096];
  76.     int rShift, gShift, bShift, intensity, fogValues, colorValues;
  77.     int i, j, k;
  78.  
  79.     switch (w.vInfoMain->class) {
  80.       case DirectColor:
  81.     fogValues = 1 << density;
  82.     colorValues = 1 << startIndex;
  83.     for (i = 0; i < colorValues; i++) {
  84.         for (j = 0; j < fogValues; j++) {
  85.         k = i * fogValues + j;
  86.         intensity = i * fogValues + j * colorValues;
  87.         if (intensity > w.vInfoMain->colormap_size) {
  88.             intensity = w.vInfoMain->colormap_size;
  89.         }
  90.         intensity = (intensity << 8) | intensity;
  91.         rShift = my_ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  92.         gShift = my_ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  93.         bShift = my_ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  94.         c[k].pixel = ((k << rShift) & w.vInfoMain->red_mask) |
  95.                  ((k << gShift) & w.vInfoMain->green_mask) |
  96.                  ((k << bShift) & w.vInfoMain->blue_mask);
  97.         c[k].red = (unsigned short)intensity;
  98.         c[k].green = (unsigned short)intensity;
  99.         c[k].blue = (unsigned short)intensity;
  100.         c[k].flags = DoRed | DoGreen | DoBlue;
  101.         }
  102.     }
  103.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  104.     break;
  105.       case GrayScale:
  106.       case PseudoColor:
  107.     fogValues = 1 << density;
  108.     colorValues = 1 << startIndex;
  109.     for (i = 0; i < colorValues; i++) {
  110.         for (j = 0; j < fogValues; j++) {
  111.         k = i * fogValues + j;
  112.         intensity = i * fogValues + j * colorValues;
  113.         if (intensity > w.vInfoMain->colormap_size) {
  114.             intensity = w.vInfoMain->colormap_size;
  115.         }
  116.         intensity = (intensity << 8) | intensity;
  117.         c[k].pixel = k;
  118.         c[k].red = (unsigned short)intensity;
  119.         c[k].green = (unsigned short)intensity;
  120.         c[k].blue = (unsigned short)intensity;
  121.         c[k].flags = DoRed | DoGreen | DoBlue;
  122.         }
  123.     }
  124.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  125.     break;
  126.     }
  127.  
  128.     XSync(xDisplay, 0);
  129. }
  130.  
  131. /******************************************************************************/
  132.  
  133. void tkSetGreyRamp(void)
  134. {
  135.     XColor c[4096];
  136.     float intensity;
  137.     int rShift, gShift, bShift, i;
  138.  
  139.     switch (w.vInfoMain->class) {
  140.       case DirectColor:
  141.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  142.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  143.             65535.0 + 0.5;
  144.         rShift = my_ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  145.         gShift = my_ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  146.         bShift = my_ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  147.         c[i].pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  148.              ((i << gShift) & w.vInfoMain->green_mask) |
  149.              ((i << bShift) & w.vInfoMain->blue_mask);
  150.         c[i].red = (unsigned short)intensity;
  151.         c[i].green = (unsigned short)intensity;
  152.         c[i].blue = (unsigned short)intensity;
  153.         c[i].flags = DoRed | DoGreen | DoBlue;
  154.     }
  155.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  156.     break;
  157.       case GrayScale:
  158.       case PseudoColor:
  159.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  160.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  161.             65535.0 + 0.5;
  162.         c[i].pixel = i;
  163.         c[i].red = (unsigned short)intensity;
  164.         c[i].green = (unsigned short)intensity;
  165.         c[i].blue = (unsigned short)intensity;
  166.         c[i].flags = DoRed | DoGreen | DoBlue;
  167.     }
  168.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  169.     break;
  170.     }
  171.  
  172.     XSync(xDisplay, 0);
  173. }
  174.  
  175. /******************************************************************************/
  176.  
  177. void tkSetOneColor(int index, float r, float g, float b)
  178. {
  179.     XColor c;
  180.     int rShift, gShift, bShift;
  181.  
  182.     switch (w.vInfoMain->class) {
  183.       case DirectColor:
  184.     rShift = my_ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  185.     gShift = my_ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  186.     bShift = my_ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  187.     c.pixel = ((index << rShift) & w.vInfoMain->red_mask) |
  188.           ((index << gShift) & w.vInfoMain->green_mask) |
  189.           ((index << bShift) & w.vInfoMain->blue_mask);
  190.     c.red = (unsigned short)(r * 65535.0 + 0.5);
  191.     c.green = (unsigned short)(g * 65535.0 + 0.5);
  192.     c.blue = (unsigned short)(b * 65535.0 + 0.5);
  193.     c.flags = DoRed | DoGreen | DoBlue;
  194.     XStoreColor(xDisplay, w.cMapMain, &c);
  195.     break;
  196.       case GrayScale:
  197.       case PseudoColor:
  198.     if (index < w.vInfoMain->colormap_size) {
  199.         c.pixel = index;
  200.         c.red = (unsigned short)(r * 65535.0 + 0.5);
  201.         c.green = (unsigned short)(g * 65535.0 + 0.5);
  202.         c.blue = (unsigned short)(b * 65535.0 + 0.5);
  203.         c.flags = DoRed | DoGreen | DoBlue;
  204.         XStoreColor(xDisplay, w.cMapMain, &c);
  205.     }
  206.     break;
  207.     }
  208.  
  209.     XSync(xDisplay, 0);
  210. }
  211.  
  212. /******************************************************************************/
  213.  
  214. void tkSetOverlayMap(int size, float *rgb)
  215. {
  216.     XColor c;
  217.     unsigned long *buf;
  218.     int max, i;
  219.     Status status;
  220.  
  221.     if (w.vInfoOverlay->class == PseudoColor) {
  222.     max = (size > w.vInfoOverlay->colormap_size) ?
  223.           w.vInfoOverlay->colormap_size : size;
  224.     buf = (unsigned long *)calloc(max, sizeof(unsigned long));
  225.     status =
  226.             XAllocColorCells(xDisplay, w.cMapOverlay, True, NULL, 0, buf, max);
  227.         if (status )
  228.     for (i = /*1*/0; i < max; i++) {
  229.         c.pixel = buf[i];
  230.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  231.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  232.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  233.         c.flags = DoRed | DoGreen | DoBlue;
  234.         XStoreColor(xDisplay, w.cMapOverlay, &c);
  235.     }
  236.     free(buf);
  237.     }
  238.  
  239.     XSync(xDisplay, 0);
  240. }
  241.  
  242. /******************************************************************************/
  243.  
  244. void tkSetRGBMap(int size, float *rgb)
  245. {
  246.     XColor c;
  247.     int rShift, gShift, bShift, max, i;
  248.  
  249.     switch (w.vInfoMain->class) {
  250.       case DirectColor:
  251.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  252.                           : size;
  253.     rShift = my_ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  254.     gShift = my_ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  255.     bShift = my_ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  256.     c.flags = DoRed | DoGreen | DoBlue;
  257.     for (i = 0; i < max; i++) {
  258.         c.pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  259.               ((i << gShift) & w.vInfoMain->green_mask) |
  260.               ((i << bShift) & w.vInfoMain->blue_mask);
  261.         c.red = c.green = c.blue = (unsigned short) (i * 65535 / (max-1));
  262.         XStoreColor(xDisplay, w.cMapMain, &c);
  263.     }
  264.     break;
  265.       case GrayScale:
  266.       case PseudoColor:
  267.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  268.                           : size;
  269.     for (i = 0; i < max; i++) {
  270.         c.pixel = i;
  271.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  272.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  273.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  274.         c.flags = DoRed | DoGreen | DoBlue;
  275.         XStoreColor(xDisplay, w.cMapMain, &c);
  276.     }
  277.     break;
  278.     }
  279.  
  280.     XSync(xDisplay, 0);
  281. }
  282.  
  283. /******************************************************************************/
  284.  
  285. GLenum tkSetWindowLevel(GLenum level)
  286. {
  287.  
  288.     switch (level) {
  289.       case TK_OVERLAY:
  290.     if (TK_HAS_OVERLAY(w.type)) {
  291.         if (!glXMakeCurrent(xDisplay, w.wOverlay, w.cOverlay)) {
  292.         return GL_FALSE;
  293.         }
  294.     } else {
  295.         return GL_FALSE;
  296.     }
  297.     break;
  298.       case TK_RGB:
  299.       case TK_INDEX:
  300.     if (!glXMakeCurrent(xDisplay, w.wMain, w.cMain)) {
  301.         return GL_FALSE;
  302.     }
  303.     break;
  304.       default:
  305.         return GL_FALSE;
  306.     }
  307.     return GL_TRUE;
  308. }
  309.  
  310. /******************************************************************************/
  311.  
  312. #else
  313.  
  314. /* This is here to avoid ANSI C "empty source file" warnings */
  315. static void no_op () {}
  316.  
  317. #endif    /* !FX */
  318.