b64424e39a1d30bf1935d64db6fe362a431166ba
[mesa.git] / src / mesa / swrast / s_aatriangle.c
1 /* $Id: s_aatriangle.c,v 1.11 2001/03/28 21:37:24 brianp 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 */
43 static INLINE void
44 compute_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 */
70 static INLINE void
71 constant_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) \
80 do { \
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 */
92 static INLINE GLfloat
93 solve_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 */
107 static INLINE GLfloat
108 solve_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 */
119 static INLINE GLchan
120 solve_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 */
137 static GLfloat
138 compute_coveragef(const GLfloat v0[3], const GLfloat v1[3],
139 const GLfloat v2[3], GLint winx, GLint winy)
140 {
141 #define B 0.125
142 static const GLfloat samples[16][2] = {
143 /* start with the four corners */
144 { 0.00+B, 0.00+B },
145 { 0.75+B, 0.00+B },
146 { 0.00+B, 0.75+B },
147 { 0.75+B, 0.75+B },
148 /* continue with interior samples */
149 { 0.25+B, 0.00+B },
150 { 0.50+B, 0.00+B },
151 { 0.00+B, 0.25+B },
152 { 0.25+B, 0.25+B },
153 { 0.50+B, 0.25+B },
154 { 0.75+B, 0.25+B },
155 { 0.00+B, 0.50+B },
156 { 0.25+B, 0.50+B },
157 { 0.50+B, 0.50+B },
158 { 0.75+B, 0.50+B },
159 { 0.25+B, 0.75+B },
160 { 0.50+B, 0.75+B }
161 };
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];
170 GLint stop = 4, i;
171 GLfloat insideCount = 16.0F;
172
173 #ifdef DEBUG
174 {
175 const GLfloat area = dx0 * dy1 - dx1 * dy0;
176 assert(area >= 0.0);
177 }
178 #endif
179
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.
195 */
196 if (cross0 == 0.0F)
197 cross0 = dx0 + dy0;
198 if (cross1 == 0.0F)
199 cross1 = dx1 + dy1;
200 if (cross2 == 0.0F)
201 cross2 = dx2 + dy2;
202 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
203 /* point is outside triangle */
204 insideCount -= 1.0F;
205 stop = 16;
206 }
207 }
208 if (stop == 4)
209 return 1.0F;
210 else
211 return insideCount * (1.0F / 16.0F);
212 }
213
214
215
216 /*
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].
220 */
221 static GLint
222 compute_coveragei(const GLfloat v0[3], const GLfloat v1[3],
223 const GLfloat v2[3], GLint winx, GLint winy)
224 {
225 /* NOTE: 15 samples instead of 16.
226 * A better sample distribution could be used.
227 */
228 static const GLfloat samples[15][2] = {
229 /* start with the four corners */
230 { 0.00+B, 0.00+B },
231 { 0.75+B, 0.00+B },
232 { 0.00+B, 0.75+B },
233 { 0.75+B, 0.75+B },
234 /* continue with interior samples */
235 { 0.25+B, 0.00+B },
236 { 0.50+B, 0.00+B },
237 { 0.00+B, 0.25+B },
238 { 0.25+B, 0.25+B },
239 { 0.50+B, 0.25+B },
240 { 0.75+B, 0.25+B },
241 { 0.00+B, 0.50+B },
242 { 0.25+B, 0.50+B },
243 /*{ 0.50, 0.50 },*/
244 { 0.75+B, 0.50+B },
245 { 0.25+B, 0.75+B },
246 { 0.50+B, 0.75+B }
247 };
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];
256 GLint stop = 4, i;
257 GLint insideCount = 15;
258
259 #ifdef DEBUG
260 {
261 const GLfloat area = dx0 * dy1 - dx1 * dy0;
262 assert(area >= 0.0);
263 }
264 #endif
265
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.
281 */
282 if (cross0 == 0.0F)
283 cross0 = dx0 + dy0;
284 if (cross1 == 0.0F)
285 cross1 = dx1 + dy1;
286 if (cross2 == 0.0F)
287 cross2 = dx2 + dy2;
288 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
289 /* point is outside triangle */
290 insideCount--;
291 stop = 15;
292 }
293 }
294 if (stop == 4)
295 return 15;
296 else
297 return insideCount;
298 }
299
300
301
302 static void
303 rgba_aa_tri(GLcontext *ctx,
304 const SWvertex *v0,
305 const SWvertex *v1,
306 const SWvertex *v2)
307 {
308 #define DO_Z
309 #define DO_RGBA
310 #include "s_aatritemp.h"
311 }
312
313
314 static void
315 index_aa_tri(GLcontext *ctx,
316 const SWvertex *v0,
317 const SWvertex *v1,
318 const SWvertex *v2)
319 {
320 #define DO_Z
321 #define DO_INDEX
322 #include "s_aatritemp.h"
323 }
324
325
326 /*
327 * Compute mipmap level of detail.
328 */
329 static INLINE GLfloat
330 compute_lambda(const GLfloat sPlane[4], const GLfloat tPlane[4],
331 GLfloat invQ, GLfloat width, GLfloat height)
332 {
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) */
342 }
343
344
345 static void
346 tex_aa_tri(GLcontext *ctx,
347 const SWvertex *v0,
348 const SWvertex *v1,
349 const SWvertex *v2)
350 {
351 #define DO_Z
352 #define DO_RGBA
353 #define DO_TEX
354 #include "s_aatritemp.h"
355 }
356
357
358 static void
359 spec_tex_aa_tri(GLcontext *ctx,
360 const SWvertex *v0,
361 const SWvertex *v1,
362 const SWvertex *v2)
363 {
364 #define DO_Z
365 #define DO_RGBA
366 #define DO_TEX
367 #define DO_SPEC
368 #include "s_aatritemp.h"
369 }
370
371
372 static void
373 multitex_aa_tri(GLcontext *ctx,
374 const SWvertex *v0,
375 const SWvertex *v1,
376 const SWvertex *v2)
377 {
378 #define DO_Z
379 #define DO_RGBA
380 #define DO_MULTITEX
381 #include "s_aatritemp.h"
382 }
383
384 static void
385 spec_multitex_aa_tri(GLcontext *ctx,
386 const SWvertex *v0,
387 const SWvertex *v1,
388 const SWvertex *v2)
389 {
390 #define DO_Z
391 #define DO_RGBA
392 #define DO_MULTITEX
393 #define DO_SPEC
394 #include "s_aatritemp.h"
395 }
396
397
398 /*
399 * Examine GL state and set swrast->Triangle to an
400 * appropriate antialiased triangle rasterizer function.
401 */
402 void
403 _mesa_set_aa_triangle_function(GLcontext *ctx)
404 {
405 SWcontext *swrast = SWRAST_CONTEXT(ctx);
406 ASSERT(ctx->Polygon.SmoothFlag);
407
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;
412 }
413 else {
414 SWRAST_CONTEXT(ctx)->Triangle = spec_tex_aa_tri;
415 }
416 }
417 else {
418 if (swrast->_MultiTextureEnabled) {
419 SWRAST_CONTEXT(ctx)->Triangle = multitex_aa_tri;
420 }
421 else {
422 SWRAST_CONTEXT(ctx)->Triangle = tex_aa_tri;
423 }
424 }
425 }
426 else if (ctx->Visual.rgbMode) {
427 SWRAST_CONTEXT(ctx)->Triangle = rgba_aa_tri;
428 }
429 else {
430 SWRAST_CONTEXT(ctx)->Triangle = index_aa_tri;
431 }
432
433 ASSERT(SWRAST_CONTEXT(ctx)->Triangle);
434 }