347322cf65e5ba43cb11b36a99d98c09e0554503
[mesa.git] / src / mesa / swrast / s_aatriangle.c
1 /* $Id: s_aatriangle.c,v 1.1 2000/10/31 18:00:04 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 "s_aatriangle.h"
34 #include "s_span.h"
35
36
37 /*
38 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
39 * vertices and the given Z values.
40 */
41 static INLINE void
42 compute_plane(const GLfloat v0[], const GLfloat v1[], const GLfloat v2[],
43 GLfloat z0, GLfloat z1, GLfloat z2, GLfloat plane[4])
44 {
45 const GLfloat px = v1[0] - v0[0];
46 const GLfloat py = v1[1] - v0[1];
47 const GLfloat pz = z1 - z0;
48
49 const GLfloat qx = v2[0] - v0[0];
50 const GLfloat qy = v2[1] - v0[1];
51 const GLfloat qz = z2 - z0;
52
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);
57
58 plane[0] = a;
59 plane[1] = b;
60 plane[2] = c;
61 plane[3] = d;
62 }
63
64
65 /*
66 * Compute coefficients of a plane with a constant Z value.
67 */
68 static INLINE void
69 constant_plane(GLfloat value, GLfloat plane[4])
70 {
71 plane[0] = 0.0;
72 plane[1] = 0.0;
73 plane[2] = -1.0;
74 plane[3] = value;
75 }
76
77 #define CONSTANT_PLANE(VALUE, PLANE) \
78 do { \
79 PLANE[0] = 0.0F; \
80 PLANE[1] = 0.0F; \
81 PLANE[2] = -1.0F; \
82 PLANE[3] = VALUE; \
83 } while (0)
84
85
86
87 /*
88 * Solve plane equation for Z at (X,Y).
89 */
90 static INLINE GLfloat
91 solve_plane(GLfloat x, GLfloat y, const GLfloat plane[4])
92 {
93 GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2];
94 return z;
95 }
96
97
98 #define SOLVE_PLANE(X, Y, PLANE) \
99 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
100
101
102 /*
103 * Return 1 / solve_plane().
104 */
105 static INLINE GLfloat
106 solve_plane_recip(GLfloat x, GLfloat y, const GLfloat plane[4])
107 {
108 GLfloat z = -plane[2] / (plane[3] + plane[0] * x + plane[1] * y);
109 return z;
110 }
111
112
113
114 /*
115 * Solve plane and return clamped GLchan value.
116 */
117 static INLINE GLchan
118 solve_plane_chan(GLfloat x, GLfloat y, const GLfloat plane[4])
119 {
120 GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2] + 0.5F;
121 if (z < 0.0F)
122 return 0;
123 else if (z > CHAN_MAXF)
124 return CHAN_MAXF;
125 return (GLchan) (GLint) z;
126 }
127
128
129
130 /*
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].
134 */
135 static GLfloat
136 compute_coveragef(const GLfloat v0[3], const GLfloat v1[3],
137 const GLfloat v2[3], GLint winx, GLint winy)
138 {
139 static const GLfloat samples[16][2] = {
140 /* start with the four corners */
141 { 0.00, 0.00 },
142 { 0.75, 0.00 },
143 { 0.00, 0.75 },
144 { 0.75, 0.75 },
145 /* continue with interior samples */
146 { 0.25, 0.00 },
147 { 0.50, 0.00 },
148 { 0.00, 0.25 },
149 { 0.25, 0.25 },
150 { 0.50, 0.25 },
151 { 0.75, 0.25 },
152 { 0.00, 0.50 },
153 { 0.25, 0.50 },
154 { 0.50, 0.50 },
155 { 0.75, 0.50 },
156 { 0.25, 0.75 },
157 { 0.50, 0.75 }
158 };
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];
167 GLint stop = 4, i;
168 GLfloat insideCount = 16.0F;
169
170 #ifdef DEBUG
171 {
172 const GLfloat area = dx0 * dy1 - dx1 * dy0;
173 assert(area >= 0.0);
174 }
175 #endif
176
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.
192 */
193 if (cross0 == 0.0F)
194 cross0 = dx0 + dy0;
195 if (cross1 == 0.0F)
196 cross1 = dx1 + dy1;
197 if (cross2 == 0.0F)
198 cross2 = dx2 + dy2;
199 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
200 /* point is outside triangle */
201 insideCount -= 1.0F;
202 stop = 16;
203 }
204 }
205 if (stop == 4)
206 return 1.0F;
207 else
208 return insideCount * (1.0F / 16.0F);
209 }
210
211
212
213 /*
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].
217 */
218 static GLint
219 compute_coveragei(const GLfloat v0[3], const GLfloat v1[3],
220 const GLfloat v2[3], GLint winx, GLint winy)
221 {
222 /* NOTE: 15 samples instead of 16.
223 * A better sample distribution could be used.
224 */
225 static const GLfloat samples[15][2] = {
226 /* start with the four corners */
227 { 0.00, 0.00 },
228 { 0.75, 0.00 },
229 { 0.00, 0.75 },
230 { 0.75, 0.75 },
231 /* continue with interior samples */
232 { 0.25, 0.00 },
233 { 0.50, 0.00 },
234 { 0.00, 0.25 },
235 { 0.25, 0.25 },
236 { 0.50, 0.25 },
237 { 0.75, 0.25 },
238 { 0.00, 0.50 },
239 { 0.25, 0.50 },
240 /*{ 0.50, 0.50 },*/
241 { 0.75, 0.50 },
242 { 0.25, 0.75 },
243 { 0.50, 0.75 }
244 };
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];
253 GLint stop = 4, i;
254 GLint insideCount = 15;
255
256 #ifdef DEBUG
257 {
258 const GLfloat area = dx0 * dy1 - dx1 * dy0;
259 assert(area >= 0.0);
260 }
261 #endif
262
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.
278 */
279 if (cross0 == 0.0F)
280 cross0 = dx0 + dy0;
281 if (cross1 == 0.0F)
282 cross1 = dx1 + dy1;
283 if (cross2 == 0.0F)
284 cross2 = dx2 + dy2;
285 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
286 /* point is outside triangle */
287 insideCount--;
288 stop = 15;
289 }
290 }
291 if (stop == 4)
292 return 15;
293 else
294 return insideCount;
295 }
296
297
298
299 static void
300 rgba_aa_tri(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv)
301 {
302 #define DO_Z
303 #define DO_RGBA
304 #include "s_aatritemp.h"
305 }
306
307
308 static void
309 index_aa_tri(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv)
310 {
311 #define DO_Z
312 #define DO_INDEX
313 #include "s_aatritemp.h"
314 }
315
316
317 /*
318 * Compute mipmap level of detail.
319 */
320 static INLINE GLfloat
321 compute_lambda(const GLfloat sPlane[4], const GLfloat tPlane[4],
322 GLfloat invQ, GLfloat width, GLfloat height)
323 {
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) */
333 }
334
335
336 static void
337 tex_aa_tri(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv)
338 {
339 #define DO_Z
340 #define DO_RGBA
341 #define DO_TEX
342 #include "s_aatritemp.h"
343 }
344
345
346 static void
347 spec_tex_aa_tri(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv)
348 {
349 #define DO_Z
350 #define DO_RGBA
351 #define DO_TEX
352 #define DO_SPEC
353 #include "s_aatritemp.h"
354 }
355
356
357 static void
358 multitex_aa_tri(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv)
359 {
360 #define DO_Z
361 #define DO_RGBA
362 #define DO_MULTITEX
363 #include "s_aatritemp.h"
364 }
365
366 static void
367 spec_multitex_aa_tri(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv)
368 {
369 #define DO_Z
370 #define DO_RGBA
371 #define DO_MULTITEX
372 #define DO_SPEC
373 #include "s_aatritemp.h"
374 }
375
376
377 /*
378 * Examine GL state and set ctx->Driver.TriangleFunc to an
379 * appropriate antialiased triangle rasterizer function.
380 */
381 void
382 _mesa_set_aa_triangle_function(GLcontext *ctx)
383 {
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;
390 }
391 else {
392 ctx->Driver.TriangleFunc = spec_tex_aa_tri;
393 }
394 }
395 else {
396 if (ctx->Texture.MultiTextureEnabled) {
397 ctx->Driver.TriangleFunc = multitex_aa_tri;
398 }
399 else {
400 ctx->Driver.TriangleFunc = tex_aa_tri;
401 }
402 }
403 }
404 else {
405 if (ctx->Visual.RGBAflag) {
406 ctx->Driver.TriangleFunc = rgba_aa_tri;
407 }
408 else {
409 ctx->Driver.TriangleFunc = index_aa_tri;
410 }
411 }
412 ASSERT(ctx->Driver.TriangleFunc);
413 }