source: trunk/src/opengl/mesa/aatriangle.c@ 3721

Last change on this file since 3721 was 3598, checked in by jeroen, 25 years ago

* empty log message *

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