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