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

  1. /*
  2. ** blendeq.c - Demonstrates the use of the blend_minmax, blend_subtract,
  3. **    and blend_logic_op extensions using glBlendEquationEXT.
  4. **
  5. **    Over a two-color backround, draw rectangles using twelve blend
  6. **    options.  The values are read back as UNSIGNED_BYTE and printed
  7. **    in hex over each value.  These values are useful for logic
  8. **    op comparisons when channels are 8 bits deep.
  9. */
  10.  
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "gltk.h"
  16.  
  17.  
  18. GLenum doubleBuffer, directRender;
  19. static int dithering = 0;
  20. static int doPrint = 1;
  21. static int deltaY;
  22. GLint windW, windH;
  23. GLuint bitmapBase;
  24.  
  25. static void Init(void)
  26. {
  27.     bitmapBase = glGenLists(256);
  28.     if (tkCreateBitmapFont(bitmapBase) == GL_FALSE) {
  29.     tkQuit();
  30.     }
  31.  
  32.     glDisable(GL_DITHER);
  33.     glShadeModel(GL_FLAT);
  34. }
  35.  
  36. static void Reshape(int width, int height)
  37. {
  38.  
  39.     windW = (GLint)width;
  40.     windH = (GLint)height;
  41.  
  42.     glViewport(0, 0, (GLint)width, (GLint)height);
  43.     deltaY = windH /16;
  44.  
  45.     glMatrixMode(GL_PROJECTION);
  46.     glLoadIdentity();
  47.     gluOrtho2D(0, windW, 0, windH);
  48.     glMatrixMode(GL_MODELVIEW);
  49. }
  50.  
  51. static GLenum Key(int key, GLenum mask)
  52. {
  53.  
  54.     switch (key) {
  55.       case TK_ESCAPE:
  56.     tkQuit();
  57.       case TK_d:
  58.     dithering = !dithering;
  59.     break;
  60.       default:
  61.     return GL_FALSE;
  62.     }
  63.     return GL_TRUE;
  64. }
  65.  
  66. static void PrintColorStrings( void )
  67. {
  68.     GLubyte ubbuf[3], ubcolor[3];
  69.     int i, xleft, xright;
  70.     char colorString[18];
  71.  
  72.     xleft = 5 + windW/4;
  73.     xright = 5 + windW/2;
  74.  
  75.     for (i = windH - deltaY + 4; i > 0; i-=deltaY) {
  76.         glReadPixels(xleft, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  77.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  78.                 ubbuf[0], ubbuf[1], ubbuf[2]);
  79.         glRasterPos2f(xleft, i);
  80.     tkDrawStr(bitmapBase, colorString);
  81.         glReadPixels(xright, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  82.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  83.                 ubbuf[0], ubbuf[1], ubbuf[2]);
  84.         glRasterPos2f(xright, i);
  85.     tkDrawStr(bitmapBase, colorString);
  86.     }
  87. }
  88.  
  89. static void Draw(void)
  90. {
  91.     float xscale, yscale;
  92.     GLfloat x, y;
  93.     int i, j;
  94.     GLfloat buf[3];
  95.     GLubyte ubbuf[3], ubcolor[3];
  96.     int stringOffset = 5, stringx = 8;
  97.     int x1, x2, xleft, xright;
  98.  
  99.  
  100.     (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  101.     glDisable(GL_BLEND);
  102.  
  103.     glClearColor(0.5, 0.6, 0.1, 1.0);
  104.     glClear(GL_COLOR_BUFFER_BIT);
  105.  
  106.     /* Draw background */
  107.     glColor3f(0.1, 0.1, 1.0);
  108.     glRectf(0.0, 0.0, windW/2, windH);
  109.  
  110.     /* Draw labels */
  111.     glColor3f(0.8, 0.8, 0.0);
  112.     i = windH - deltaY + stringOffset;
  113.     glRasterPos2f(stringx, i); i -= deltaY;
  114.     tkDrawStr(bitmapBase, "SOURCE");
  115.     glRasterPos2f(stringx, i); i -= deltaY;
  116.     tkDrawStr(bitmapBase, "DEST");
  117.     glRasterPos2f(stringx, i); i -= deltaY;
  118.     tkDrawStr(bitmapBase, "min");
  119.     glRasterPos2f(stringx, i); i -= deltaY;
  120.     tkDrawStr(bitmapBase, "max");
  121.     glRasterPos2f(stringx, i); i -= deltaY;
  122.     tkDrawStr(bitmapBase, "subtract");
  123.     glRasterPos2f(stringx, i); i -= deltaY;
  124.     tkDrawStr(bitmapBase, "reverse_subtract");
  125.     glRasterPos2f(stringx, i); i -= deltaY;
  126.     tkDrawStr(bitmapBase, "clear");
  127.     glRasterPos2f(stringx, i); i -= deltaY;
  128.     tkDrawStr(bitmapBase, "set");
  129.     glRasterPos2f(stringx, i); i -= deltaY;
  130.     tkDrawStr(bitmapBase, "copy");
  131.     glRasterPos2f(stringx, i); i -= deltaY;
  132.     tkDrawStr(bitmapBase, "noop");
  133.     glRasterPos2f(stringx, i); i -= deltaY;
  134.     tkDrawStr(bitmapBase, "and");
  135.     glRasterPos2f(stringx, i); i -= deltaY;
  136.     tkDrawStr(bitmapBase, "invert");
  137.     glRasterPos2f(stringx, i); i -= deltaY;
  138.     tkDrawStr(bitmapBase, "or");
  139.     glRasterPos2f(stringx, i); i -= deltaY;
  140.     tkDrawStr(bitmapBase, "xor");
  141.  
  142.  
  143.     i = windH - deltaY;
  144.     x1 = windW/4;
  145.     x2 = 3 * windW/4;
  146.     xleft = 5 + windW/4;
  147.     xright = 5 + windW/2;
  148.  
  149.     /* Draw foreground color for comparison */
  150.     glColor3f(0.9, 0.2, 0.8);
  151.     glRectf(x1, i, x2, i+deltaY);
  152.  
  153.     /* Leave one rectangle of background color */
  154.  
  155.     /* Begin test cases */
  156.     glEnable(GL_BLEND);
  157.     glBlendFunc(GL_ONE, GL_ONE);
  158.  
  159.     i -= 2*deltaY;
  160.     glBlendEquationEXT(GL_MIN_EXT);
  161.     glRectf(x1, i, x2, i+deltaY);
  162.  
  163.     i -= deltaY;
  164.     glBlendEquationEXT(GL_MAX_EXT);
  165.     glRectf(x1, i, x2, i+deltaY);
  166.  
  167.     i -= deltaY;
  168.     glBlendEquationEXT(GL_FUNC_SUBTRACT_EXT);
  169.     glRectf(x1, i, x2, i+deltaY);
  170.  
  171.     i -= deltaY;
  172.     glBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
  173.     glRectf(x1, i, x2, i+deltaY);
  174.  
  175.     glBlendFunc(GL_ONE, GL_ZERO);
  176.     i -= deltaY;
  177.     glBlendEquationEXT(GL_LOGIC_OP);
  178.     glLogicOp(GL_CLEAR);
  179.     glRectf(x1, i, x2, i+deltaY);
  180.  
  181.     i -= deltaY;
  182.     glBlendEquationEXT(GL_LOGIC_OP);
  183.     glLogicOp(GL_SET);
  184.     glRectf(x1, i, x2, i+deltaY);
  185.  
  186.     i -= deltaY;
  187.     glBlendEquationEXT(GL_LOGIC_OP);
  188.     glLogicOp(GL_COPY);
  189.     glRectf(x1, i, x2, i+deltaY);
  190.  
  191.     i -= deltaY;
  192.     glBlendEquationEXT(GL_LOGIC_OP);
  193.     glLogicOp(GL_NOOP);
  194.     glRectf(x1, i, x2, i+deltaY);
  195.  
  196.     i -= deltaY;
  197.     glBlendEquationEXT(GL_LOGIC_OP);
  198.     glLogicOp(GL_AND);
  199.     glRectf(x1, i, x2, i+deltaY);
  200.  
  201.     i -= deltaY;
  202.     glBlendEquationEXT(GL_LOGIC_OP);
  203.     glLogicOp(GL_INVERT);
  204.     glRectf(x1, i, x2, i+deltaY);
  205.  
  206.     i -= deltaY;
  207.     glBlendEquationEXT(GL_LOGIC_OP);
  208.     glLogicOp(GL_OR);
  209.     glRectf(x1, i, x2, i+deltaY);
  210.  
  211.     i -= deltaY;
  212.     glBlendEquationEXT(GL_LOGIC_OP);
  213.     glLogicOp(GL_XOR);
  214.     glRectf(x1, i, x2, i+deltaY);
  215.     glRectf(x1, i+10, x2, i+5);
  216.  
  217.   if (doPrint) {
  218.       glDisable(GL_BLEND);
  219.       glColor3f(1.0, 1.0, 1.0);
  220.       PrintColorStrings();
  221.   }
  222.   glFlush();
  223.  
  224.     if (doubleBuffer) {
  225.        tkSwapBuffers();
  226.     }
  227.  
  228. }
  229.  
  230. static GLenum Args(int argc, char **argv)
  231. {
  232.     GLint i;
  233.  
  234.     doubleBuffer = GL_FALSE;
  235.     directRender = GL_TRUE;
  236.  
  237.     for (i = 1; i < argc; i++) {
  238.     if (strcmp(argv[i], "-sb") == 0) {
  239.         doubleBuffer = GL_FALSE;
  240.     } else if (strcmp(argv[i], "-db") == 0) {
  241.         doubleBuffer = GL_TRUE;
  242.     } else if (strcmp(argv[i], "-dr") == 0) {
  243.         directRender = GL_TRUE;
  244.     } else if (strcmp(argv[i], "-ir") == 0) {
  245.         directRender = GL_FALSE;
  246.     } else {
  247.         printf("%s (Bad option).\n", argv[i]);
  248.         return GL_FALSE;
  249.     }
  250.     }
  251.     return GL_TRUE;
  252. }
  253.  
  254. void main(int argc, char **argv)
  255. {
  256.     GLenum type;
  257.     char *s;
  258.     char *extName1 = "GL_EXT_blend_logic_op";
  259.     char *extName2 = "GL_EXT_blend_minmax";
  260.     char *extName3 = "GL_EXT_blend_subtract";
  261.  
  262.     if (Args(argc, argv) == GL_FALSE) {
  263.     tkQuit();
  264.     }
  265.  
  266.     tkInitPosition(0, 0, 800, 400);
  267.  
  268.     type = TK_RGB;
  269.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  270.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  271.     tkInitDisplayMode(type);
  272.  
  273.     if (tkInitWindow("Blend Equation") == GL_FALSE) {
  274.     tkQuit();
  275.     }
  276.  
  277.     /* Make sure blend_logic_op extension is there. */
  278.     s = (char *) glGetString(GL_EXTENSIONS);
  279.     if (!s)
  280.     tkQuit();
  281.     if (strstr(s,extName1) == 0) {
  282.     printf("Blend_logic_op extension is not present.\n");
  283.     tkQuit();
  284.     }
  285.     if (strstr(s,extName2) == 0) {
  286.     printf("Blend_minmax extension is not present.\n");
  287.     tkQuit();
  288.     }
  289.     if (strstr(s,extName3) == 0) {
  290.     printf("Blend_subtract extension is not present.\n");
  291.     tkQuit();
  292.     }
  293.  
  294.     Init();
  295.  
  296.     tkExposeFunc(Reshape);
  297.     tkReshapeFunc(Reshape);
  298.     tkKeyDownFunc(Key);
  299.     tkDisplayFunc(Draw);
  300.     tkExec();
  301. }
  302.