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