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

  1. /* logic.c */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25. $Id: logic.c,v 1.9 1995/10/30 15:32:41 brianp Exp $
  26.  
  27. $Log: logic.c,v $
  28.  * Revision 1.9  1995/10/30  15:32:41  brianp
  29.  * added mask argument to DD.read_index_pixels call
  30.  *
  31.  * Revision 1.8  1995/08/31  21:27:13  brianp
  32.  * call DD.read_index_span instead of dd_read_index_span
  33.  * introduced CC.NewState convention
  34.  *
  35.  * Revision 1.7  1995/05/22  21:02:41  brianp
  36.  * Release 1.2
  37.  *
  38.  * Revision 1.6  1995/05/12  16:57:22  brianp
  39.  * replaced CC.Mode!=0 with INSIDE_BEGIN_END
  40.  *
  41.  * Revision 1.5  1995/03/24  17:00:38  brianp
  42.  * added gl_update_pixel_logic
  43.  *
  44.  * Revision 1.4  1995/03/08  15:10:02  brianp
  45.  * added support for dd_logicop
  46.  *
  47.  * Revision 1.3  1995/03/07  18:32:09  brianp
  48.  * added gl_logic_pixels()
  49.  *
  50.  * Revision 1.2  1995/03/04  19:29:44  brianp
  51.  * 1.1 beta revision
  52.  *
  53.  * Revision 1.1  1995/02/24  14:24:47  brianp
  54.  * Initial revision
  55.  *
  56.  */
  57.  
  58.  
  59. /* color-index pixel logic operations */
  60.  
  61.  
  62. #include "context.h"
  63. #include "dd.h"
  64. #include "list.h"
  65. #include "macros.h"
  66. #include "pb.h"
  67.  
  68.  
  69.  
  70. void gl_logicop( GLenum opcode )
  71. {
  72.    if (INSIDE_BEGIN_END) {
  73.       gl_error( GL_INVALID_OPERATION, "glLogicOp" );
  74.       return;
  75.    }
  76.    switch (opcode) {
  77.       case GL_CLEAR:
  78.       case GL_SET:
  79.       case GL_COPY:
  80.       case GL_COPY_INVERTED:
  81.       case GL_NOOP:
  82.       case GL_INVERT:
  83.       case GL_AND:
  84.       case GL_NAND:
  85.       case GL_OR:
  86.       case GL_NOR:
  87.       case GL_XOR:
  88.       case GL_EQUIV:
  89.       case GL_AND_REVERSE:
  90.       case GL_AND_INVERTED:
  91.       case GL_OR_REVERSE:
  92.       case GL_OR_INVERTED:
  93.          CC.Color.LogicOp = opcode;
  94.          CC.NewState = GL_TRUE;
  95.      return;
  96.       default:
  97.          gl_error( GL_INVALID_ENUM, "glLogicOp" );
  98.      return;
  99.    }
  100. }
  101.  
  102.  
  103.  
  104.  
  105. void glLogicOp( GLenum opcode )
  106. {
  107.    if (CC.CompileFlag) {
  108.       gl_save_logicop( opcode );
  109.    }
  110.    if (CC.ExecuteFlag) {
  111.       gl_logicop( opcode );
  112.    }
  113. }
  114.  
  115.  
  116.  
  117. /*
  118.  * Apply the current logic operator to a span of pixels.  This is only
  119.  * used if the device driver can't do logic ops.
  120.  */
  121. void gl_logic_span( GLuint n, GLint x, GLint y, GLuint index[],
  122.             GLubyte mask[] )
  123. {
  124.    GLuint dest[MAX_WIDTH];
  125.    GLuint i;
  126.  
  127.    /* Read dest values from frame buffer */
  128.    (*DD.read_index_span)( n, x, y, dest );
  129.  
  130.    switch (CC.Color.LogicOp) {
  131.       case GL_CLEAR:
  132.          for (i=0;i<n;i++) {
  133.         if (mask[i]) {
  134.            index[i] = 0;
  135.         }
  136.      }
  137.      break;
  138.       case GL_SET:
  139.          for (i=0;i<n;i++) {
  140.         if (mask[i]) {
  141.            index[i] = 1;
  142.         }
  143.      }
  144.      break;
  145.       case GL_COPY:
  146.      /* do nothing */
  147.      break;
  148.       case GL_COPY_INVERTED:
  149.          for (i=0;i<n;i++) {
  150.         if (mask[i]) {
  151.            index[i] = !index[i];
  152.         }
  153.      }
  154.      break;
  155.       case GL_NOOP:
  156.          for (i=0;i<n;i++) {
  157.         if (mask[i]) {
  158.            index[i] = dest[i];
  159.         }
  160.      }
  161.      break;
  162.       case GL_INVERT:
  163.          for (i=0;i<n;i++) {
  164.         if (mask[i]) {
  165.            index[i] = !dest[i];
  166.         }
  167.      }
  168.      break;
  169.       case GL_AND:
  170.          for (i=0;i<n;i++) {
  171.         if (mask[i]) {
  172.            index[i] &= dest[i];
  173.         }
  174.      }
  175.      break;
  176.       case GL_NAND:
  177.          for (i=0;i<n;i++) {
  178.         if (mask[i]) {
  179.            index[i] = !(index[i] & dest[i]);
  180.         }
  181.      }
  182.      break;
  183.       case GL_OR:
  184.          for (i=0;i<n;i++) {
  185.         if (mask[i]) {
  186.            index[i] |= dest[i];
  187.         }
  188.      }
  189.      break;
  190.       case GL_NOR:
  191.          for (i=0;i<n;i++) {
  192.         if (mask[i]) {
  193.            index[i] = !(index[i] | dest[i]);
  194.         }
  195.      }
  196.      break;
  197.       case GL_XOR:
  198.          for (i=0;i<n;i++) {
  199.         if (mask[i]) {
  200.            index[i] ^= dest[i];
  201.         }
  202.      }
  203.      break;
  204.       case GL_EQUIV:
  205.          for (i=0;i<n;i++) {
  206.         if (mask[i]) {
  207.            index[i] = !(index[i] ^ dest[i]);
  208.         }
  209.      }
  210.      break;
  211.       case GL_AND_REVERSE:
  212.          for (i=0;i<n;i++) {
  213.         if (mask[i]) {
  214.            index[i] = index[i] & !dest[i];
  215.         }
  216.      }
  217.      break;
  218.       case GL_AND_INVERTED:
  219.          for (i=0;i<n;i++) {
  220.         if (mask[i]) {
  221.            index[i] = !index[i] & dest[i];
  222.         }
  223.      }
  224.      break;
  225.       case GL_OR_REVERSE:
  226.          for (i=0;i<n;i++) {
  227.         if (mask[i]) {
  228.            index[i] = index[i] | !dest[i];
  229.         }
  230.      }
  231.      break;
  232.       case GL_OR_INVERTED:
  233.          for (i=0;i<n;i++) {
  234.         if (mask[i]) {
  235.            index[i] = !index[i] | dest[i];
  236.         }
  237.      }
  238.      break;
  239.       default:
  240.      gl_error( GL_INVALID_ENUM, "gl_logic error" );
  241.    }
  242. }
  243.  
  244.  
  245.  
  246. /*
  247.  * Apply the current logic operator to an array of pixels.  This is only
  248.  * used if the device driver can't do logic ops.
  249.  */
  250. void gl_logic_pixels( GLuint n, const GLint x[], const GLint y[],
  251.               GLuint index[], GLubyte mask[] )
  252. {
  253.    GLuint dest[PB_SIZE];
  254.    GLuint i;
  255.  
  256.    /* Read dest values from frame buffer */
  257.    (*DD.read_index_pixels)( n, x, y, dest, mask );
  258.  
  259.    switch (CC.Color.LogicOp) {
  260.       case GL_CLEAR:
  261.          for (i=0;i<n;i++) {
  262.         if (mask[i]) {
  263.            index[i] = 0;
  264.         }
  265.      }
  266.      break;
  267.       case GL_SET:
  268.          for (i=0;i<n;i++) {
  269.         if (mask[i]) {
  270.            index[i] = 1;
  271.         }
  272.      }
  273.      break;
  274.       case GL_COPY:
  275.      /* do nothing */
  276.      break;
  277.       case GL_COPY_INVERTED:
  278.          for (i=0;i<n;i++) {
  279.         if (mask[i]) {
  280.            index[i] = !index[i];
  281.         }
  282.      }
  283.      break;
  284.       case GL_NOOP:
  285.          for (i=0;i<n;i++) {
  286.         if (mask[i]) {
  287.            index[i] = dest[i];
  288.         }
  289.      }
  290.      break;
  291.       case GL_INVERT:
  292.          for (i=0;i<n;i++) {
  293.         if (mask[i]) {
  294.            index[i] = !dest[i];
  295.         }
  296.      }
  297.      break;
  298.       case GL_AND:
  299.          for (i=0;i<n;i++) {
  300.         if (mask[i]) {
  301.            index[i] &= dest[i];
  302.         }
  303.      }
  304.      break;
  305.       case GL_NAND:
  306.          for (i=0;i<n;i++) {
  307.         if (mask[i]) {
  308.            index[i] = !(index[i] & dest[i]);
  309.         }
  310.      }
  311.      break;
  312.       case GL_OR:
  313.          for (i=0;i<n;i++) {
  314.         if (mask[i]) {
  315.            index[i] |= dest[i];
  316.         }
  317.      }
  318.      break;
  319.       case GL_NOR:
  320.          for (i=0;i<n;i++) {
  321.         if (mask[i]) {
  322.            index[i] = !(index[i] | dest[i]);
  323.         }
  324.      }
  325.      break;
  326.       case GL_XOR:
  327.          for (i=0;i<n;i++) {
  328.         if (mask[i]) {
  329.            index[i] ^= dest[i];
  330.         }
  331.      }
  332.      break;
  333.       case GL_EQUIV:
  334.          for (i=0;i<n;i++) {
  335.         if (mask[i]) {
  336.            index[i] = !(index[i] ^ dest[i]);
  337.         }
  338.      }
  339.      break;
  340.       case GL_AND_REVERSE:
  341.          for (i=0;i<n;i++) {
  342.         if (mask[i]) {
  343.            index[i] = index[i] & !dest[i];
  344.         }
  345.      }
  346.      break;
  347.       case GL_AND_INVERTED:
  348.          for (i=0;i<n;i++) {
  349.         if (mask[i]) {
  350.            index[i] = !index[i] & dest[i];
  351.         }
  352.      }
  353.      break;
  354.       case GL_OR_REVERSE:
  355.          for (i=0;i<n;i++) {
  356.         if (mask[i]) {
  357.            index[i] = index[i] | !dest[i];
  358.         }
  359.      }
  360.      break;
  361.       case GL_OR_INVERTED:
  362.          for (i=0;i<n;i++) {
  363.         if (mask[i]) {
  364.            index[i] = !index[i] | dest[i];
  365.         }
  366.      }
  367.      break;
  368.       default:
  369.      gl_error( GL_INVALID_ENUM, "gl_logic_pixels error" );
  370.    }
  371. }
  372.