1 /* $Id: s_aatriangle.c,v 1.14 2001/05/10 17:41:41 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
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 const 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 const GLfloat denom
= plane
[3] + plane
[0] * x
+ plane
[1] * y
;
114 return -plane
[2] / denom
;
120 * Solve plane and return clamped GLchan value.
123 solve_plane_chan(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
125 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2] + 0.5F
;
128 else if (z
> CHAN_MAXF
)
129 return (GLchan
) CHAN_MAXF
;
130 return (GLchan
) (GLint
) z
;
136 * Compute how much (area) of the given pixel is inside the triangle.
137 * Vertices MUST be specified in counter-clockwise order.
138 * Return: coverage in [0, 1].
141 compute_coveragef(const GLfloat v0
[3], const GLfloat v1
[3],
142 const GLfloat v2
[3], GLint winx
, GLint winy
)
145 static const GLfloat samples
[16][2] = {
146 /* start with the four corners */
151 /* continue with interior samples */
165 const GLfloat x
= (GLfloat
) winx
;
166 const GLfloat y
= (GLfloat
) winy
;
167 const GLfloat dx0
= v1
[0] - v0
[0];
168 const GLfloat dy0
= v1
[1] - v0
[1];
169 const GLfloat dx1
= v2
[0] - v1
[0];
170 const GLfloat dy1
= v2
[1] - v1
[1];
171 const GLfloat dx2
= v0
[0] - v2
[0];
172 const GLfloat dy2
= v0
[1] - v2
[1];
174 GLfloat insideCount
= 16.0F
;
178 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
183 for (i
= 0; i
< stop
; i
++) {
184 const GLfloat sx
= x
+ samples
[i
][0];
185 const GLfloat sy
= y
+ samples
[i
][1];
186 const GLfloat fx0
= sx
- v0
[0];
187 const GLfloat fy0
= sy
- v0
[1];
188 const GLfloat fx1
= sx
- v1
[0];
189 const GLfloat fy1
= sy
- v1
[1];
190 const GLfloat fx2
= sx
- v2
[0];
191 const GLfloat fy2
= sy
- v2
[1];
192 /* cross product determines if sample is inside or outside each edge */
193 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
194 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
195 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
196 /* Check if the sample is exactly on an edge. If so, let cross be a
197 * positive or negative value depending on the direction of the edge.
205 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
206 /* point is outside triangle */
214 return insideCount
* (1.0F
/ 16.0F
);
220 * Compute how much (area) of the given pixel is inside the triangle.
221 * Vertices MUST be specified in counter-clockwise order.
222 * Return: coverage in [0, 15].
225 compute_coveragei(const GLfloat v0
[3], const GLfloat v1
[3],
226 const GLfloat v2
[3], GLint winx
, GLint winy
)
228 /* NOTE: 15 samples instead of 16.
229 * A better sample distribution could be used.
231 static const GLfloat samples
[15][2] = {
232 /* start with the four corners */
237 /* continue with interior samples */
251 const GLfloat x
= (GLfloat
) winx
;
252 const GLfloat y
= (GLfloat
) winy
;
253 const GLfloat dx0
= v1
[0] - v0
[0];
254 const GLfloat dy0
= v1
[1] - v0
[1];
255 const GLfloat dx1
= v2
[0] - v1
[0];
256 const GLfloat dy1
= v2
[1] - v1
[1];
257 const GLfloat dx2
= v0
[0] - v2
[0];
258 const GLfloat dy2
= v0
[1] - v2
[1];
260 GLint insideCount
= 15;
264 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
269 for (i
= 0; i
< stop
; i
++) {
270 const GLfloat sx
= x
+ samples
[i
][0];
271 const GLfloat sy
= y
+ samples
[i
][1];
272 const GLfloat fx0
= sx
- v0
[0];
273 const GLfloat fy0
= sy
- v0
[1];
274 const GLfloat fx1
= sx
- v1
[0];
275 const GLfloat fy1
= sy
- v1
[1];
276 const GLfloat fx2
= sx
- v2
[0];
277 const GLfloat fy2
= sy
- v2
[1];
278 /* cross product determines if sample is inside or outside each edge */
279 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
280 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
281 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
282 /* Check if the sample is exactly on an edge. If so, let cross be a
283 * positive or negative value depending on the direction of the edge.
291 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
292 /* point is outside triangle */
306 rgba_aa_tri(GLcontext
*ctx
,
313 #include "s_aatritemp.h"
318 index_aa_tri(GLcontext
*ctx
,
325 #include "s_aatritemp.h"
330 * Compute mipmap level of detail.
332 static INLINE GLfloat
333 compute_lambda(const GLfloat sPlane
[4], const GLfloat tPlane
[4],
334 GLfloat invQ
, GLfloat width
, GLfloat height
)
336 GLfloat dudx
= sPlane
[0] / sPlane
[2] * invQ
* width
;
337 GLfloat dudy
= sPlane
[1] / sPlane
[2] * invQ
* width
;
338 GLfloat dvdx
= tPlane
[0] / tPlane
[2] * invQ
* height
;
339 GLfloat dvdy
= tPlane
[1] / tPlane
[2] * invQ
* height
;
340 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
341 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
342 GLfloat rho2
= r1
+ r2
;
343 /* return log base 2 of rho */
347 return log(rho2
) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
352 tex_aa_tri(GLcontext
*ctx
,
360 #include "s_aatritemp.h"
365 spec_tex_aa_tri(GLcontext
*ctx
,
374 #include "s_aatritemp.h"
379 multitex_aa_tri(GLcontext
*ctx
,
387 #include "s_aatritemp.h"
391 spec_multitex_aa_tri(GLcontext
*ctx
,
400 #include "s_aatritemp.h"
405 * Examine GL state and set swrast->Triangle to an
406 * appropriate antialiased triangle rasterizer function.
409 _mesa_set_aa_triangle_function(GLcontext
*ctx
)
411 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
412 ASSERT(ctx
->Polygon
.SmoothFlag
);
414 if (ctx
->Texture
._ReallyEnabled
) {
415 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
416 if (swrast
->_MultiTextureEnabled
) {
417 SWRAST_CONTEXT(ctx
)->Triangle
= spec_multitex_aa_tri
;
420 SWRAST_CONTEXT(ctx
)->Triangle
= spec_tex_aa_tri
;
424 if (swrast
->_MultiTextureEnabled
) {
425 SWRAST_CONTEXT(ctx
)->Triangle
= multitex_aa_tri
;
428 SWRAST_CONTEXT(ctx
)->Triangle
= tex_aa_tri
;
432 else if (ctx
->Visual
.rgbMode
) {
433 SWRAST_CONTEXT(ctx
)->Triangle
= rgba_aa_tri
;
436 SWRAST_CONTEXT(ctx
)->Triangle
= index_aa_tri
;
439 ASSERT(SWRAST_CONTEXT(ctx
)->Triangle
);