1 /* $Id: s_aatriangle.c,v 1.3 2000/11/13 20:02:57 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Antialiased Triangle rasterizers
33 #include "s_aatriangle.h"
34 #include "s_context.h"
39 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
40 * vertices and the given Z values.
43 compute_plane(const GLfloat v0
[], const GLfloat v1
[], const GLfloat v2
[],
44 GLfloat z0
, GLfloat z1
, GLfloat z2
, GLfloat plane
[4])
46 const GLfloat px
= v1
[0] - v0
[0];
47 const GLfloat py
= v1
[1] - v0
[1];
48 const GLfloat pz
= z1
- z0
;
50 const GLfloat qx
= v2
[0] - v0
[0];
51 const GLfloat qy
= v2
[1] - v0
[1];
52 const GLfloat qz
= z2
- z0
;
54 const GLfloat a
= py
* qz
- pz
* qy
;
55 const GLfloat b
= pz
* qx
- px
* qz
;
56 const GLfloat c
= px
* qy
- py
* qx
;
57 const GLfloat d
= -(a
* v0
[0] + b
* v0
[1] + c
* z0
);
67 * Compute coefficients of a plane with a constant Z value.
70 constant_plane(GLfloat value
, GLfloat plane
[4])
78 #define CONSTANT_PLANE(VALUE, PLANE) \
89 * Solve plane equation for Z at (X,Y).
92 solve_plane(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
94 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2];
99 #define SOLVE_PLANE(X, Y, PLANE) \
100 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
104 * Return 1 / solve_plane().
106 static INLINE GLfloat
107 solve_plane_recip(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
109 GLfloat z
= -plane
[2] / (plane
[3] + plane
[0] * x
+ plane
[1] * y
);
116 * Solve plane and return clamped GLchan value.
119 solve_plane_chan(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
121 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2] + 0.5F
;
124 else if (z
> CHAN_MAXF
)
126 return (GLchan
) (GLint
) z
;
132 * Compute how much (area) of the given pixel is inside the triangle.
133 * Vertices MUST be specified in counter-clockwise order.
134 * Return: coverage in [0, 1].
137 compute_coveragef(const GLfloat v0
[3], const GLfloat v1
[3],
138 const GLfloat v2
[3], GLint winx
, GLint winy
)
140 static const GLfloat samples
[16][2] = {
141 /* start with the four corners */
146 /* continue with interior samples */
160 const GLfloat x
= (GLfloat
) winx
;
161 const GLfloat y
= (GLfloat
) winy
;
162 const GLfloat dx0
= v1
[0] - v0
[0];
163 const GLfloat dy0
= v1
[1] - v0
[1];
164 const GLfloat dx1
= v2
[0] - v1
[0];
165 const GLfloat dy1
= v2
[1] - v1
[1];
166 const GLfloat dx2
= v0
[0] - v2
[0];
167 const GLfloat dy2
= v0
[1] - v2
[1];
169 GLfloat insideCount
= 16.0F
;
173 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
178 for (i
= 0; i
< stop
; i
++) {
179 const GLfloat sx
= x
+ samples
[i
][0];
180 const GLfloat sy
= y
+ samples
[i
][1];
181 const GLfloat fx0
= sx
- v0
[0];
182 const GLfloat fy0
= sy
- v0
[1];
183 const GLfloat fx1
= sx
- v1
[0];
184 const GLfloat fy1
= sy
- v1
[1];
185 const GLfloat fx2
= sx
- v2
[0];
186 const GLfloat fy2
= sy
- v2
[1];
187 /* cross product determines if sample is inside or outside each edge */
188 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
189 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
190 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
191 /* Check if the sample is exactly on an edge. If so, let cross be a
192 * positive or negative value depending on the direction of the edge.
200 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
201 /* point is outside triangle */
209 return insideCount
* (1.0F
/ 16.0F
);
215 * Compute how much (area) of the given pixel is inside the triangle.
216 * Vertices MUST be specified in counter-clockwise order.
217 * Return: coverage in [0, 15].
220 compute_coveragei(const GLfloat v0
[3], const GLfloat v1
[3],
221 const GLfloat v2
[3], GLint winx
, GLint winy
)
223 /* NOTE: 15 samples instead of 16.
224 * A better sample distribution could be used.
226 static const GLfloat samples
[15][2] = {
227 /* start with the four corners */
232 /* continue with interior samples */
246 const GLfloat x
= (GLfloat
) winx
;
247 const GLfloat y
= (GLfloat
) winy
;
248 const GLfloat dx0
= v1
[0] - v0
[0];
249 const GLfloat dy0
= v1
[1] - v0
[1];
250 const GLfloat dx1
= v2
[0] - v1
[0];
251 const GLfloat dy1
= v2
[1] - v1
[1];
252 const GLfloat dx2
= v0
[0] - v2
[0];
253 const GLfloat dy2
= v0
[1] - v2
[1];
255 GLint insideCount
= 15;
259 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
264 for (i
= 0; i
< stop
; i
++) {
265 const GLfloat sx
= x
+ samples
[i
][0];
266 const GLfloat sy
= y
+ samples
[i
][1];
267 const GLfloat fx0
= sx
- v0
[0];
268 const GLfloat fy0
= sy
- v0
[1];
269 const GLfloat fx1
= sx
- v1
[0];
270 const GLfloat fy1
= sy
- v1
[1];
271 const GLfloat fx2
= sx
- v2
[0];
272 const GLfloat fy2
= sy
- v2
[1];
273 /* cross product determines if sample is inside or outside each edge */
274 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
275 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
276 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
277 /* Check if the sample is exactly on an edge. If so, let cross be a
278 * positive or negative value depending on the direction of the edge.
286 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
287 /* point is outside triangle */
301 rgba_aa_tri(GLcontext
*ctx
,
308 #include "s_aatritemp.h"
313 index_aa_tri(GLcontext
*ctx
,
320 #include "s_aatritemp.h"
325 * Compute mipmap level of detail.
327 static INLINE GLfloat
328 compute_lambda(const GLfloat sPlane
[4], const GLfloat tPlane
[4],
329 GLfloat invQ
, GLfloat width
, GLfloat height
)
331 GLfloat dudx
= sPlane
[0] / sPlane
[2] * invQ
* width
;
332 GLfloat dudy
= sPlane
[1] / sPlane
[2] * invQ
* width
;
333 GLfloat dvdx
= tPlane
[0] / tPlane
[2] * invQ
* height
;
334 GLfloat dvdy
= tPlane
[1] / tPlane
[2] * invQ
* height
;
335 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
336 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
337 GLfloat rho2
= r1
+ r2
;
338 /* return log base 2 of rho */
339 return log(rho2
) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
344 tex_aa_tri(GLcontext
*ctx
,
352 #include "s_aatritemp.h"
357 spec_tex_aa_tri(GLcontext
*ctx
,
366 #include "s_aatritemp.h"
371 multitex_aa_tri(GLcontext
*ctx
,
379 #include "s_aatritemp.h"
383 spec_multitex_aa_tri(GLcontext
*ctx
,
392 #include "s_aatritemp.h"
397 * Examine GL state and set ctx->Driver.TriangleFunc to an
398 * appropriate antialiased triangle rasterizer function.
401 _mesa_set_aa_triangle_function(GLcontext
*ctx
)
403 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
404 ASSERT(ctx
->Polygon
.SmoothFlag
);
406 if (ctx
->Texture
._ReallyEnabled
) {
407 if (ctx
->Light
.Enabled
&&
408 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
||
409 ctx
->Fog
.ColorSumEnabled
)) {
410 if (swrast
->_MultiTextureEnabled
) {
411 SWRAST_CONTEXT(ctx
)->Triangle
= spec_multitex_aa_tri
;
414 SWRAST_CONTEXT(ctx
)->Triangle
= spec_tex_aa_tri
;
418 if (swrast
->_MultiTextureEnabled
) {
419 SWRAST_CONTEXT(ctx
)->Triangle
= multitex_aa_tri
;
422 SWRAST_CONTEXT(ctx
)->Triangle
= tex_aa_tri
;
427 if (ctx
->Visual
.RGBAflag
) {
428 SWRAST_CONTEXT(ctx
)->Triangle
= rgba_aa_tri
;
431 SWRAST_CONTEXT(ctx
)->Triangle
= index_aa_tri
;
434 ASSERT(SWRAST_CONTEXT(ctx
)->Triangle
);