1 /* $Id: s_aatriangle.c,v 1.1 2000/10/31 18:00:04 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"
38 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
39 * vertices and the given Z values.
42 compute_plane(const GLfloat v0
[], const GLfloat v1
[], const GLfloat v2
[],
43 GLfloat z0
, GLfloat z1
, GLfloat z2
, GLfloat plane
[4])
45 const GLfloat px
= v1
[0] - v0
[0];
46 const GLfloat py
= v1
[1] - v0
[1];
47 const GLfloat pz
= z1
- z0
;
49 const GLfloat qx
= v2
[0] - v0
[0];
50 const GLfloat qy
= v2
[1] - v0
[1];
51 const GLfloat qz
= z2
- z0
;
53 const GLfloat a
= py
* qz
- pz
* qy
;
54 const GLfloat b
= pz
* qx
- px
* qz
;
55 const GLfloat c
= px
* qy
- py
* qx
;
56 const GLfloat d
= -(a
* v0
[0] + b
* v0
[1] + c
* z0
);
66 * Compute coefficients of a plane with a constant Z value.
69 constant_plane(GLfloat value
, GLfloat plane
[4])
77 #define CONSTANT_PLANE(VALUE, PLANE) \
88 * Solve plane equation for Z at (X,Y).
91 solve_plane(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
93 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2];
98 #define SOLVE_PLANE(X, Y, PLANE) \
99 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
103 * Return 1 / solve_plane().
105 static INLINE GLfloat
106 solve_plane_recip(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
108 GLfloat z
= -plane
[2] / (plane
[3] + plane
[0] * x
+ plane
[1] * y
);
115 * Solve plane and return clamped GLchan value.
118 solve_plane_chan(GLfloat x
, GLfloat y
, const GLfloat plane
[4])
120 GLfloat z
= (plane
[3] + plane
[0] * x
+ plane
[1] * y
) / -plane
[2] + 0.5F
;
123 else if (z
> CHAN_MAXF
)
125 return (GLchan
) (GLint
) z
;
131 * Compute how much (area) of the given pixel is inside the triangle.
132 * Vertices MUST be specified in counter-clockwise order.
133 * Return: coverage in [0, 1].
136 compute_coveragef(const GLfloat v0
[3], const GLfloat v1
[3],
137 const GLfloat v2
[3], GLint winx
, GLint winy
)
139 static const GLfloat samples
[16][2] = {
140 /* start with the four corners */
145 /* continue with interior samples */
159 const GLfloat x
= (GLfloat
) winx
;
160 const GLfloat y
= (GLfloat
) winy
;
161 const GLfloat dx0
= v1
[0] - v0
[0];
162 const GLfloat dy0
= v1
[1] - v0
[1];
163 const GLfloat dx1
= v2
[0] - v1
[0];
164 const GLfloat dy1
= v2
[1] - v1
[1];
165 const GLfloat dx2
= v0
[0] - v2
[0];
166 const GLfloat dy2
= v0
[1] - v2
[1];
168 GLfloat insideCount
= 16.0F
;
172 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
177 for (i
= 0; i
< stop
; i
++) {
178 const GLfloat sx
= x
+ samples
[i
][0];
179 const GLfloat sy
= y
+ samples
[i
][1];
180 const GLfloat fx0
= sx
- v0
[0];
181 const GLfloat fy0
= sy
- v0
[1];
182 const GLfloat fx1
= sx
- v1
[0];
183 const GLfloat fy1
= sy
- v1
[1];
184 const GLfloat fx2
= sx
- v2
[0];
185 const GLfloat fy2
= sy
- v2
[1];
186 /* cross product determines if sample is inside or outside each edge */
187 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
188 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
189 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
190 /* Check if the sample is exactly on an edge. If so, let cross be a
191 * positive or negative value depending on the direction of the edge.
199 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
200 /* point is outside triangle */
208 return insideCount
* (1.0F
/ 16.0F
);
214 * Compute how much (area) of the given pixel is inside the triangle.
215 * Vertices MUST be specified in counter-clockwise order.
216 * Return: coverage in [0, 15].
219 compute_coveragei(const GLfloat v0
[3], const GLfloat v1
[3],
220 const GLfloat v2
[3], GLint winx
, GLint winy
)
222 /* NOTE: 15 samples instead of 16.
223 * A better sample distribution could be used.
225 static const GLfloat samples
[15][2] = {
226 /* start with the four corners */
231 /* continue with interior samples */
245 const GLfloat x
= (GLfloat
) winx
;
246 const GLfloat y
= (GLfloat
) winy
;
247 const GLfloat dx0
= v1
[0] - v0
[0];
248 const GLfloat dy0
= v1
[1] - v0
[1];
249 const GLfloat dx1
= v2
[0] - v1
[0];
250 const GLfloat dy1
= v2
[1] - v1
[1];
251 const GLfloat dx2
= v0
[0] - v2
[0];
252 const GLfloat dy2
= v0
[1] - v2
[1];
254 GLint insideCount
= 15;
258 const GLfloat area
= dx0
* dy1
- dx1
* dy0
;
263 for (i
= 0; i
< stop
; i
++) {
264 const GLfloat sx
= x
+ samples
[i
][0];
265 const GLfloat sy
= y
+ samples
[i
][1];
266 const GLfloat fx0
= sx
- v0
[0];
267 const GLfloat fy0
= sy
- v0
[1];
268 const GLfloat fx1
= sx
- v1
[0];
269 const GLfloat fy1
= sy
- v1
[1];
270 const GLfloat fx2
= sx
- v2
[0];
271 const GLfloat fy2
= sy
- v2
[1];
272 /* cross product determines if sample is inside or outside each edge */
273 GLfloat cross0
= (dx0
* fy0
- dy0
* fx0
);
274 GLfloat cross1
= (dx1
* fy1
- dy1
* fx1
);
275 GLfloat cross2
= (dx2
* fy2
- dy2
* fx2
);
276 /* Check if the sample is exactly on an edge. If so, let cross be a
277 * positive or negative value depending on the direction of the edge.
285 if (cross0
< 0.0F
|| cross1
< 0.0F
|| cross2
< 0.0F
) {
286 /* point is outside triangle */
300 rgba_aa_tri(GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
304 #include "s_aatritemp.h"
309 index_aa_tri(GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
313 #include "s_aatritemp.h"
318 * Compute mipmap level of detail.
320 static INLINE GLfloat
321 compute_lambda(const GLfloat sPlane
[4], const GLfloat tPlane
[4],
322 GLfloat invQ
, GLfloat width
, GLfloat height
)
324 GLfloat dudx
= sPlane
[0] / sPlane
[2] * invQ
* width
;
325 GLfloat dudy
= sPlane
[1] / sPlane
[2] * invQ
* width
;
326 GLfloat dvdx
= tPlane
[0] / tPlane
[2] * invQ
* height
;
327 GLfloat dvdy
= tPlane
[1] / tPlane
[2] * invQ
* height
;
328 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
329 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
330 GLfloat rho2
= r1
+ r2
;
331 /* return log base 2 of rho */
332 return log(rho2
) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
337 tex_aa_tri(GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
342 #include "s_aatritemp.h"
347 spec_tex_aa_tri(GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
353 #include "s_aatritemp.h"
358 multitex_aa_tri(GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
363 #include "s_aatritemp.h"
367 spec_multitex_aa_tri(GLcontext
*ctx
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
373 #include "s_aatritemp.h"
378 * Examine GL state and set ctx->Driver.TriangleFunc to an
379 * appropriate antialiased triangle rasterizer function.
382 _mesa_set_aa_triangle_function(GLcontext
*ctx
)
384 ASSERT(ctx
->Polygon
.SmoothFlag
);
385 if (ctx
->Texture
.ReallyEnabled
) {
386 if (ctx
->Light
.Enabled
&&
387 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
) {
388 if (ctx
->Texture
.MultiTextureEnabled
) {
389 ctx
->Driver
.TriangleFunc
= spec_multitex_aa_tri
;
392 ctx
->Driver
.TriangleFunc
= spec_tex_aa_tri
;
396 if (ctx
->Texture
.MultiTextureEnabled
) {
397 ctx
->Driver
.TriangleFunc
= multitex_aa_tri
;
400 ctx
->Driver
.TriangleFunc
= tex_aa_tri
;
405 if (ctx
->Visual
.RGBAflag
) {
406 ctx
->Driver
.TriangleFunc
= rgba_aa_tri
;
409 ctx
->Driver
.TriangleFunc
= index_aa_tri
;
412 ASSERT(ctx
->Driver
.TriangleFunc
);