s_aatriangle.c revision 22144ab7552f0799bcfca506bf4ffa7f70a06649
1/* $Id: s_aatriangle.c,v 1.9 2001/03/12 00:48:41 gareth Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version:  3.5
6 *
7 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28/*
29 * Antialiased Triangle rasterizers
30 */
31
32
33#include "mmath.h"
34#include "s_aatriangle.h"
35#include "s_context.h"
36#include "s_span.h"
37
38
39/*
40 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
41 * vertices and the given Z values.
42 */
43static INLINE void
44compute_plane(const GLfloat v0[], const GLfloat v1[], const GLfloat v2[],
45              GLfloat z0, GLfloat z1, GLfloat z2, GLfloat plane[4])
46{
47   const GLfloat px = v1[0] - v0[0];
48   const GLfloat py = v1[1] - v0[1];
49   const GLfloat pz = z1 - z0;
50
51   const GLfloat qx = v2[0] - v0[0];
52   const GLfloat qy = v2[1] - v0[1];
53   const GLfloat qz = z2 - z0;
54
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);
59
60   plane[0] = a;
61   plane[1] = b;
62   plane[2] = c;
63   plane[3] = d;
64}
65
66
67/*
68 * Compute coefficients of a plane with a constant Z value.
69 */
70static INLINE void
71constant_plane(GLfloat value, GLfloat plane[4])
72{
73   plane[0] = 0.0;
74   plane[1] = 0.0;
75   plane[2] = -1.0;
76   plane[3] = value;
77}
78
79#define CONSTANT_PLANE(VALUE, PLANE)	\
80do {					\
81   PLANE[0] = 0.0F;			\
82   PLANE[1] = 0.0F;			\
83   PLANE[2] = -1.0F;			\
84   PLANE[3] = VALUE;			\
85} while (0)
86
87
88
89/*
90 * Solve plane equation for Z at (X,Y).
91 */
92static INLINE GLfloat
93solve_plane(GLfloat x, GLfloat y, const GLfloat plane[4])
94{
95   GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2];
96   return z;
97}
98
99
100#define SOLVE_PLANE(X, Y, PLANE) \
101   ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
102
103
104/*
105 * Return 1 / solve_plane().
106 */
107static INLINE GLfloat
108solve_plane_recip(GLfloat x, GLfloat y, const GLfloat plane[4])
109{
110   GLfloat z = -plane[2] / (plane[3] + plane[0] * x + plane[1] * y);
111   return z;
112}
113
114
115
116/*
117 * Solve plane and return clamped GLchan value.
118 */
119static INLINE GLchan
120solve_plane_chan(GLfloat x, GLfloat y, const GLfloat plane[4])
121{
122   GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2] + 0.5F;
123   if (z < 0.0F)
124      return 0;
125   else if (z > CHAN_MAXF)
126      return (GLchan) CHAN_MAXF;
127   return (GLchan) (GLint) z;
128}
129
130
131
132/*
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].
136 */
137static GLfloat
138compute_coveragef(const GLfloat v0[3], const GLfloat v1[3],
139                  const GLfloat v2[3], GLint winx, GLint winy)
140{
141   static const GLfloat samples[16][2] = {
142      /* start with the four corners */
143      { 0.00, 0.00 },
144      { 0.75, 0.00 },
145      { 0.00, 0.75 },
146      { 0.75, 0.75 },
147      /* continue with interior samples */
148      { 0.25, 0.00 },
149      { 0.50, 0.00 },
150      { 0.00, 0.25 },
151      { 0.25, 0.25 },
152      { 0.50, 0.25 },
153      { 0.75, 0.25 },
154      { 0.00, 0.50 },
155      { 0.25, 0.50 },
156      { 0.50, 0.50 },
157      { 0.75, 0.50 },
158      { 0.25, 0.75 },
159      { 0.50, 0.75 }
160   };
161   const GLfloat x = (GLfloat) winx;
162   const GLfloat y = (GLfloat) winy;
163   const GLfloat dx0 = v1[0] - v0[0];
164   const GLfloat dy0 = v1[1] - v0[1];
165   const GLfloat dx1 = v2[0] - v1[0];
166   const GLfloat dy1 = v2[1] - v1[1];
167   const GLfloat dx2 = v0[0] - v2[0];
168   const GLfloat dy2 = v0[1] - v2[1];
169   GLint stop = 4, i;
170   GLfloat insideCount = 16.0F;
171
172#ifdef DEBUG
173   {
174      const GLfloat area = dx0 * dy1 - dx1 * dy0;
175      assert(area >= 0.0);
176   }
177#endif
178
179   for (i = 0; i < stop; i++) {
180      const GLfloat sx = x + samples[i][0];
181      const GLfloat sy = y + samples[i][1];
182      const GLfloat fx0 = sx - v0[0];
183      const GLfloat fy0 = sy - v0[1];
184      const GLfloat fx1 = sx - v1[0];
185      const GLfloat fy1 = sy - v1[1];
186      const GLfloat fx2 = sx - v2[0];
187      const GLfloat fy2 = sy - v2[1];
188      /* cross product determines if sample is inside or outside each edge */
189      GLfloat cross0 = (dx0 * fy0 - dy0 * fx0);
190      GLfloat cross1 = (dx1 * fy1 - dy1 * fx1);
191      GLfloat cross2 = (dx2 * fy2 - dy2 * fx2);
192      /* Check if the sample is exactly on an edge.  If so, let cross be a
193       * positive or negative value depending on the direction of the edge.
194       */
195      if (cross0 == 0.0F)
196         cross0 = dx0 + dy0;
197      if (cross1 == 0.0F)
198         cross1 = dx1 + dy1;
199      if (cross2 == 0.0F)
200         cross2 = dx2 + dy2;
201      if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
202         /* point is outside triangle */
203         insideCount -= 1.0F;
204         stop = 16;
205      }
206   }
207   if (stop == 4)
208      return 1.0F;
209   else
210      return insideCount * (1.0F / 16.0F);
211}
212
213
214
215/*
216 * Compute how much (area) of the given pixel is inside the triangle.
217 * Vertices MUST be specified in counter-clockwise order.
218 * Return:  coverage in [0, 15].
219 */
220static GLint
221compute_coveragei(const GLfloat v0[3], const GLfloat v1[3],
222                  const GLfloat v2[3], GLint winx, GLint winy)
223{
224   /* NOTE: 15 samples instead of 16.
225    * A better sample distribution could be used.
226    */
227   static const GLfloat samples[15][2] = {
228      /* start with the four corners */
229      { 0.00, 0.00 },
230      { 0.75, 0.00 },
231      { 0.00, 0.75 },
232      { 0.75, 0.75 },
233      /* continue with interior samples */
234      { 0.25, 0.00 },
235      { 0.50, 0.00 },
236      { 0.00, 0.25 },
237      { 0.25, 0.25 },
238      { 0.50, 0.25 },
239      { 0.75, 0.25 },
240      { 0.00, 0.50 },
241      { 0.25, 0.50 },
242      /*{ 0.50, 0.50 },*/
243      { 0.75, 0.50 },
244      { 0.25, 0.75 },
245      { 0.50, 0.75 }
246   };
247   const GLfloat x = (GLfloat) winx;
248   const GLfloat y = (GLfloat) winy;
249   const GLfloat dx0 = v1[0] - v0[0];
250   const GLfloat dy0 = v1[1] - v0[1];
251   const GLfloat dx1 = v2[0] - v1[0];
252   const GLfloat dy1 = v2[1] - v1[1];
253   const GLfloat dx2 = v0[0] - v2[0];
254   const GLfloat dy2 = v0[1] - v2[1];
255   GLint stop = 4, i;
256   GLint insideCount = 15;
257
258#ifdef DEBUG
259   {
260      const GLfloat area = dx0 * dy1 - dx1 * dy0;
261      assert(area >= 0.0);
262   }
263#endif
264
265   for (i = 0; i < stop; i++) {
266      const GLfloat sx = x + samples[i][0];
267      const GLfloat sy = y + samples[i][1];
268      const GLfloat fx0 = sx - v0[0];
269      const GLfloat fy0 = sy - v0[1];
270      const GLfloat fx1 = sx - v1[0];
271      const GLfloat fy1 = sy - v1[1];
272      const GLfloat fx2 = sx - v2[0];
273      const GLfloat fy2 = sy - v2[1];
274      /* cross product determines if sample is inside or outside each edge */
275      GLfloat cross0 = (dx0 * fy0 - dy0 * fx0);
276      GLfloat cross1 = (dx1 * fy1 - dy1 * fx1);
277      GLfloat cross2 = (dx2 * fy2 - dy2 * fx2);
278      /* Check if the sample is exactly on an edge.  If so, let cross be a
279       * positive or negative value depending on the direction of the edge.
280       */
281      if (cross0 == 0.0F)
282         cross0 = dx0 + dy0;
283      if (cross1 == 0.0F)
284         cross1 = dx1 + dy1;
285      if (cross2 == 0.0F)
286         cross2 = dx2 + dy2;
287      if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
288         /* point is outside triangle */
289         insideCount--;
290         stop = 15;
291      }
292   }
293   if (stop == 4)
294      return 15;
295   else
296      return insideCount;
297}
298
299
300
301static void
302rgba_aa_tri(GLcontext *ctx,
303	    const SWvertex *v0,
304	    const SWvertex *v1,
305	    const SWvertex *v2)
306{
307#define DO_Z
308#define DO_RGBA
309#include "s_aatritemp.h"
310}
311
312
313static void
314index_aa_tri(GLcontext *ctx,
315	     const SWvertex *v0,
316	     const SWvertex *v1,
317	     const SWvertex *v2)
318{
319#define DO_Z
320#define DO_INDEX
321#include "s_aatritemp.h"
322}
323
324
325/*
326 * Compute mipmap level of detail.
327 */
328static INLINE GLfloat
329compute_lambda(const GLfloat sPlane[4], const GLfloat tPlane[4],
330               GLfloat invQ, GLfloat width, GLfloat height)
331{
332   GLfloat dudx = sPlane[0] / sPlane[2] * invQ * width;
333   GLfloat dudy = sPlane[1] / sPlane[2] * invQ * width;
334   GLfloat dvdx = tPlane[0] / tPlane[2] * invQ * height;
335   GLfloat dvdy = tPlane[1] / tPlane[2] * invQ * height;
336   GLfloat r1 = dudx * dudx + dudy * dudy;
337   GLfloat r2 = dvdx * dvdx + dvdy * dvdy;
338   GLfloat rho2 = r1 + r2;
339   /* return log base 2 of rho */
340   return log(rho2) * 1.442695 * 0.5;       /* 1.442695 = 1/log(2) */
341}
342
343
344static void
345tex_aa_tri(GLcontext *ctx,
346	   const SWvertex *v0,
347	   const SWvertex *v1,
348	   const SWvertex *v2)
349{
350#define DO_Z
351#define DO_RGBA
352#define DO_TEX
353#include "s_aatritemp.h"
354}
355
356
357static void
358spec_tex_aa_tri(GLcontext *ctx,
359		const SWvertex *v0,
360		const SWvertex *v1,
361		const SWvertex *v2)
362{
363#define DO_Z
364#define DO_RGBA
365#define DO_TEX
366#define DO_SPEC
367#include "s_aatritemp.h"
368}
369
370
371static void
372multitex_aa_tri(GLcontext *ctx,
373		const SWvertex *v0,
374		const SWvertex *v1,
375		const SWvertex *v2)
376{
377#define DO_Z
378#define DO_RGBA
379#define DO_MULTITEX
380#include "s_aatritemp.h"
381}
382
383static void
384spec_multitex_aa_tri(GLcontext *ctx,
385		     const SWvertex *v0,
386		     const SWvertex *v1,
387		     const SWvertex *v2)
388{
389#define DO_Z
390#define DO_RGBA
391#define DO_MULTITEX
392#define DO_SPEC
393#include "s_aatritemp.h"
394}
395
396
397/*
398 * Examine GL state and set ctx->Driver.TriangleFunc to an
399 * appropriate antialiased triangle rasterizer function.
400 */
401void
402_mesa_set_aa_triangle_function(GLcontext *ctx)
403{
404   SWcontext *swrast = SWRAST_CONTEXT(ctx);
405   ASSERT(ctx->Polygon.SmoothFlag);
406
407   if (ctx->Texture._ReallyEnabled) {
408      if (ctx->_TriangleCaps & DD_SEPERATE_SPECULAR) {
409         if (swrast->_MultiTextureEnabled) {
410            SWRAST_CONTEXT(ctx)->Triangle = spec_multitex_aa_tri;
411         }
412         else {
413            SWRAST_CONTEXT(ctx)->Triangle = spec_tex_aa_tri;
414         }
415      }
416      else {
417         if (swrast->_MultiTextureEnabled) {
418            SWRAST_CONTEXT(ctx)->Triangle = multitex_aa_tri;
419         }
420         else {
421            SWRAST_CONTEXT(ctx)->Triangle = tex_aa_tri;
422         }
423      }
424   }
425   else if (ctx->Visual.rgbMode) {
426      SWRAST_CONTEXT(ctx)->Triangle = rgba_aa_tri;
427   }
428   else {
429      SWRAST_CONTEXT(ctx)->Triangle = index_aa_tri;
430   }
431
432   ASSERT(SWRAST_CONTEXT(ctx)->Triangle);
433}
434