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