b64424e39a1d30bf1935d64db6fe362a431166ba
1 /* $Id: s_aatriangle.c,v 1.11 2001/03/28 21:37:24 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 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
)
126 return (GLchan
) 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
)
142 static const GLfloat samples
[16][2] = {
143 /* start with the four corners */
148 /* continue with interior samples */
162 const GLfloat x
= (GLfloat
) winx
;
163 const GLfloat y
= (GLfloat
) winy
;
164 const GLfloat dx0
= v1
[0] - v0
[0];
165 const GLfloat dy0
= v1
[1] - v0
[1];
166 const GLfloat dx1
= v2
[0] - v1
[0];
167 const GLfloat dy1
= v2
[1] - v1
[1];
168 const GLfloat dx2
= v0
[0] - v2
[0];
169 const GLfloat dy2
= v0
[1] - v2
[1];
171 GLfloat insideCount
= 16.0F
;
175 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
180 for (i
= 0; i
< stop
; i
++) {
181 const GLfloat sx
= x
+ samples
[i
][0];
182 const GLfloat sy
= y
+ samples
[i
][1];
183 const GLfloat fx0
= sx
- v0
[0];
184 const GLfloat fy0
= sy
- v0
[1];
185 const GLfloat fx1
= sx
- v1
[0];
186 const GLfloat fy1
= sy
- v1
[1];
187 const GLfloat fx2
= sx
- v2
[0];
188 const GLfloat fy2
= sy
- v2
[1];
189 /* cross product determines if sample is inside or outside each edge */
190 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
191 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
192 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
193 /* Check if the sample is exactly on an edge. If so, let cross be a
194 * positive or negative value depending on the direction of the edge.
202 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
203 /* point is outside triangle */
211 return insideCount
* (1.0F
/ 16.0F
);
217 * Compute how much (area) of the given pixel is inside the triangle.
218 * Vertices MUST be specified in counter-clockwise order.
219 * Return: coverage in [0, 15].
222 compute_coveragei(const GLfloat v0
[3], const GLfloat v1
[3],
223 const GLfloat v2
[3], GLint winx
, GLint winy
)
225 /* NOTE: 15 samples instead of 16.
226 * A better sample distribution could be used.
228 static const GLfloat samples
[15][2] = {
229 /* start with the four corners */
234 /* continue with interior samples */
248 const GLfloat x
= (GLfloat
) winx
;
249 const GLfloat y
= (GLfloat
) winy
;
250 const GLfloat dx0
= v1
[0] - v0
[0];
251 const GLfloat dy0
= v1
[1] - v0
[1];
252 const GLfloat dx1
= v2
[0] - v1
[0];
253 const GLfloat dy1
= v2
[1] - v1
[1];
254 const GLfloat dx2
= v0
[0] - v2
[0];
255 const GLfloat dy2
= v0
[1] - v2
[1];
257 GLint insideCount
= 15;
261 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
266 for (i
= 0; i
< stop
; i
++) {
267 const GLfloat sx
= x
+ samples
[i
][0];
268 const GLfloat sy
= y
+ samples
[i
][1];
269 const GLfloat fx0
= sx
- v0
[0];
270 const GLfloat fy0
= sy
- v0
[1];
271 const GLfloat fx1
= sx
- v1
[0];
272 const GLfloat fy1
= sy
- v1
[1];
273 const GLfloat fx2
= sx
- v2
[0];
274 const GLfloat fy2
= sy
- v2
[1];
275 /* cross product determines if sample is inside or outside each edge */
276 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
277 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
278 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
279 /* Check if the sample is exactly on an edge. If so, let cross be a
280 * positive or negative value depending on the direction of the edge.
288 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
289 /* point is outside triangle */
303 rgba_aa_tri(GLcontext
*ctx
,
310 #include "s_aatritemp.h"
315 index_aa_tri(GLcontext
*ctx
,
322 #include "s_aatritemp.h"
327 * Compute mipmap level of detail.
329 static INLINE GLfloat
330 compute_lambda(const GLfloat sPlane
[4], const GLfloat tPlane
[4],
331 GLfloat invQ
, GLfloat width
, GLfloat height
)
333 GLfloat dudx
= sPlane
[0] / sPlane
[2] * invQ
* width
;
334 GLfloat dudy
= sPlane
[1] / sPlane
[2] * invQ
* width
;
335 GLfloat dvdx
= tPlane
[0] / tPlane
[2] * invQ
* height
;
336 GLfloat dvdy
= tPlane
[1] / tPlane
[2] * invQ
* height
;
337 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
338 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
339 GLfloat rho2
= r1
+ r2
;
340 /* return log base 2 of rho */
341 return log(rho2
) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
346 tex_aa_tri(GLcontext
*ctx
,
354 #include "s_aatritemp.h"
359 spec_tex_aa_tri(GLcontext
*ctx
,
368 #include "s_aatritemp.h"
373 multitex_aa_tri(GLcontext
*ctx
,
381 #include "s_aatritemp.h"
385 spec_multitex_aa_tri(GLcontext
*ctx
,
394 #include "s_aatritemp.h"
399 * Examine GL state and set swrast->Triangle to an
400 * appropriate antialiased triangle rasterizer function.
403 _mesa_set_aa_triangle_function(GLcontext
*ctx
)
405 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
406 ASSERT(ctx
->Polygon
.SmoothFlag
);
408 if (ctx
->Texture
._ReallyEnabled
) {
409 if (ctx
->_TriangleCaps
& DD_SEPERATE_SPECULAR
) {
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
;
426 else if (ctx
->Visual
.rgbMode
) {
427 SWRAST_CONTEXT(ctx
)->Triangle
= rgba_aa_tri
;
430 SWRAST_CONTEXT(ctx
)->Triangle
= index_aa_tri
;
433 ASSERT(SWRAST_CONTEXT(ctx
)->Triangle
);