gba-x3dtest

view src/polyfill.c @ 14:c398d834d64a

fixed the rendering bugs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 10:33:24 +0300
parents b0ed38f13261
children b755fb002f17
line source
1 #include "config.h"
2 #include <string.h>
3 #include <assert.h>
4 #include "polyfill.h"
5 #include "fixed.h"
6 #include "gbasys.h"
8 #define VNEXT(x, n) (((x) + 1) % (n))
9 #define VPREV(x, n) ((x) > 0 ? (x) - 1 : (n) - 1)
11 static void fill_scanline_pal(int y, int x0, int x1, uint8_t color);
12 static void fill_scanline_rgb(int y, int x0, int x1, uint16_t color);
13 static int winding(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
15 void draw_poly(int num, const pvec3 *verts, uint16_t color)
16 {
17 int i, topidx = 0, botidx = 0;
18 int lidx[2] = {-1, -1}, ridx[2] = {-1, -1};
19 int32_t y, topy, boty;
20 int32_t ldy = 0, rdy = 0, ldxdy, rdxdy;
21 int32_t lx, rx;
22 int start, end;
23 pvec3 v0, v1;
25 v0.x = verts[1].x - verts[0].x;
26 v0.y = verts[1].y - verts[0].y;
28 v1.x = verts[2].x - verts[0].x;
29 v1.y = verts[2].y - verts[0].y;
31 if(winding(v0.x, v0.y, v1.x, v1.y) < 0) {
32 return; /* backface */
33 }
35 topy = boty = verts[0].y;
36 for(i=1; i<num; i++) {
37 int32_t y = verts[i].y;
38 if(y < topy) {
39 topy = y;
40 topidx = i;
41 }
42 if(y > boty) {
43 boty = y;
44 botidx = i;
45 }
46 }
48 lidx[0] = ridx[0] = topidx;
50 /* find starting left edge */
51 lidx[1] = VPREV(lidx[0], num);
52 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
54 while(ldy == 0) {
55 lidx[0] = lidx[1];
56 lidx[1] = VPREV(lidx[1], num);
58 if(lidx[1] == topidx) {
59 return; /* degenerate */
60 }
62 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
63 }
64 lx = verts[lidx[0]].x;
65 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
67 /* find starting right edge */
68 ridx[1] = VNEXT(ridx[0], num);
69 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
71 while(rdy == 0) {
72 ridx[0] = ridx[1];
73 ridx[1] = VNEXT(ridx[1], num);
75 if(ridx[1] == topidx) {
76 return; /* degenerate */
77 }
79 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
80 }
81 rx = verts[ridx[0]].x;
82 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
84 start = topy >> 16;
85 end = boty >> 16;
87 if(end >= HEIGHT) end = HEIGHT - 1;
89 y = topy;
90 for(i=start; i<=end; i++) {
91 short x0, x1;
93 if(y >= verts[lidx[1]].y) {
94 lx = verts[lidx[1]].x;
95 lidx[0] = lidx[1];
96 lidx[1] = VPREV(lidx[1], num);
97 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
98 if(ldy < 0) {
99 break;
100 }
101 if(ldy) {
102 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
103 } else {
104 ldxdy = verts[lidx[1]].x - lx;
105 }
106 }
107 if(y >= verts[ridx[1]].y) {
108 rx = verts[ridx[1]].x;
109 ridx[0] = ridx[1];
110 ridx[1] = VNEXT(ridx[1], num);
111 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
112 if(rdy < 0) {
113 break;
114 }
115 if(rdy) {
116 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
117 } else {
118 rdxdy = verts[ridx[1]].x - rx;
119 }
120 }
122 x0 = lx >> 16;
123 x1 = rx >> 16;
125 if(i >= 0) {// && x0 < x1) {
126 #ifdef PALMODE
127 fill_scanline_pal(i, x0, x1, (uint8_t)color);
128 #else
129 fill_scanline_rgb(i, x0, x1, color);
130 #endif
131 }
133 lx += ldxdy;
134 rx += rdxdy;
135 y += 65536;
136 }
137 }
140 static void fill_scanline_pal(int y, int x0, int x1, uint8_t color)
141 {
142 #if 1
143 int i, num_pairs, num_pix = x1 - x0;
144 uint16_t *pixels = (uint16_t*)back_buffer->pixels + (y * WIDTH + x0) / 2;
145 uint16_t colpair = (uint16_t)color | ((uint16_t)color << 8);
147 if(x0 & 1) {
148 uint16_t pix = *pixels & 0xff;
149 *pixels++ = pix | ((uint16_t)color << 8);
150 --num_pix;
151 }
153 num_pairs = (num_pix & 0xfffe) / 2;
155 for(i=0; i<num_pairs; i++) {
156 *pixels++ = colpair;
157 }
159 if(num_pix & 1) {
160 uint16_t pix = *pixels & 0xff00;
161 *pixels = pix | color;
162 }
163 #else
164 int i;
165 uint8_t *pixels = (uint8_t*)back_buffer->pixels + y * WIDTH + x0;
167 for(i=x0; i<x1; i++) {
168 *pixels++ = color;
169 }
170 #endif
171 }
173 static void fill_scanline_rgb(int y, int x0, int x1, uint16_t color)
174 {
175 int i;
176 uint16_t *pixels;
178 if(x0 > x1) {
179 i = x0;
180 x0 = x1;
181 x1 = i;
182 }
184 if(x0 < 0) x0 = 0;
185 if(x1 >= WIDTH - 1) x1 = WIDTH - 1;
187 pixels = (uint16_t*)back_buffer->pixels + y * WIDTH + x0;
188 for(i=x0; i<x1; i++) {
189 *pixels++ = color;
190 }
191 }
194 void draw_point(const pvec3 *v, uint16_t color)
195 {
196 int x = v->x >> 16;
197 int y = v->y >> 16;
198 uint16_t *pixels = (uint16_t*)back_buffer->pixels;
200 if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
201 return;
202 }
204 #ifdef PALMODE
205 pixels += (y * WIDTH + x) / 2;
206 if(x & 1) {
207 *pixels = (*pixels & 0xff) | (color << 8);
208 } else {
209 *pixels = (*pixels & 0xff00) | color;
210 }
211 #else
212 pixels[y * WIDTH + x] = color;
213 #endif
214 }
216 static int winding(int32_t x0, int32_t y0, int32_t x1, int32_t y1)
217 {
218 return x16mul(x0, y1) - x16mul(y0, x1);
219 }