Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/accum.c

raw
1 /* $Id: accum.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 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "accum.h"
27 #include "context.h"
28 #include "dlist.h"
29 #include "macros.h"
30 #include "types.h"
31
32
33
34 void gl_alloc_accum_buffer( GLcontext *ctx )
35 {
36 GLint n;
37
38 if (ctx->Buffer->Accum) {
39 free( ctx->Buffer->Accum );
40 ctx->Buffer->Accum = NULL;
41 }
42
43 /* allocate accumulation buffer if not already present */
44 n = ctx->Buffer->Width * ctx->Buffer->Height * 4 * sizeof(GLaccum);
45 ctx->Buffer->Accum = (GLaccum *) malloc( n );
46 if (!ctx->Buffer->Accum) {
47 /* unable to setup accumulation buffer */
48 gl_error( ctx, GL_OUT_OF_MEMORY, "glAccum" );
49 }
50 }
51
52
53
54 void gl_ClearAccum( GLcontext *ctx,
55 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
56 {
57 if (INSIDE_BEGIN_END(ctx)) {
58 gl_error( ctx, GL_INVALID_OPERATION, "glAccum" );
59 return;
60 }
61 ctx->Accum.ClearColor[0] = CLAMP( red, -1.0, 1.0 );
62 ctx->Accum.ClearColor[1] = CLAMP( green, -1.0, 1.0 );
63 ctx->Accum.ClearColor[2] = CLAMP( blue, -1.0, 1.0 );
64 ctx->Accum.ClearColor[3] = CLAMP( alpha, -1.0, 1.0 );
65 }
66
67
68
69
70 void gl_Accum( GLcontext *ctx, GLenum op, GLfloat value )
71 {
72 GLuint xpos, ypos, width, height;
73 GLfloat acc_scale;
74
75 if (ctx->Visual->AccumBits==0 || !ctx->Buffer->Accum) {
76 /* No accumulation buffer! */
77 return;
78 }
79
80 if (sizeof(GLaccum)==1) {
81 acc_scale = 127.0;
82 }
83 else if (sizeof(GLaccum)==2) {
84 acc_scale = 32767.0;
85 }
86 else {
87 /* sizeof(GLaccum) > 2 (Cray) */
88 acc_scale = (float) SHRT_MAX;
89 }
90
91 if (INSIDE_BEGIN_END(ctx)) {
92 gl_error( ctx, GL_INVALID_OPERATION, "glAccum" );
93 return;
94 }
95
96 /* Determine region to operate upon. */
97 if (ctx->Scissor.Enabled) {
98 xpos = ctx->Scissor.X;
99 ypos = ctx->Scissor.Y;
100 width = ctx->Scissor.Width;
101 height = ctx->Scissor.Height;
102 }
103 else {
104 /* whole window */
105 xpos = 0;
106 ypos = 0;
107 width = ctx->Buffer->Width;
108 height = ctx->Buffer->Height;
109 }
110
111 switch (op) {
112 case GL_ADD:
113 {
114 GLaccum ival, *acc;
115 GLuint i, j;
116
117 ival = (GLaccum) (value * acc_scale);
118 for (j=0;j<height;j++) {
119 acc = ctx->Buffer->Accum
120 + (ypos * ctx->Buffer->Width + xpos) * 4;
121 for (i=0;i<width;i++) {
122 *acc += ival; acc++; /* red */
123 *acc += ival; acc++; /* green */
124 *acc += ival; acc++; /* blue */
125 *acc += ival; acc++; /* alpha */
126 }
127 ypos++;
128 }
129 }
130 break;
131 case GL_MULT:
132 {
133 GLaccum *acc;
134 GLuint i, j;
135
136 for (j=0;j<height;j++) {
137 acc = ctx->Buffer->Accum
138 + (ypos * ctx->Buffer->Width + xpos) * 4;
139 for (i=0;i<width;i++) {
140 *acc = (GLaccum) ( (GLfloat) *acc * value ); acc++; /*r*/
141 *acc = (GLaccum) ( (GLfloat) *acc * value ); acc++; /*g*/
142 *acc = (GLaccum) ( (GLfloat) *acc * value ); acc++; /*g*/
143 *acc = (GLaccum) ( (GLfloat) *acc * value ); acc++; /*a*/
144 }
145 ypos++;
146 }
147 }
148 break;
149 case GL_ACCUM:
150 {
151 GLaccum *acc;
152 GLubyte red[MAX_WIDTH], green[MAX_WIDTH];
153 GLubyte blue[MAX_WIDTH], alpha[MAX_WIDTH];
154 GLfloat rscale, gscale, bscale, ascale;
155 GLuint i, j;
156
157 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.ReadBuffer );
158
159 /* Accumulate */
160 rscale = value * acc_scale * ctx->Visual->InvRedScale;
161 gscale = value * acc_scale * ctx->Visual->InvGreenScale;
162 bscale = value * acc_scale * ctx->Visual->InvBlueScale;
163 ascale = value * acc_scale * ctx->Visual->InvAlphaScale;
164 for (j=0;j<height;j++) {
165 (*ctx->Driver.ReadColorSpan)( ctx, width, xpos, ypos,
166 red, green, blue, alpha);
167 acc = ctx->Buffer->Accum
168 + (ypos * ctx->Buffer->Width + xpos) * 4;
169 for (i=0;i<width;i++) {
170 *acc += (GLaccum) ( (GLfloat) red[i] * rscale ); acc++;
171 *acc += (GLaccum) ( (GLfloat) green[i] * gscale ); acc++;
172 *acc += (GLaccum) ( (GLfloat) blue[i] * bscale ); acc++;
173 *acc += (GLaccum) ( (GLfloat) alpha[i] * ascale ); acc++;
174 }
175 ypos++;
176 }
177
178 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer );
179 }
180 break;
181 case GL_LOAD:
182 {
183 GLaccum *acc;
184 GLubyte red[MAX_WIDTH], green[MAX_WIDTH];
185 GLubyte blue[MAX_WIDTH], alpha[MAX_WIDTH];
186 GLfloat rscale, gscale, bscale, ascale;
187 GLuint i, j;
188
189 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.ReadBuffer );
190
191 /* Load accumulation buffer */
192 rscale = value * acc_scale * ctx->Visual->InvRedScale;
193 gscale = value * acc_scale * ctx->Visual->InvGreenScale;
194 bscale = value * acc_scale * ctx->Visual->InvBlueScale;
195 ascale = value * acc_scale * ctx->Visual->InvAlphaScale;
196 for (j=0;j<height;j++) {
197 (*ctx->Driver.ReadColorSpan)( ctx, width, xpos, ypos,
198 red, green, blue, alpha);
199 acc = ctx->Buffer->Accum
200 + (ypos * ctx->Buffer->Width + xpos) * 4;
201 for (i=0;i<width;i++) {
202 *acc++ = (GLaccum) ( (GLfloat) red[i] * rscale );
203 *acc++ = (GLaccum) ( (GLfloat) green[i] * gscale );
204 *acc++ = (GLaccum) ( (GLfloat) blue[i] * bscale );
205 *acc++ = (GLaccum) ( (GLfloat) alpha[i] * ascale );
206 }
207 ypos++;
208 }
209
210 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer );
211 }
212 break;
213 case GL_RETURN:
214 {
215 GLubyte red[MAX_WIDTH], green[MAX_WIDTH];
216 GLubyte blue[MAX_WIDTH], alpha[MAX_WIDTH];
217 GLaccum *acc;
218 GLfloat rscale, gscale, bscale, ascale;
219 GLint rmax, gmax, bmax, amax;
220 GLuint i, j;
221
222 rscale = value / acc_scale * ctx->Visual->RedScale;
223 gscale = value / acc_scale * ctx->Visual->GreenScale;
224 bscale = value / acc_scale * ctx->Visual->BlueScale;
225 ascale = value / acc_scale * ctx->Visual->AlphaScale;
226 rmax = (GLint) ctx->Visual->RedScale;
227 gmax = (GLint) ctx->Visual->GreenScale;
228 bmax = (GLint) ctx->Visual->BlueScale;
229 amax = (GLint) ctx->Visual->AlphaScale;
230 for (j=0;j<height;j++) {
231 acc = ctx->Buffer->Accum
232 + (ypos * ctx->Buffer->Width + xpos) * 4;
233 for (i=0;i<width;i++) {
234 GLint r, g, b, a;
235 r = (GLint) ( (GLfloat) (*acc++) * rscale + 0.5F );
236 g = (GLint) ( (GLfloat) (*acc++) * gscale + 0.5F );
237 b = (GLint) ( (GLfloat) (*acc++) * bscale + 0.5F );
238 a = (GLint) ( (GLfloat) (*acc++) * ascale + 0.5F );
239 red[i] = CLAMP( r, 0, rmax );
240 green[i] = CLAMP( g, 0, gmax );
241 blue[i] = CLAMP( b, 0, bmax );
242 alpha[i] = CLAMP( a, 0, amax );
243 }
244 (*ctx->Driver.WriteColorSpan)( ctx, width, xpos, ypos,
245 red, green, blue, alpha, NULL );
246 ypos++;
247 }
248 }
249 break;
250 default:
251 gl_error( ctx, GL_INVALID_ENUM, "glAccum" );
252 }
253 }
254
255
256
257
258 /*
259 * Clear the accumulation Buffer->
260 */
261 void gl_clear_accum_buffer( GLcontext *ctx )
262 {
263 GLuint buffersize;
264 GLfloat acc_scale;
265
266 if (ctx->Visual->AccumBits==0) {
267 /* No accumulation buffer! */
268 return;
269 }
270
271 if (sizeof(GLaccum)==1) {
272 acc_scale = 127.0;
273 }
274 else if (sizeof(GLaccum)==2) {
275 acc_scale = 32767.0;
276 }
277 else {
278 /* sizeof(GLaccum) > 2 (Cray) */
279 acc_scale = (float) SHRT_MAX;
280 }
281
282 /* number of pixels */
283 buffersize = ctx->Buffer->Width * ctx->Buffer->Height;
284
285 if (!ctx->Buffer->Accum) {
286 /* try to alloc accumulation buffer */
287 ctx->Buffer->Accum = (GLaccum *)
288 malloc( buffersize * 4 * sizeof(GLaccum) );
289 }
290
291 if (ctx->Buffer->Accum) {
292 if (ctx->Scissor.Enabled) {
293 /* Limit clear to scissor box */
294 GLaccum r, g, b, a;
295 GLint i, j;
296 GLint width, height;
297 GLaccum *row;
298 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
299 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
300 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
301 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
302 /* size of region to clear */
303 width = 4 * (ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1);
304 height = ctx->Buffer->Ymax - ctx->Buffer->Ymin + 1;
305 /* ptr to first element to clear */
306 row = ctx->Buffer->Accum
307 + 4 * (ctx->Buffer->Ymin * ctx->Buffer->Width
308 + ctx->Buffer->Xmin);
309 for (j=0;j<height;j++) {
310 for (i=0;i<width;i+=4) {
311 row[i+0] = r;
312 row[i+1] = g;
313 row[i+2] = b;
314 row[i+3] = a;
315 }
316 row += 4 * ctx->Buffer->Width;
317 }
318 }
319 else {
320 /* clear whole buffer */
321 if (ctx->Accum.ClearColor[0]==0.0 &&
322 ctx->Accum.ClearColor[1]==0.0 &&
323 ctx->Accum.ClearColor[2]==0.0 &&
324 ctx->Accum.ClearColor[3]==0.0) {
325 /* Black */
326 MEMSET( ctx->Buffer->Accum, 0, buffersize * 4 * sizeof(GLaccum) );
327 }
328 else {
329 /* Not black */
330 GLaccum *acc, r, g, b, a;
331 GLuint i;
332
333 acc = ctx->Buffer->Accum;
334 r = (GLaccum) (ctx->Accum.ClearColor[0] * acc_scale);
335 g = (GLaccum) (ctx->Accum.ClearColor[1] * acc_scale);
336 b = (GLaccum) (ctx->Accum.ClearColor[2] * acc_scale);
337 a = (GLaccum) (ctx->Accum.ClearColor[3] * acc_scale);
338 for (i=0;i<buffersize;i++) {
339 *acc++ = r;
340 *acc++ = g;
341 *acc++ = b;
342 *acc++ = a;
343 }
344 }
345 }
346 }
347 }