1 /* $Id: s_aatriangle.c,v 1.7 2001/02/16 18:14:41 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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
34 #include "s_aatriangle.h"
35 #include "s_context.h"
40 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
41 * vertices and the given Z values.
44 compute_plane(const GLfloat v0
[], const GLfloat v1
[], const GLfloat v2
[],
45 GLfloat z0
, GLfloat z1
, GLfloat z2
, GLfloat plane
[4])
47 const GLfloat px
= v1
[0] - v0
[0];
48 const GLfloat py
= v1
[1] - v0
[1];
49 const GLfloat pz
= z1
- z0
;
51 const GLfloat qx
= v2
[0] - v0
[0];
52 const GLfloat qy
= v2
[1] - v0
[1];
53 const GLfloat qz
= z2
- z0
;
55 const GLfloat a
= py
* qz
- pz
* qy
;
56 const GLfloat b
= pz
* qx
- px
* qz
;
57 const GLfloat c
= px
* qy
- py
* qx
;
58 const GLfloat d
= -(a
* v0
[0] + b
* v0
[1] + c
* z0
);
68 * Compute coefficients of a plane with a constant Z value.
71 constant_plane(GLfloat value
, GLfloat plane
[4])
79 #define CONSTANT_PLANE(VALUE, PLANE) \
90 * Solve plane equation for Z at (X,Y).
93 solve_plane(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
95 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2];
100 #define SOLVE_PLANE(X, Y, PLANE) \
101 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
105 * Return 1 / solve_plane().
107 static INLINE GLfloat
108 solve_plane_recip(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
110 GLfloat z
= -plane
[2] / (plane
[3] + plane
[0] * x
+ plane
[1] * y
);
117 * Solve plane and return clamped GLchan value.
120 solve_plane_chan(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
122 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2] + 0.5F
;
125 else if (z
> CHAN_MAXF
)
127 return (GLchan
) (GLint
) z
;
133 * Compute how much (area) of the given pixel is inside the triangle.
134 * Vertices MUST be specified in counter-clockwise order.
135 * Return: coverage in [0, 1].
138 compute_coveragef(const GLfloat v0
[3], const GLfloat v1
[3],
139 const GLfloat v2
[3], GLint winx
, GLint winy
)
141 static const GLfloat samples
[16][2] = {
142 /* start with the four corners */
147 /* continue with interior samples */
161 const GLfloat x
= (GLfloat
) winx
;
162 const GLfloat y
= (GLfloat
) winy
;
163 const GLfloat dx0
= v1
[0] - v0
[0];
164 const GLfloat dy0
= v1
[1] - v0
[1];
165 const GLfloat dx1
= v2
[0] - v1
[0];
166 const GLfloat dy1
= v2
[1] - v1
[1];
167 const GLfloat dx2
= v0
[0] - v2
[0];
168 const GLfloat dy2
= v0
[1] - v2
[1];
170 GLfloat insideCount
= 16.0F
;
174 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
179 for (i
= 0; i
< stop
; i
++) {
180 const GLfloat sx
= x
+ samples
[i
][0];
181 const GLfloat sy
= y
+ samples
[i
][1];
182 const GLfloat fx0
= sx
- v0
[0];
183 const GLfloat fy0
= sy
- v0
[1];
184 const GLfloat fx1
= sx
- v1
[0];
185 const GLfloat fy1
= sy
- v1
[1];
186 const GLfloat fx2
= sx
- v2
[0];
187 const GLfloat fy2
= sy
- v2
[1];
188 /* cross product determines if sample is inside or outside each edge */
189 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
190 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
191 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
192 /* Check if the sample is exactly on an edge. If so, let cross be a
193 * positive or negative value depending on the direction of the edge.
201 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
202 /* point is outside triangle */
210 return insideCount
* (1.0F
/ 16.0F
);
216 * Compute how much (area) of the given pixel is inside the triangle.
217 * Vertices MUST be specified in counter-clockwise order.
218 * Return: coverage in [0, 15].
221 compute_coveragei(const GLfloat v0
[3], const GLfloat v1
[3],
222 const GLfloat v2
[3], GLint winx
, GLint winy
)
224 /* NOTE: 15 samples instead of 16.
225 * A better sample distribution could be used.
227 static const GLfloat samples
[15][2] = {
228 /* start with the four corners */
233 /* continue with interior samples */
247 const GLfloat x
= (GLfloat
) winx
;
248 const GLfloat y
= (GLfloat
) winy
;
249 const GLfloat dx0
= v1
[0] - v0
[0];
250 const GLfloat dy0
= v1
[1] - v0
[1];
251 const GLfloat dx1
= v2
[0] - v1
[0];
252 const GLfloat dy1
= v2
[1] - v1
[1];
253 const GLfloat dx2
= v0
[0] - v2
[0];
254 const GLfloat dy2
= v0
[1] - v2
[1];
256 GLint insideCount
= 15;
260 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
265 for (i
= 0; i
< stop
; i
++) {
266 const GLfloat sx
= x
+ samples
[i
][0];
267 const GLfloat sy
= y
+ samples
[i
][1];
268 const GLfloat fx0
= sx
- v0
[0];
269 const GLfloat fy0
= sy
- v0
[1];
270 const GLfloat fx1
= sx
- v1
[0];
271 const GLfloat fy1
= sy
- v1
[1];
272 const GLfloat fx2
= sx
- v2
[0];
273 const GLfloat fy2
= sy
- v2
[1];
274 /* cross product determines if sample is inside or outside each edge */
275 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
276 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
277 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
278 /* Check if the sample is exactly on an edge. If so, let cross be a
279 * positive or negative value depending on the direction of the edge.
287 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
288 /* point is outside triangle */
302 rgba_aa_tri(GLcontext
*ctx
,
309 #include "s_aatritemp.h"
314 index_aa_tri(GLcontext
*ctx
,
321 #include "s_aatritemp.h"
326 * Compute mipmap level of detail.
328 static INLINE GLfloat
329 compute_lambda(const GLfloat sPlane
[4], const GLfloat tPlane
[4],
330 GLfloat invQ
, GLfloat width
, GLfloat height
)
332 GLfloat dudx
= sPlane
[0] / sPlane
[2] * invQ
* width
;
333 GLfloat dudy
= sPlane
[1] / sPlane
[2] * invQ
* width
;
334 GLfloat dvdx
= tPlane
[0] / tPlane
[2] * invQ
* height
;
335 GLfloat dvdy
= tPlane
[1] / tPlane
[2] * invQ
* height
;
336 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
337 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
338 GLfloat rho2
= r1
+ r2
;
339 /* return log base 2 of rho */
340 return log(rho2
) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
345 tex_aa_tri(GLcontext
*ctx
,
353 #include "s_aatritemp.h"
358 spec_tex_aa_tri(GLcontext
*ctx
,
367 #include "s_aatritemp.h"
372 multitex_aa_tri(GLcontext
*ctx
,
380 #include "s_aatritemp.h"
384 spec_multitex_aa_tri(GLcontext
*ctx
,
393 #include "s_aatritemp.h"
398 * Examine GL state and set ctx->Driver.TriangleFunc to an
399 * appropriate antialiased triangle rasterizer function.
402 _mesa_set_aa_triangle_function(GLcontext
*ctx
)
404 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
405 ASSERT(ctx
->Polygon
.SmoothFlag
);
407 if (ctx
->Texture
._ReallyEnabled
) {
408 if (ctx
->_TriangleCaps
& DD_SEPERATE_SPECULAR
) {
409 if (swrast
->_MultiTextureEnabled
) {
410 SWRAST_CONTEXT(ctx
)->Triangle
= spec_multitex_aa_tri
;
413 SWRAST_CONTEXT(ctx
)->Triangle
= spec_tex_aa_tri
;
417 if (swrast
->_MultiTextureEnabled
) {
418 SWRAST_CONTEXT(ctx
)->Triangle
= multitex_aa_tri
;
421 SWRAST_CONTEXT(ctx
)->Triangle
= tex_aa_tri
;
425 else if (ctx
->Visual
.rgbMode
) {
426 SWRAST_CONTEXT(ctx
)->Triangle
= rgba_aa_tri
;
429 SWRAST_CONTEXT(ctx
)->Triangle
= index_aa_tri
;
432 ASSERT(SWRAST_CONTEXT(ctx
)->Triangle
);