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

  1. /* $Id: alphabuf.c,v 1.1 1996/02/19 21:53:51 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995-1996  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. $Log: alphabuf.c,v $
  26.  * Revision 1.1  1996/02/19  21:53:51  brianp
  27.  * Initial revision
  28.  *
  29.  */
  30.  
  31.  
  32.  
  33. /*
  34.  * Software alpha planes.  Many frame buffers don't have alpha bits so
  35.  * we simulate them in software.
  36.  */
  37.  
  38.  
  39.  
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "context.h"
  43. #include "macros.h"
  44.  
  45.  
  46.  
  47.  
  48. #define ALPHA_ADDR(X,Y)  (CC.AlphaBuffer + (Y) * CC.BufferWidth + (X))
  49.  
  50.  
  51.  
  52. /*
  53.  * Allocate a new front and back alpha buffer.
  54.  */
  55. void gl_alloc_alpha_buffers( void )
  56. {
  57.    GLint bytes = CC.BufferWidth * CC.BufferHeight * sizeof(GLubyte);
  58.  
  59.    if (CC.FrontAlphaEnabled) {
  60.       if (CC.FrontAlphaBuffer) {
  61.          free( CC.FrontAlphaBuffer );
  62.       }
  63.       CC.FrontAlphaBuffer = (GLubyte *) malloc( bytes );
  64.       if (!CC.FrontAlphaBuffer) {
  65.          /* out of memory */
  66.          gl_error( GL_OUT_OF_MEMORY, "Couldn't allocate front alpha buffer" );
  67.       }
  68.    }
  69.    if (CC.BackAlphaEnabled) {
  70.       if (CC.BackAlphaBuffer) {
  71.          free( CC.BackAlphaBuffer );
  72.       }
  73.       CC.BackAlphaBuffer = (GLubyte *) malloc( bytes );
  74.       if (!CC.BackAlphaBuffer) {
  75.          /* out of memory */
  76.          gl_error( GL_OUT_OF_MEMORY, "Couldn't allocate back alpha buffer" );
  77.       }
  78.    }
  79.    if (CC.Color.DrawBuffer==GL_FRONT) {
  80.       CC.AlphaBuffer = CC.FrontAlphaBuffer;
  81.    }
  82.    if (CC.Color.DrawBuffer==GL_BACK) {
  83.       CC.AlphaBuffer = CC.BackAlphaBuffer;
  84.    }
  85. }
  86.  
  87.  
  88.  
  89. /*
  90.  * Clear the front or back alpha planes.
  91.  */
  92. void gl_clear_alpha_buffers( void )
  93. {
  94.    GLint buffer;
  95.  
  96.    for (buffer=0;buffer<2;buffer++) {
  97.       GLubyte *abuffer = NULL;
  98.       if (buffer==0 && CC.Color.DrawBuffer==GL_FRONT
  99.           && CC.FrontAlphaEnabled && CC.FrontAlphaBuffer) {
  100.          abuffer = CC.FrontAlphaBuffer;
  101.       }
  102.       else if (buffer==1 && CC.Color.DrawBuffer==GL_BACK
  103.           && CC.BackAlphaEnabled && CC.BackAlphaBuffer) {
  104.          abuffer = CC.BackAlphaBuffer;
  105.       }
  106.       if (abuffer) {
  107.          GLubyte aclear = (GLint) (CC.Color.ClearColor[3] * CC.AlphaScale);
  108.          if (CC.Scissor.Enabled) {
  109.             GLint i, j;
  110.             for (j=0;j<CC.Scissor.Height;j++) {
  111.                GLubyte *aptr = ALPHA_ADDR(CC.Scissor.Xmin, CC.Scissor.Ymin+j);
  112.                for (i=0;i<CC.Scissor.Width;i++) {
  113.                   *aptr++ = aclear;
  114.                }
  115.             }
  116.          }
  117.          else {
  118.             MEMSET( abuffer, aclear, CC.BufferWidth * CC.BufferHeight );
  119.          }
  120.       }
  121.    }
  122. }
  123.  
  124.  
  125.  
  126. void gl_write_alpha_span( GLuint n, GLint x, GLint y,
  127.                           GLubyte alpha[], GLubyte mask[] )
  128. {
  129.    GLubyte *aptr = ALPHA_ADDR( x, y );
  130.    GLuint i;
  131.  
  132.    if (mask) {
  133.       for (i=0;i<n;i++) {
  134.          if (mask[i]) {
  135.             *aptr = alpha[i];
  136.          }
  137.          aptr++;
  138.       }
  139.    }
  140.    else {
  141.       for (i=0;i<n;i++) {
  142.          *aptr++ = alpha[i];
  143.       }
  144.    }
  145. }
  146.  
  147.  
  148. void gl_write_mono_alpha_span( GLuint n, GLint x, GLint y,
  149.                                GLubyte alpha, GLubyte mask[] )
  150. {
  151.    GLubyte *aptr = ALPHA_ADDR( x, y );
  152.    GLuint i;
  153.  
  154.    if (mask) {
  155.       for (i=0;i<n;i++) {
  156.          if (mask[i]) {
  157.             *aptr = alpha;
  158.          }
  159.          aptr++;
  160.       }
  161.    }
  162.    else {
  163.       for (i=0;i<n;i++) {
  164.          *aptr++ = alpha;
  165.       }
  166.    }
  167. }
  168.  
  169.  
  170. void gl_write_alpha_pixels( GLuint n, const GLint x[], const GLint y[],
  171.                             const GLubyte alpha[], const GLubyte mask[] )
  172. {
  173.    GLuint i;
  174.  
  175.    if (mask) {
  176.       for (i=0;i<n;i++) {
  177.          if (mask[i]) {
  178.             GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  179.             *aptr = alpha[i];
  180.          }
  181.       }
  182.    }
  183.    else {
  184.       for (i=0;i<n;i++) {
  185.          GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  186.          *aptr = alpha[i];
  187.       }
  188.    }
  189. }
  190.  
  191.  
  192. void gl_write_mono_alpha_pixels( GLuint n, const GLint x[], const GLint y[],
  193.                                  GLubyte alpha, const GLubyte mask[] )
  194. {
  195.    GLuint i;
  196.  
  197.    if (mask) {
  198.       for (i=0;i<n;i++) {
  199.          if (mask[i]) {
  200.             GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  201.             *aptr = alpha;
  202.          }
  203.       }
  204.    }
  205.    else {
  206.       for (i=0;i<n;i++) {
  207.          GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  208.          *aptr = alpha;
  209.       }
  210.    }
  211. }
  212.  
  213.  
  214.  
  215. void gl_read_alpha_span( GLuint n, GLint x, GLint y, GLubyte alpha[] )
  216. {
  217.    GLubyte *aptr = ALPHA_ADDR( x, y );
  218.    GLuint i;
  219.    for (i=0;i<n;i++) {
  220.       alpha[i] = *aptr++;
  221.    }
  222. }
  223.  
  224.  
  225. void gl_read_alpha_pixels( GLuint n, const GLint x[], const GLint y[],
  226.                            GLubyte alpha[], const GLubyte mask[] )
  227. {
  228.    GLuint i;
  229.    for (i=0;i<n;i++) {
  230.       if (mask[i]) {
  231.          GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  232.          alpha[i] = *aptr;
  233.       }
  234.    }
  235. }
  236.  
  237.  
  238.  
  239.