6 static void filltop(struct g3d_vertex *v0, struct g3d_vertex *v1, struct g3d_vertex *v2);
7 static void fillbot(struct g3d_vertex *v0, struct g3d_vertex *v1, struct g3d_vertex *v2);
9 void g3d_polyfill(struct g3d_vertex *verts)
11 int i, topidx, botidx, mididx;
13 int32_t v01x, v01y, v02x, v02y;
14 struct g3d_vertex vtmp;
16 /* calculate winding */
17 v01x = verts[1].x - verts[0].x;
18 v01y = verts[1].y - verts[0].y;
19 v02x = verts[2].x - verts[0].x;
20 v02y = verts[2].y - verts[0].y;
21 if(!((v01x * v02y - v02x * v01y) & 0x80000000)) {
27 if(verts[i].y < verts[topidx].y) {
30 if(verts[i].y > verts[botidx].y) {
34 mididx = topidx + 1; if(mididx > 2) mididx = 0;
35 if(mididx == botidx) {
36 if(++mididx > 2) mididx = 0;
39 dy = verts[botidx].y - verts[topidx].y;
42 dx = verts[botidx].x - verts[topidx].x;
43 dym = verts[mididx].y - verts[topidx].y;
44 vtmp.x = muldiv(dx, dym, dy) + verts[topidx].x; /* dx * dym / dy + vtop.x */
45 vtmp.y = verts[mididx].y;
47 if(verts[topidx].y != verts[mididx].y) {
48 filltop(verts + topidx, verts + mididx, &vtmp);
50 if(verts[mididx].y != verts[botidx].y) {
51 fillbot(verts + mididx, &vtmp, verts + botidx);
55 static void filltop(struct g3d_vertex *v0, struct g3d_vertex *v1, struct g3d_vertex *v2)
57 struct g3d_vertex *vtmp;
58 int x, xn, line, lasty, len;
59 int32_t xl, xr, dxl, dxr, slopel, sloper, dy;
60 int32_t y0, y1, yoffs;
72 slopel = (dxl << 8) / dy;
73 sloper = (dxr << 8) / dy;
75 y0 = (v0->y + 0x100) & 0xffffff00; /* start from the next scanline */
76 yoffs = y0 - v0->y; /* offset of the next scanline */
77 xl = v0->x + ((yoffs * slopel) >> 8);
78 xr = v0->x + ((yoffs * sloper) >> 8);
82 if(lasty >= YRES) lasty = YRES - 1;
85 fbptr = g3d_fbpixels + line * XRES + x;
87 while(line <= lasty) {
89 len = ((xr + 0x100) >> 8) - (xl >> 8);
90 if(len > 0) memset(fbptr, g3d_curcidx, len);
96 fbptr += XRES + (xn - x);
102 static void fillbot(struct g3d_vertex *v0, struct g3d_vertex *v1, struct g3d_vertex *v2)
104 struct g3d_vertex *vtmp;
105 int x, xn, line, lasty, len;
106 int32_t xl, xr, dxl, dxr, slopel, sloper, dy;
107 int32_t y0, y1, yoffs;
108 unsigned char *fbptr;
119 slopel = (dxl << 8) / dy;
120 sloper = (dxr << 8) / dy;
122 y0 = (v0->y + 0x100) & 0xffffff00; /* start from the next scanline */
123 yoffs = y0 - v0->y; /* offset of the next scanline */
124 xl = v0->x + ((yoffs * slopel) >> 8);
125 xr = v1->x + ((yoffs * sloper) >> 8);
129 if(lasty >= YRES) lasty = YRES - 1;
132 fbptr = g3d_fbpixels + line * XRES + x;
134 while(line <= lasty) {
136 len = ((xr + 0x100) >> 8) - (xl >> 8);
137 if(len > 0) memset(fbptr, g3d_curcidx, len);
143 fbptr += XRES + (xn - x);