Debian Codegraph @

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

raw
1 /* $Id: misc.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 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 <stdlib.h>
24 #include <string.h>
25 #include "accum.h"
26 #include "alphabuf.h"
27 #include "context.h"
28 #include "depth.h"
29 #include "macros.h"
30 #include "masking.h"
31 #include "misc.h"
32 #include "stencil.h"
33 #include "types.h"
34
35
36
37
38
39 void gl_ClearIndex( GLcontext *ctx, GLfloat c )
40 {
41 if (INSIDE_BEGIN_END(ctx)) {
42 gl_error( ctx, GL_INVALID_OPERATION, "glClearIndex" );
43 return;
44 }
45 ctx->Color.ClearIndex = (GLuint) c;
46 if (!ctx->Visual->RGBAflag) {
47 /* it's OK to call glClearIndex in RGBA mode but it should be a NOP */
48 (*ctx->Driver.ClearIndex)( ctx, ctx->Color.ClearIndex );
49 }
50 }
51
52
53
54 void gl_ClearColor( GLcontext *ctx, GLclampf red, GLclampf green,
55 GLclampf blue, GLclampf alpha )
56 {
57 if (INSIDE_BEGIN_END(ctx)) {
58 gl_error( ctx, GL_INVALID_OPERATION, "glClearColor" );
59 return;
60 }
61
62 ctx->Color.ClearColor[0] = CLAMP( red, 0.0F, 1.0F );
63 ctx->Color.ClearColor[1] = CLAMP( green, 0.0F, 1.0F );
64 ctx->Color.ClearColor[2] = CLAMP( blue, 0.0F, 1.0F );
65 ctx->Color.ClearColor[3] = CLAMP( alpha, 0.0F, 1.0F );
66
67 if (ctx->Visual->RGBAflag) {
68 GLubyte r = (GLint) (ctx->Color.ClearColor[0] * ctx->Visual->RedScale);
69 GLubyte g = (GLint) (ctx->Color.ClearColor[1] * ctx->Visual->GreenScale);
70 GLubyte b = (GLint) (ctx->Color.ClearColor[2] * ctx->Visual->BlueScale);
71 GLubyte a = (GLint) (ctx->Color.ClearColor[3] * ctx->Visual->AlphaScale);
72 (*ctx->Driver.ClearColor)( ctx, r, g, b, a );
73 }
74 }
75
76
77
78
79 /*
80 * Clear the color buffer when glColorMask or glIndexMask is in effect.
81 */
82 static void clear_color_buffer_with_masking( GLcontext *ctx )
83 {
84 GLint x, y, height, width;
85
86 /* Compute region to clear */
87 if (ctx->Scissor.Enabled) {
88 x = ctx->Buffer->Xmin;
89 y = ctx->Buffer->Ymin;
90 height = ctx->Buffer->Ymax - ctx->Buffer->Ymin + 1;
91 width = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
92 }
93 else {
94 x = 0;
95 y = 0;
96 height = ctx->Buffer->Height;
97 width = ctx->Buffer->Width;
98 }
99
100 if (ctx->Visual->RGBAflag) {
101 /* RGBA mode */
102 GLubyte red[MAX_WIDTH], green[MAX_WIDTH];
103 GLubyte blue[MAX_WIDTH], alpha[MAX_WIDTH];
104 GLubyte r = ctx->Color.ClearColor[0] * ctx->Visual->RedScale;
105 GLubyte g = ctx->Color.ClearColor[1] * ctx->Visual->GreenScale;
106 GLubyte b = ctx->Color.ClearColor[2] * ctx->Visual->BlueScale;
107 GLubyte a = ctx->Color.ClearColor[3] * ctx->Visual->AlphaScale;
108 GLint i;
109 for (i=0;i<height;i++,y++) {
110 MEMSET( red, (int) r, width );
111 MEMSET( green, (int) g, width );
112 MEMSET( blue, (int) b, width );
113 MEMSET( alpha, (int) a, width );
114 gl_mask_color_span( ctx, width, x, y, red, green, blue, alpha );
115 (*ctx->Driver.WriteColorSpan)( ctx,
116 width, x, y, red, green, blue, alpha, NULL );
117 if (ctx->RasterMask & ALPHABUF_BIT) {
118 gl_write_alpha_span( ctx, width, x, y, alpha, NULL );
119 }
120 }
121 }
122 else {
123 /* Color index mode */
124 GLuint indx[MAX_WIDTH];
125 GLubyte mask[MAX_WIDTH];
126 GLint i, j;
127 MEMSET( mask, 1, width );
128 for (i=0;i<height;i++,y++) {
129 for (j=0;j<width;j++) {
130 indx[j] = ctx->Color.ClearIndex;
131 }
132 gl_mask_index_span( ctx, width, x, y, indx );
133 (*ctx->Driver.WriteIndexSpan)( ctx, width, x, y, indx, mask );
134 }
135 }
136 }
137
138
139
140 /*
141 * Clear the front and/or back color buffers. Also clear the alpha
142 * buffer(s) if present.
143 */
144 static void clear_color_buffers( GLcontext *ctx )
145 {
146 if (ctx->Color.SWmasking) {
147 clear_color_buffer_with_masking( ctx );
148 }
149 else {
150 GLint x = ctx->Buffer->Xmin;
151 GLint y = ctx->Buffer->Ymin;
152 GLint height = ctx->Buffer->Ymax - ctx->Buffer->Ymin + 1;
153 GLint width = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
154 (*ctx->Driver.Clear)( ctx, !ctx->Scissor.Enabled,
155 x, y, width, height );
156 if (ctx->RasterMask & ALPHABUF_BIT) {
157 /* front and/or back alpha buffers will be cleared here */
158 gl_clear_alpha_buffers( ctx );
159 }
160 }
161
162 if (ctx->RasterMask & FRONT_AND_BACK_BIT) {
163 /*** Also clear the back buffer ***/
164 (*ctx->Driver.SetBuffer)( ctx, GL_BACK );
165 if (ctx->Color.SWmasking) {
166 clear_color_buffer_with_masking( ctx );
167 }
168 else {
169 GLint x = ctx->Buffer->Xmin;
170 GLint y = ctx->Buffer->Ymin;
171 GLint height = ctx->Buffer->Ymax - ctx->Buffer->Ymin + 1;
172 GLint width = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
173 (*ctx->Driver.Clear)( ctx, !ctx->Scissor.Enabled,
174 x, y, width, height );
175 }
176 (*ctx->Driver.SetBuffer)( ctx, GL_FRONT );
177 }
178 }
179
180
181
182 void gl_Clear( GLcontext *ctx, GLbitfield mask )
183 {
184 #ifdef PROFILE
185 GLdouble t0 = gl_time();
186 #endif
187
188 if (INSIDE_BEGIN_END(ctx)) {
189 gl_error( ctx, GL_INVALID_OPERATION, "glClear" );
190 return;
191 }
192
193 if (ctx->NewState) {
194 gl_update_state( ctx );
195 }
196
197 if (mask & GL_COLOR_BUFFER_BIT) clear_color_buffers( ctx );
198 if (mask & GL_DEPTH_BUFFER_BIT) (*ctx->Driver.ClearDepthBuffer)( ctx );
199 if (mask & GL_ACCUM_BUFFER_BIT) gl_clear_accum_buffer( ctx );
200 if (mask & GL_STENCIL_BUFFER_BIT) gl_clear_stencil_buffer( ctx );
201
202 #ifdef PROFILE
203 ctx->ClearTime += gl_time() - t0;
204 ctx->ClearCount++;
205 #endif
206 }
207
208
209
210 const GLubyte *gl_GetString( GLcontext *ctx, GLenum name )
211 {
212 static char *vendor = "Brian Paul";
213 static char *renderer = "Mesa";
214 static char *version = "1.1 Mesa 2.2";
215 static char *extensions = "GL_EXT_blend_color GL_EXT_blend_minmax GL_EXT_blend_logic_op GL_EXT_blend_subtract GL_EXT_polygon_offset GL_EXT_vertex_array GL_EXT_texture_object GL_EXT_texture3D GL_MESA_window_pos GL_MESA_resize_buffers";
216
217 if (INSIDE_BEGIN_END(ctx)) {
218 gl_error( ctx, GL_INVALID_OPERATION, "glGetString" );
219 return (GLubyte *) 0;
220 }
221
222 switch (name) {
223 case GL_VENDOR:
224 return (GLubyte *) vendor;
225 case GL_RENDERER:
226 return (GLubyte *) renderer;
227 case GL_VERSION:
228 return (GLubyte *) version;
229 case GL_EXTENSIONS:
230 return (GLubyte *) extensions;
231 default:
232 gl_error( ctx, GL_INVALID_ENUM, "glGetString" );
233 return (GLubyte *) 0;
234 }
235 }
236
237
238
239 void gl_Finish( GLcontext *ctx )
240 {
241 /* Don't compile into display list */
242 if (INSIDE_BEGIN_END(ctx)) {
243 gl_error( ctx, GL_INVALID_OPERATION, "glFinish" );
244 return;
245 }
246 if (ctx->Driver.Finish) {
247 (*ctx->Driver.Finish)( ctx );
248 }
249 }
250
251
252
253 void gl_Flush( GLcontext *ctx )
254 {
255 /* Don't compile into display list */
256 if (INSIDE_BEGIN_END(ctx)) {
257 gl_error( ctx, GL_INVALID_OPERATION, "glFlush" );
258 return;
259 }
260 if (ctx->Driver.Flush) {
261 (*ctx->Driver.Flush)( ctx );
262 }
263 }
264
265
266
267 void gl_Hint( GLcontext *ctx, GLenum target, GLenum mode )
268 {
269 if (INSIDE_BEGIN_END(ctx)) {
270 gl_error( ctx, GL_INVALID_OPERATION, "glHint" );
271 return;
272 }
273 if (mode!=GL_DONT_CARE && mode!=GL_FASTEST && mode!=GL_NICEST) {
274 gl_error( ctx, GL_INVALID_ENUM, "glHint(mode)" );
275 return;
276 }
277 switch (target) {
278 case GL_FOG_HINT:
279 ctx->Hint.Fog = mode;
280 break;
281 case GL_LINE_SMOOTH_HINT:
282 ctx->Hint.LineSmooth = mode;
283 break;
284 case GL_PERSPECTIVE_CORRECTION_HINT:
285 ctx->Hint.PerspectiveCorrection = mode;
286 break;
287 case GL_POINT_SMOOTH_HINT:
288 ctx->Hint.PointSmooth = mode;
289 break;
290 case GL_POLYGON_SMOOTH_HINT:
291 ctx->Hint.PolygonSmooth = mode;
292 break;
293 default:
294 gl_error( ctx, GL_INVALID_ENUM, "glHint(target)" );
295 }
296 ctx->NewState |= NEW_ALL; /* just to be safe */
297 }
298
299
300
301 void gl_DrawBuffer( GLcontext *ctx, GLenum mode )
302 {
303 if (INSIDE_BEGIN_END(ctx)) {
304 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
305 return;
306 }
307 switch (mode) {
308 case GL_FRONT:
309 case GL_FRONT_LEFT:
310 case GL_FRONT_AND_BACK:
311 if ( (*ctx->Driver.SetBuffer)( ctx, GL_FRONT ) == GL_FALSE ) {
312 gl_error( ctx, GL_INVALID_ENUM, "glDrawBuffer" );
313 return;
314 }
315 ctx->Color.DrawBuffer = mode;
316 ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha;
317 ctx->NewState |= NEW_RASTER_OPS;
318 break;
319 case GL_BACK:
320 case GL_BACK_LEFT:
321 if ( (*ctx->Driver.SetBuffer)( ctx, GL_BACK ) == GL_FALSE) {
322 gl_error( ctx, GL_INVALID_ENUM, "glDrawBuffer" );
323 return;
324 }
325 ctx->Color.DrawBuffer = mode;
326 ctx->Buffer->Alpha = ctx->Buffer->BackAlpha;
327 ctx->NewState |= NEW_RASTER_OPS;
328 break;
329 case GL_NONE:
330 ctx->Color.DrawBuffer = mode;
331 ctx->Buffer->Alpha = NULL;
332 ctx->NewState |= NEW_RASTER_OPS;
333 break;
334 case GL_FRONT_RIGHT:
335 case GL_BACK_RIGHT:
336 case GL_LEFT:
337 case GL_RIGHT:
338 case GL_AUX0:
339 gl_error( ctx, GL_INVALID_OPERATION, "glDrawBuffer" );
340 break;
341 default:
342 gl_error( ctx, GL_INVALID_ENUM, "glDrawBuffer" );
343 }
344 }
345
346
347
348 void gl_ReadBuffer( GLcontext *ctx, GLenum mode )
349 {
350 if (INSIDE_BEGIN_END(ctx)) {
351 gl_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
352 return;
353 }
354 switch (mode) {
355 case GL_FRONT:
356 case GL_FRONT_LEFT:
357 if ( (*ctx->Driver.SetBuffer)( ctx, GL_FRONT ) == GL_FALSE) {
358 gl_error( ctx, GL_INVALID_ENUM, "glReadBuffer" );
359 return;
360 }
361 ctx->Pixel.ReadBuffer = mode;
362 ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha;
363 ctx->NewState |= NEW_RASTER_OPS;
364 break;
365 case GL_BACK:
366 case GL_BACK_LEFT:
367 if ( (*ctx->Driver.SetBuffer)( ctx, GL_BACK ) == GL_FALSE) {
368 gl_error( ctx, GL_INVALID_ENUM, "glReadBuffer" );
369 return;
370 }
371 ctx->Pixel.ReadBuffer = mode;
372 ctx->Buffer->Alpha = ctx->Buffer->BackAlpha;
373 ctx->NewState |= NEW_RASTER_OPS;
374 break;
375 case GL_FRONT_RIGHT:
376 case GL_BACK_RIGHT:
377 case GL_LEFT:
378 case GL_RIGHT:
379 case GL_AUX0:
380 gl_error( ctx, GL_INVALID_OPERATION, "glReadBuffer" );
381 break;
382 default:
383 gl_error( ctx, GL_INVALID_ENUM, "glReadBuffer" );
384 }
385
386 /* Remember, the draw buffer is the default state */
387 (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer );
388 }
389
390
391
392
393 void gl_Rectf( GLcontext *ctx, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2 )
394 {
395 if (INSIDE_BEGIN_END(ctx)) {
396 gl_error( ctx, GL_INVALID_OPERATION, "glRect" );
397 return;
398 }
399 (*ctx->API.Begin)( ctx, GL_QUADS );
400 (*ctx->API.Vertex4f)( ctx, x1, y1, 0.0F, 1.0F );
401 (*ctx->API.Vertex4f)( ctx, x2, y1, 0.0F, 1.0F );
402 (*ctx->API.Vertex4f)( ctx, x2, y2, 0.0F, 1.0F );
403 (*ctx->API.Vertex4f)( ctx, x1, y2, 0.0F, 1.0F );
404 (*ctx->API.End)( ctx );
405 }
406