5 #if defined(__WATCOMC__) || defined(_WIN32) || defined(__DJGPP__)
13 #define FILL_POLY_BITS 0x03
15 /* mode bits: 00-wire 01-flat 10-gouraud 11-reserved
17 * bit 3-4: blend mode: 00-none 01-alpha 10-additive 11-reserved
20 void (*fillfunc[])(struct pvertex*, int) = {
31 polyfill_alpha_gouraud,
33 polyfill_alpha_tex_wire,
34 polyfill_alpha_tex_flat,
35 polyfill_alpha_tex_gouraud,
41 polyfill_add_tex_wire,
42 polyfill_add_tex_flat,
43 polyfill_add_tex_gouraud,
44 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 polyfill_gouraud_zbuf,
50 polyfill_tex_flat_zbuf,
51 polyfill_tex_gouraud_zbuf,
54 polyfill_alpha_flat_zbuf,
55 polyfill_alpha_gouraud_zbuf,
57 polyfill_alpha_tex_wire,
58 polyfill_alpha_tex_flat_zbuf,
59 polyfill_alpha_tex_gouraud_zbuf,
62 polyfill_add_flat_zbuf,
63 polyfill_add_gouraud_zbuf,
65 polyfill_add_tex_wire,
66 polyfill_add_tex_flat_zbuf,
67 polyfill_add_tex_gouraud_zbuf,
68 0, 0, 0, 0, 0, 0, 0, 0, 0
71 struct pimage pfill_fb, pfill_tex;
75 static struct pvertex *edgebuf, *left, *right;
76 static int edgebuf_size;
80 #define CHECKEDGE(x) \
83 assert(x < fbheight); \
89 void polyfill_fbheight(int height)
92 int newsz = (height * 2 + EDGEPAD * 3) * sizeof *edgebuf;
94 if(newsz > edgebuf_size) {
96 if(!(edgebuf = malloc(newsz))) {
97 fprintf(stderr, "failed to allocate edge table buffer (%d bytes)\n", newsz);
100 edgebuf_size = newsz;
102 left = edgebuf + EDGEPAD;
103 right = edgebuf + height + EDGEPAD * 2;
106 memset(edgebuf, 0xaa, EDGEPAD * sizeof *edgebuf);
107 memset(edgebuf + height + EDGEPAD, 0xaa, EDGEPAD * sizeof *edgebuf);
108 memset(edgebuf + height * 2 + EDGEPAD * 2, 0xaa, EDGEPAD * sizeof *edgebuf);
115 void polyfill(int mode, struct pvertex *verts, int nverts)
118 if(!fillfunc[mode]) {
119 fprintf(stderr, "polyfill mode %d not implemented\n", mode);
124 fillfunc[mode](verts, nverts);
127 void polyfill_wire(struct pvertex *verts, int nverts)
129 int i, x0, y0, x1, y1;
130 struct pvertex *v = verts;
131 unsigned short color = ((v->r << 8) & 0xf800) |
132 ((v->g << 3) & 0x7e0) | ((v->b >> 3) & 0x1f);
134 for(i=0; i<nverts - 1; i++) {
140 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pfill_fb.width, pfill_fb.height)) {
141 draw_line(x0, y0, x1, y1, color);
144 x0 = verts[0].x >> 8;
145 y0 = verts[0].y >> 8;
146 if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pfill_fb.width, pfill_fb.height)) {
147 draw_line(x1, y1, x0, y0, color);
151 void polyfill_tex_wire(struct pvertex *verts, int nverts)
153 polyfill_wire(verts, nverts); /* TODO */
156 void polyfill_alpha_wire(struct pvertex *verts, int nverts)
158 polyfill_wire(verts, nverts); /* TODO */
161 void polyfill_alpha_tex_wire(struct pvertex *verts, int nverts)
163 polyfill_wire(verts, nverts); /* TODO */
166 void polyfill_add_wire(struct pvertex *verts, int nverts)
168 polyfill_wire(verts, nverts); /* TODO */
171 void polyfill_add_tex_wire(struct pvertex *verts, int nverts)
173 polyfill_wire(verts, nverts); /* TODO */
176 #define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
177 #define PREVIDX(x) (((x) + 1) % nverts)
180 * When HIGH_QUALITY is defined, the rasterizer calculates slopes for attribute
181 * interpolation on each scanline separately; otherwise the slope for each
182 * attribute would be calculated once for the whole polygon, which is faster,
183 * but produces some slight quantization artifacts, due to the limited precision
184 * of fixed-point calculations.
188 /* extra bits of precision to use when interpolating colors.
189 * try tweaking this if you notice strange quantization artifacts.
191 #define COLOR_SHIFT 12
194 #define POLYFILL polyfill_flat
195 #define SCANEDGE scanedge_flat
201 #include "polytmpl.h"
205 #define POLYFILL polyfill_gouraud
206 #define SCANEDGE scanedge_gouraud
212 #include "polytmpl.h"
216 #define POLYFILL polyfill_tex_flat
217 #define SCANEDGE scanedge_tex_flat
223 #include "polytmpl.h"
227 #define POLYFILL polyfill_tex_gouraud
228 #define SCANEDGE scanedge_tex_gouraud
234 #include "polytmpl.h"
238 #define POLYFILL polyfill_alpha_flat
239 #define SCANEDGE scanedge_alpha_flat
245 #include "polytmpl.h"
249 #define POLYFILL polyfill_alpha_gouraud
250 #define SCANEDGE scanedge_alpha_gouraud
256 #include "polytmpl.h"
260 #define POLYFILL polyfill_alpha_tex_flat
261 #define SCANEDGE scanedge_alpha_tex_flat
267 #include "polytmpl.h"
271 #define POLYFILL polyfill_alpha_tex_gouraud
272 #define SCANEDGE scanedge_alpha_tex_gouraud
278 #include "polytmpl.h"
282 #define POLYFILL polyfill_add_flat
283 #define SCANEDGE scanedge_add_flat
289 #include "polytmpl.h"
293 #define POLYFILL polyfill_add_gouraud
294 #define SCANEDGE scanedge_add_gouraud
300 #include "polytmpl.h"
304 #define POLYFILL polyfill_add_tex_flat
305 #define SCANEDGE scanedge_add_tex_flat
311 #include "polytmpl.h"
315 #define POLYFILL polyfill_add_tex_gouraud
316 #define SCANEDGE scanedge_add_tex_gouraud
322 #include "polytmpl.h"
326 /* ---- zbuffer variants ----- */
328 #define POLYFILL polyfill_flat_zbuf
329 #define SCANEDGE scanedge_flat_zbuf
335 #include "polytmpl.h"
339 #define POLYFILL polyfill_gouraud_zbuf
340 #define SCANEDGE scanedge_gouraud_zbuf
346 #include "polytmpl.h"
350 #define POLYFILL polyfill_tex_flat_zbuf
351 #define SCANEDGE scanedge_tex_flat_zbuf
357 #include "polytmpl.h"
361 #define POLYFILL polyfill_tex_gouraud_zbuf
362 #define SCANEDGE scanedge_tex_gouraud_zbuf
368 #include "polytmpl.h"
372 #define POLYFILL polyfill_alpha_flat_zbuf
373 #define SCANEDGE scanedge_alpha_flat_zbuf
379 #include "polytmpl.h"
383 #define POLYFILL polyfill_alpha_gouraud_zbuf
384 #define SCANEDGE scanedge_alpha_gouraud_zbuf
390 #include "polytmpl.h"
394 #define POLYFILL polyfill_alpha_tex_flat_zbuf
395 #define SCANEDGE scanedge_alpha_tex_flat_zbuf
401 #include "polytmpl.h"
405 #define POLYFILL polyfill_alpha_tex_gouraud_zbuf
406 #define SCANEDGE scanedge_alpha_tex_gouraud_zbuf
412 #include "polytmpl.h"
416 #define POLYFILL polyfill_add_flat_zbuf
417 #define SCANEDGE scanedge_add_flat_zbuf
423 #include "polytmpl.h"
427 #define POLYFILL polyfill_add_gouraud_zbuf
428 #define SCANEDGE scanedge_add_gouraud_zbuf
434 #include "polytmpl.h"
438 #define POLYFILL polyfill_add_tex_flat_zbuf
439 #define SCANEDGE scanedge_add_tex_flat_zbuf
445 #include "polytmpl.h"
449 #define POLYFILL polyfill_add_tex_gouraud_zbuf
450 #define SCANEDGE scanedge_add_tex_gouraud_zbuf
456 #include "polytmpl.h"