1 /* $Id: s_aatriangle.c,v 1.18 2001/05/29 15:23:15 brianp 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
35 #include "s_aatriangle.h"
36 #include "s_context.h"
41 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
42 * vertices and the given Z values.
45 compute_plane(const GLfloat v0
[], const GLfloat v1
[], const GLfloat v2
[],
46 GLfloat z0
, GLfloat z1
, GLfloat z2
, GLfloat plane
[4])
48 const GLfloat px
= v1
[0] - v0
[0];
49 const GLfloat py
= v1
[1] - v0
[1];
50 const GLfloat pz
= z1
- z0
;
52 const GLfloat qx
= v2
[0] - v0
[0];
53 const GLfloat qy
= v2
[1] - v0
[1];
54 const GLfloat qz
= z2
- z0
;
56 const GLfloat a
= py
* qz
- pz
* qy
;
57 const GLfloat b
= pz
* qx
- px
* qz
;
58 const GLfloat c
= px
* qy
- py
* qx
;
59 const GLfloat d
= -(a
* v0
[0] + b
* v0
[1] + c
* z0
);
69 * Compute coefficients of a plane with a constant Z value.
72 constant_plane(GLfloat value
, GLfloat plane
[4])
80 #define CONSTANT_PLANE(VALUE, PLANE) \
91 * Solve plane equation for Z at (X,Y).
94 solve_plane(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
96 const GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2];
101 #define SOLVE_PLANE(X, Y, PLANE) \
102 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
106 * Return 1 / solve_plane().
108 static INLINE GLfloat
109 solve_plane_recip(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
111 const GLfloat denom
= plane
[3] + plane
[0] * x
+ plane
[1] * y
;
115 return -plane
[2] / denom
;
121 * Solve plane and return clamped GLchan value.
124 solve_plane_chan(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
126 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2] + 0.5F
;
129 else if (z
> CHAN_MAXF
)
130 return (GLchan
) CHAN_MAXF
;
131 return (GLchan
) (GLint
) z
;
137 * Compute how much (area) of the given pixel is inside the triangle.
138 * Vertices MUST be specified in counter-clockwise order.
139 * Return: coverage in [0, 1].
142 compute_coveragef(const GLfloat v0
[3], const GLfloat v1
[3],
143 const GLfloat v2
[3], GLint winx
, GLint winy
)
146 static const GLfloat samples
[16][2] = {
147 /* start with the four corners */
152 /* continue with interior samples */
166 const GLfloat x
= (GLfloat
) winx
;
167 const GLfloat y
= (GLfloat
) winy
;
168 const GLfloat dx0
= v1
[0] - v0
[0];
169 const GLfloat dy0
= v1
[1] - v0
[1];
170 const GLfloat dx1
= v2
[0] - v1
[0];
171 const GLfloat dy1
= v2
[1] - v1
[1];
172 const GLfloat dx2
= v0
[0] - v2
[0];
173 const GLfloat dy2
= v0
[1] - v2
[1];
175 GLfloat insideCount
= 16.0F
;
179 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
184 for (i
= 0; i
< stop
; i
++) {
185 const GLfloat sx
= x
+ samples
[i
][0];
186 const GLfloat sy
= y
+ samples
[i
][1];
187 const GLfloat fx0
= sx
- v0
[0];
188 const GLfloat fy0
= sy
- v0
[1];
189 const GLfloat fx1
= sx
- v1
[0];
190 const GLfloat fy1
= sy
- v1
[1];
191 const GLfloat fx2
= sx
- v2
[0];
192 const GLfloat fy2
= sy
- v2
[1];
193 /* cross product determines if sample is inside or outside each edge */
194 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
195 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
196 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
197 /* Check if the sample is exactly on an edge. If so, let cross be a
198 * positive or negative value depending on the direction of the edge.
206 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
207 /* point is outside triangle */
215 return insideCount
* (1.0F
/ 16.0F
);
221 * Compute how much (area) of the given pixel is inside the triangle.
222 * Vertices MUST be specified in counter-clockwise order.
223 * Return: coverage in [0, 15].
226 compute_coveragei(const GLfloat v0
[3], const GLfloat v1
[3],
227 const GLfloat v2
[3], GLint winx
, GLint winy
)
229 /* NOTE: 15 samples instead of 16.
230 * A better sample distribution could be used.
232 static const GLfloat samples
[15][2] = {
233 /* start with the four corners */
238 /* continue with interior samples */
252 const GLfloat x
= (GLfloat
) winx
;
253 const GLfloat y
= (GLfloat
) winy
;
254 const GLfloat dx0
= v1
[0] - v0
[0];
255 const GLfloat dy0
= v1
[1] - v0
[1];
256 const GLfloat dx1
= v2
[0] - v1
[0];
257 const GLfloat dy1
= v2
[1] - v1
[1];
258 const GLfloat dx2
= v0
[0] - v2
[0];
259 const GLfloat dy2
= v0
[1] - v2
[1];
261 GLint insideCount
= 15;
265 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
270 for (i
= 0; i
< stop
; i
++) {
271 const GLfloat sx
= x
+ samples
[i
][0];
272 const GLfloat sy
= y
+ samples
[i
][1];
273 const GLfloat fx0
= sx
- v0
[0];
274 const GLfloat fy0
= sy
- v0
[1];
275 const GLfloat fx1
= sx
- v1
[0];
276 const GLfloat fy1
= sy
- v1
[1];
277 const GLfloat fx2
= sx
- v2
[0];
278 const GLfloat fy2
= sy
- v2
[1];
279 /* cross product determines if sample is inside or outside each edge */
280 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
281 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
282 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
283 /* Check if the sample is exactly on an edge. If so, let cross be a
284 * positive or negative value depending on the direction of the edge.
292 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
293 /* point is outside triangle */
307 rgba_aa_tri(GLcontext
*ctx
,
315 #include "s_aatritemp.h"
320 index_aa_tri(GLcontext
*ctx
,
328 #include "s_aatritemp.h"
333 * Compute mipmap level of detail.
335 static INLINE GLfloat
336 compute_lambda(const GLfloat sPlane
[4], const GLfloat tPlane
[4],
337 GLfloat invQ
, GLfloat width
, GLfloat height
)
339 GLfloat dudx
= sPlane
[0] / sPlane
[2] * invQ
* width
;
340 GLfloat dudy
= sPlane
[1] / sPlane
[2] * invQ
* width
;
341 GLfloat dvdx
= tPlane
[0] / tPlane
[2] * invQ
* height
;
342 GLfloat dvdy
= tPlane
[1] / tPlane
[2] * invQ
* height
;
343 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
344 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
345 GLfloat rho2
= r1
+ r2
;
346 /* return log base 2 of rho */
350 return log(rho2
) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
355 tex_aa_tri(GLcontext
*ctx
,
364 #include "s_aatritemp.h"
369 spec_tex_aa_tri(GLcontext
*ctx
,
379 #include "s_aatritemp.h"
384 multitex_aa_tri(GLcontext
*ctx
,
393 #include "s_aatritemp.h"
397 spec_multitex_aa_tri(GLcontext
*ctx
,
407 #include "s_aatritemp.h"
412 * Examine GL state and set swrast->Triangle to an
413 * appropriate antialiased triangle rasterizer function.
416 _mesa_set_aa_triangle_function(GLcontext
*ctx
)
418 ASSERT(ctx
->Polygon
.SmoothFlag
);
420 if (ctx
->Texture
._ReallyEnabled
) {
421 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
422 if (ctx
->Texture
._ReallyEnabled
> TEXTURE0_ANY
) {
423 SWRAST_CONTEXT(ctx
)->Triangle
= spec_multitex_aa_tri
;
426 SWRAST_CONTEXT(ctx
)->Triangle
= spec_tex_aa_tri
;
430 if (ctx
->Texture
._ReallyEnabled
> TEXTURE0_ANY
) {
431 SWRAST_CONTEXT(ctx
)->Triangle
= multitex_aa_tri
;
434 SWRAST_CONTEXT(ctx
)->Triangle
= tex_aa_tri
;
438 else if (ctx
->Visual
.rgbMode
) {
439 SWRAST_CONTEXT(ctx
)->Triangle
= rgba_aa_tri
;
442 SWRAST_CONTEXT(ctx
)->Triangle
= index_aa_tri
;
445 ASSERT(SWRAST_CONTEXT(ctx
)->Triangle
);