17117f1b4Smrg/* 27117f1b4Smrg * Mesa 3-D graphics library 37117f1b4Smrg * 47117f1b4Smrg * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 57117f1b4Smrg * 67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a 77117f1b4Smrg * copy of this software and associated documentation files (the "Software"), 87117f1b4Smrg * to deal in the Software without restriction, including without limitation 97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the 117117f1b4Smrg * Software is furnished to do so, subject to the following conditions: 127117f1b4Smrg * 137117f1b4Smrg * The above copyright notice and this permission notice shall be included 147117f1b4Smrg * in all copies or substantial portions of the Software. 157117f1b4Smrg * 167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE. 237117f1b4Smrg */ 247117f1b4Smrg 257117f1b4Smrg 267117f1b4Smrg/* 277117f1b4Smrg * Antialiased Triangle rasterizers 287117f1b4Smrg */ 297117f1b4Smrg 307117f1b4Smrg 31c1f859d4Smrg#include "main/glheader.h" 32c1f859d4Smrg#include "main/context.h" 33c1f859d4Smrg#include "main/macros.h" 343464ebd5Sriastradh#include "main/state.h" 357117f1b4Smrg#include "s_aatriangle.h" 367117f1b4Smrg#include "s_context.h" 377117f1b4Smrg#include "s_span.h" 387117f1b4Smrg 397117f1b4Smrg 407117f1b4Smrg/* 417117f1b4Smrg * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2 427117f1b4Smrg * vertices and the given Z values. 437117f1b4Smrg * A point (x,y,z) lies on plane iff a*x+b*y+c*z+d = 0. 447117f1b4Smrg */ 45af69d88dSmrgstatic inline void 467117f1b4Smrgcompute_plane(const GLfloat v0[], const GLfloat v1[], const GLfloat v2[], 477117f1b4Smrg GLfloat z0, GLfloat z1, GLfloat z2, GLfloat plane[4]) 487117f1b4Smrg{ 497117f1b4Smrg const GLfloat px = v1[0] - v0[0]; 507117f1b4Smrg const GLfloat py = v1[1] - v0[1]; 517117f1b4Smrg const GLfloat pz = z1 - z0; 527117f1b4Smrg 537117f1b4Smrg const GLfloat qx = v2[0] - v0[0]; 547117f1b4Smrg const GLfloat qy = v2[1] - v0[1]; 557117f1b4Smrg const GLfloat qz = z2 - z0; 567117f1b4Smrg 577117f1b4Smrg /* Crossproduct "(a,b,c):= dv1 x dv2" is orthogonal to plane. */ 587117f1b4Smrg const GLfloat a = py * qz - pz * qy; 597117f1b4Smrg const GLfloat b = pz * qx - px * qz; 607117f1b4Smrg const GLfloat c = px * qy - py * qx; 617117f1b4Smrg /* Point on the plane = "r*(a,b,c) + w", with fixed "r" depending 627117f1b4Smrg on the distance of plane from origin and arbitrary "w" parallel 637117f1b4Smrg to the plane. */ 647117f1b4Smrg /* The scalar product "(r*(a,b,c)+w)*(a,b,c)" is "r*(a^2+b^2+c^2)", 657117f1b4Smrg which is equal to "-d" below. */ 667117f1b4Smrg const GLfloat d = -(a * v0[0] + b * v0[1] + c * z0); 677117f1b4Smrg 687117f1b4Smrg plane[0] = a; 697117f1b4Smrg plane[1] = b; 707117f1b4Smrg plane[2] = c; 717117f1b4Smrg plane[3] = d; 727117f1b4Smrg} 737117f1b4Smrg 747117f1b4Smrg 757117f1b4Smrg/* 767117f1b4Smrg * Compute coefficients of a plane with a constant Z value. 777117f1b4Smrg */ 78af69d88dSmrgstatic inline void 797117f1b4Smrgconstant_plane(GLfloat value, GLfloat plane[4]) 807117f1b4Smrg{ 817117f1b4Smrg plane[0] = 0.0; 827117f1b4Smrg plane[1] = 0.0; 837117f1b4Smrg plane[2] = -1.0; 847117f1b4Smrg plane[3] = value; 857117f1b4Smrg} 867117f1b4Smrg 877117f1b4Smrg#define CONSTANT_PLANE(VALUE, PLANE) \ 887117f1b4Smrgdo { \ 897117f1b4Smrg PLANE[0] = 0.0F; \ 907117f1b4Smrg PLANE[1] = 0.0F; \ 917117f1b4Smrg PLANE[2] = -1.0F; \ 927117f1b4Smrg PLANE[3] = VALUE; \ 937117f1b4Smrg} while (0) 947117f1b4Smrg 957117f1b4Smrg 967117f1b4Smrg 977117f1b4Smrg/* 987117f1b4Smrg * Solve plane equation for Z at (X,Y). 997117f1b4Smrg */ 100af69d88dSmrgstatic inline GLfloat 1017117f1b4Smrgsolve_plane(GLfloat x, GLfloat y, const GLfloat plane[4]) 1027117f1b4Smrg{ 10301e04c3fSmrg assert(plane[2] != 0.0F); 1047117f1b4Smrg return (plane[3] + plane[0] * x + plane[1] * y) / -plane[2]; 1057117f1b4Smrg} 1067117f1b4Smrg 1077117f1b4Smrg 1087117f1b4Smrg#define SOLVE_PLANE(X, Y, PLANE) \ 1097117f1b4Smrg ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2]) 1107117f1b4Smrg 1117117f1b4Smrg 1127117f1b4Smrg/* 1137117f1b4Smrg * Solve plane and return clamped GLchan value. 1147117f1b4Smrg */ 115af69d88dSmrgstatic inline GLchan 1167117f1b4Smrgsolve_plane_chan(GLfloat x, GLfloat y, const GLfloat plane[4]) 1177117f1b4Smrg{ 1187117f1b4Smrg const GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2]; 1197117f1b4Smrg#if CHAN_TYPE == GL_FLOAT 1207117f1b4Smrg return CLAMP(z, 0.0F, CHAN_MAXF); 1217117f1b4Smrg#else 1227117f1b4Smrg if (z < 0) 1237117f1b4Smrg return 0; 1247117f1b4Smrg else if (z > CHAN_MAX) 1257117f1b4Smrg return CHAN_MAX; 1267ec681f3Smrg return (GLchan) lroundf(z); 1277117f1b4Smrg#endif 1287117f1b4Smrg} 1297117f1b4Smrg 1307117f1b4Smrg 131af69d88dSmrgstatic inline GLfloat 132c1f859d4Smrgplane_dx(const GLfloat plane[4]) 133c1f859d4Smrg{ 134c1f859d4Smrg return -plane[0] / plane[2]; 135c1f859d4Smrg} 136c1f859d4Smrg 137af69d88dSmrgstatic inline GLfloat 138c1f859d4Smrgplane_dy(const GLfloat plane[4]) 139c1f859d4Smrg{ 140c1f859d4Smrg return -plane[1] / plane[2]; 141c1f859d4Smrg} 142c1f859d4Smrg 143c1f859d4Smrg 1447117f1b4Smrg 1457117f1b4Smrg/* 1467117f1b4Smrg * Compute how much (area) of the given pixel is inside the triangle. 1477117f1b4Smrg * Vertices MUST be specified in counter-clockwise order. 1487117f1b4Smrg * Return: coverage in [0, 1]. 1497117f1b4Smrg */ 1507117f1b4Smrgstatic GLfloat 1517117f1b4Smrgcompute_coveragef(const GLfloat v0[3], const GLfloat v1[3], 1527117f1b4Smrg const GLfloat v2[3], GLint winx, GLint winy) 1537117f1b4Smrg{ 1547117f1b4Smrg /* Given a position [0,3]x[0,3] return the sub-pixel sample position. 1557117f1b4Smrg * Contributed by Ray Tice. 1567117f1b4Smrg * 1577117f1b4Smrg * Jitter sample positions - 1587117f1b4Smrg * - average should be .5 in x & y for each column 1597117f1b4Smrg * - each of the 16 rows and columns should be used once 1607117f1b4Smrg * - the rectangle formed by the first four points 1617117f1b4Smrg * should contain the other points 1627117f1b4Smrg * - the distrubition should be fairly even in any given direction 1637117f1b4Smrg * 1647117f1b4Smrg * The pattern drawn below isn't optimal, but it's better than a regular 1657117f1b4Smrg * grid. In the drawing, the center of each subpixel is surrounded by 1667117f1b4Smrg * four dots. The "x" marks the jittered position relative to the 1677117f1b4Smrg * subpixel center. 1687117f1b4Smrg */ 1697117f1b4Smrg#define POS(a, b) (0.5+a*4+b)/16 1707117f1b4Smrg static const GLfloat samples[16][2] = { 1717117f1b4Smrg /* start with the four corners */ 1727117f1b4Smrg { POS(0, 2), POS(0, 0) }, 1737117f1b4Smrg { POS(3, 3), POS(0, 2) }, 1747117f1b4Smrg { POS(0, 0), POS(3, 1) }, 1757117f1b4Smrg { POS(3, 1), POS(3, 3) }, 1767117f1b4Smrg /* continue with interior samples */ 1777117f1b4Smrg { POS(1, 1), POS(0, 1) }, 1787117f1b4Smrg { POS(2, 0), POS(0, 3) }, 1797117f1b4Smrg { POS(0, 3), POS(1, 3) }, 1807117f1b4Smrg { POS(1, 2), POS(1, 0) }, 1817117f1b4Smrg { POS(2, 3), POS(1, 2) }, 1827117f1b4Smrg { POS(3, 2), POS(1, 1) }, 1837117f1b4Smrg { POS(0, 1), POS(2, 2) }, 1847117f1b4Smrg { POS(1, 0), POS(2, 1) }, 1857117f1b4Smrg { POS(2, 1), POS(2, 3) }, 1867117f1b4Smrg { POS(3, 0), POS(2, 0) }, 1877117f1b4Smrg { POS(1, 3), POS(3, 0) }, 1887117f1b4Smrg { POS(2, 2), POS(3, 2) } 1897117f1b4Smrg }; 1907117f1b4Smrg 1917117f1b4Smrg const GLfloat x = (GLfloat) winx; 1927117f1b4Smrg const GLfloat y = (GLfloat) winy; 1937117f1b4Smrg const GLfloat dx0 = v1[0] - v0[0]; 1947117f1b4Smrg const GLfloat dy0 = v1[1] - v0[1]; 1957117f1b4Smrg const GLfloat dx1 = v2[0] - v1[0]; 1967117f1b4Smrg const GLfloat dy1 = v2[1] - v1[1]; 1977117f1b4Smrg const GLfloat dx2 = v0[0] - v2[0]; 1987117f1b4Smrg const GLfloat dy2 = v0[1] - v2[1]; 1997117f1b4Smrg GLint stop = 4, i; 2007117f1b4Smrg GLfloat insideCount = 16.0F; 2017117f1b4Smrg 20201e04c3fSmrg assert(dx0 * dy1 - dx1 * dy0 >= 0.0); /* area >= 0.0 */ 2037117f1b4Smrg 2047117f1b4Smrg for (i = 0; i < stop; i++) { 2057117f1b4Smrg const GLfloat sx = x + samples[i][0]; 2067117f1b4Smrg const GLfloat sy = y + samples[i][1]; 2077117f1b4Smrg /* cross product determines if sample is inside or outside each edge */ 2087117f1b4Smrg GLfloat cross = (dx0 * (sy - v0[1]) - dy0 * (sx - v0[0])); 2097117f1b4Smrg /* Check if the sample is exactly on an edge. If so, let cross be a 2107117f1b4Smrg * positive or negative value depending on the direction of the edge. 2117117f1b4Smrg */ 2127117f1b4Smrg if (cross == 0.0F) 2137117f1b4Smrg cross = dx0 + dy0; 2147117f1b4Smrg if (cross < 0.0F) { 2157117f1b4Smrg /* sample point is outside first edge */ 2167117f1b4Smrg insideCount -= 1.0F; 2177117f1b4Smrg stop = 16; 2187117f1b4Smrg } 2197117f1b4Smrg else { 2207117f1b4Smrg /* sample point is inside first edge */ 2217117f1b4Smrg cross = (dx1 * (sy - v1[1]) - dy1 * (sx - v1[0])); 2227117f1b4Smrg if (cross == 0.0F) 2237117f1b4Smrg cross = dx1 + dy1; 2247117f1b4Smrg if (cross < 0.0F) { 2257117f1b4Smrg /* sample point is outside second edge */ 2267117f1b4Smrg insideCount -= 1.0F; 2277117f1b4Smrg stop = 16; 2287117f1b4Smrg } 2297117f1b4Smrg else { 2307117f1b4Smrg /* sample point is inside first and second edges */ 2317117f1b4Smrg cross = (dx2 * (sy - v2[1]) - dy2 * (sx - v2[0])); 2327117f1b4Smrg if (cross == 0.0F) 2337117f1b4Smrg cross = dx2 + dy2; 2347117f1b4Smrg if (cross < 0.0F) { 2357117f1b4Smrg /* sample point is outside third edge */ 2367117f1b4Smrg insideCount -= 1.0F; 2377117f1b4Smrg stop = 16; 2387117f1b4Smrg } 2397117f1b4Smrg } 2407117f1b4Smrg } 2417117f1b4Smrg } 2427117f1b4Smrg if (stop == 4) 2437117f1b4Smrg return 1.0F; 2447117f1b4Smrg else 2457117f1b4Smrg return insideCount * (1.0F / 16.0F); 2467117f1b4Smrg} 2477117f1b4Smrg 2487117f1b4Smrg 2497117f1b4Smrg 2507117f1b4Smrgstatic void 2513464ebd5Sriastradhrgba_aa_tri(struct gl_context *ctx, 2527117f1b4Smrg const SWvertex *v0, 2537117f1b4Smrg const SWvertex *v1, 2547117f1b4Smrg const SWvertex *v2) 2557117f1b4Smrg{ 2567117f1b4Smrg#define DO_Z 2577117f1b4Smrg#include "s_aatritemp.h" 2587117f1b4Smrg} 2597117f1b4Smrg 2607117f1b4Smrg 2617117f1b4Smrgstatic void 2623464ebd5Sriastradhgeneral_aa_tri(struct gl_context *ctx, 263c1f859d4Smrg const SWvertex *v0, 264c1f859d4Smrg const SWvertex *v1, 265c1f859d4Smrg const SWvertex *v2) 2667117f1b4Smrg{ 2677117f1b4Smrg#define DO_Z 2687117f1b4Smrg#define DO_ATTRIBS 2697117f1b4Smrg#include "s_aatritemp.h" 2707117f1b4Smrg} 2717117f1b4Smrg 2727117f1b4Smrg 2737117f1b4Smrg 2747117f1b4Smrg/* 2757117f1b4Smrg * Examine GL state and set swrast->Triangle to an 2767117f1b4Smrg * appropriate antialiased triangle rasterizer function. 2777117f1b4Smrg */ 2787117f1b4Smrgvoid 2793464ebd5Sriastradh_swrast_set_aa_triangle_function(struct gl_context *ctx) 2807117f1b4Smrg{ 281c1f859d4Smrg SWcontext *swrast = SWRAST_CONTEXT(ctx); 282c1f859d4Smrg 28301e04c3fSmrg assert(ctx->Polygon.SmoothFlag); 2847117f1b4Smrg 2857117f1b4Smrg if (ctx->Texture._EnabledCoordUnits != 0 286af69d88dSmrg || _swrast_use_fragment_program(ctx) 287c1f859d4Smrg || swrast->_FogEnabled 2883464ebd5Sriastradh || _mesa_need_secondary_color(ctx)) { 289c1f859d4Smrg SWRAST_CONTEXT(ctx)->Triangle = general_aa_tri; 2907117f1b4Smrg } 2917117f1b4Smrg else { 292cdc920a0Smrg SWRAST_CONTEXT(ctx)->Triangle = rgba_aa_tri; 2937117f1b4Smrg } 2947117f1b4Smrg 29501e04c3fSmrg assert(SWRAST_CONTEXT(ctx)->Triangle); 2967117f1b4Smrg} 297