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

  1. /*
  2. ** blendxor.c - Demonstrates the use of the blend_logic_op
  3. **    extension to draw hilights.  Using XOR to draw the same
  4. **    image twice restores the background to its original value.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include "gltk.h"
  12.  
  13.  
  14. GLenum doubleBuffer, directRender;
  15. int dithering = 0;
  16. GLint windW, windH;
  17.  
  18. static void Init(void)
  19. {
  20.     glDisable(GL_DITHER);
  21.     glShadeModel(GL_FLAT);
  22. }
  23.  
  24. static void Reshape(int width, int height)
  25. {
  26.  
  27.     windW = (GLint)width;
  28.     windH = (GLint)height;
  29.  
  30.     glViewport(0, 0, (GLint)width, (GLint)height);
  31.  
  32.     glMatrixMode(GL_PROJECTION);
  33.     glLoadIdentity();
  34.     gluOrtho2D(0, 400, 0, 400);
  35.     glMatrixMode(GL_MODELVIEW);
  36. }
  37.  
  38. static GLenum Key(int key, GLenum mask)
  39. {
  40.  
  41.     switch (key) {
  42.       case TK_ESCAPE:
  43.     tkQuit();
  44.       case TK_d:
  45.     dithering = !dithering;
  46.     break;
  47.       default:
  48.     return GL_FALSE;
  49.     }
  50.     return GL_TRUE;
  51. }
  52.  
  53. static void Draw(void)
  54. {
  55.     float xscale, yscale;
  56.     GLfloat x, y;
  57.     int i;
  58.  
  59.     glDisable(GL_BLEND);
  60.  
  61.     (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  62.  
  63.     glClearColor(0.5, 0.6, 0.1, 1.0);
  64.     glClear(GL_COLOR_BUFFER_BIT);
  65.  
  66.     /* Draw background prims */
  67.     glColor3f(0.1, 0.1, 1.0);
  68.     glBegin(GL_TRIANGLES);
  69.         glVertex2i(5, 5);
  70.         glVertex2i(130, 50);
  71.         glVertex2i(100,  300);
  72.     glEnd();
  73.     glColor3f(0.5, 0.2, 0.9);
  74.     glBegin(GL_TRIANGLES);
  75.         glVertex2i(200, 100);
  76.         glVertex2i(330, 50);
  77.         glVertex2i(340,  400);
  78.     glEnd();
  79.  
  80.     glEnable(GL_BLEND);
  81.     glBlendEquationEXT(GL_LOGIC_OP);
  82.     glLogicOp(GL_XOR);
  83.  
  84.     /* Draw a set of rectangles across the window */
  85.     glColor3f(0.9, 0.2, 0.8);
  86.     for(i = 0; i < 400; i+=60) {
  87.         glBegin(GL_POLYGON);
  88.             glVertex2i(i, 100);
  89.             glVertex2i(i+50, 100);
  90.             glVertex2i(i+50, 200);
  91.             glVertex2i(i, 200);
  92.         glEnd();
  93.     }
  94.     glFlush();   /* Added by Brian Paul */
  95.     sleep(2);
  96.  
  97.     /* Redraw  the rectangles, which should erase them */
  98.     for(i = 0; i < 400; i+=60) {
  99.         glBegin(GL_POLYGON);
  100.             glVertex2i(i, 100);
  101.             glVertex2i(i+50, 100);
  102.             glVertex2i(i+50, 200);
  103.             glVertex2i(i, 200);
  104.         glEnd();
  105.     }
  106.     glFlush();
  107.  
  108.  
  109.     if (doubleBuffer) {
  110.     tkSwapBuffers();
  111.     }
  112. }
  113.  
  114. static GLenum Args(int argc, char **argv)
  115. {
  116.     GLint i;
  117.  
  118.     doubleBuffer = GL_FALSE;
  119.     directRender = GL_TRUE;
  120.  
  121.     for (i = 1; i < argc; i++) {
  122.     if (strcmp(argv[i], "-sb") == 0) {
  123.         doubleBuffer = GL_FALSE;
  124.     } else if (strcmp(argv[i], "-db") == 0) {
  125.         doubleBuffer = GL_TRUE;
  126.     } else if (strcmp(argv[i], "-dr") == 0) {
  127.         directRender = GL_TRUE;
  128.     } else if (strcmp(argv[i], "-ir") == 0) {
  129.         directRender = GL_FALSE;
  130.     } else {
  131.         printf("%s (Bad option).\n", argv[i]);
  132.         return GL_FALSE;
  133.     }
  134.     }
  135.     return GL_TRUE;
  136. }
  137.  
  138. void main(int argc, char **argv)
  139. {
  140.     GLenum type;
  141.     char *s;
  142.     char *extName = "GL_EXT_blend_logic_op";
  143.  
  144.     if (Args(argc, argv) == GL_FALSE) {
  145.     tkQuit();
  146.     }
  147.  
  148.     tkInitPosition(0, 0, 400, 400);
  149.  
  150.     type = TK_RGB;
  151.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  152.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  153.     tkInitDisplayMode(type);
  154.  
  155.     if (tkInitWindow("Blend XOR") == GL_FALSE) {
  156.     tkQuit();
  157.     }
  158.  
  159.     /* Make sure blend_logic_op extension is there. */
  160.     s = (char *) glGetString(GL_EXTENSIONS);
  161.     if (!s)
  162.     tkQuit();
  163.     if (strstr(s,extName) == 0) {
  164.     printf("Blend_logic_op extension is not present.\n");
  165.     tkQuit();
  166.     }
  167.  
  168.     Init();
  169.  
  170.     tkExposeFunc(Reshape);
  171.     tkReshapeFunc(Reshape);
  172.     tkKeyDownFunc(Key);
  173.     tkDisplayFunc(Draw);
  174.     tkExec();
  175. }
  176.