gba-x3dtest

view src/polyfill.c @ 18:f907b2c50a8b

added fps bar
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Jun 2014 21:48:09 +0300
parents b755fb002f17
children 62390f9cc93e
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 /* TODO: constant interpolant optimization */
10 #define VNEXT(x, n) (((x) + 1) % (n))
11 #define VPREV(x, n) ((x) > 0 ? (x) - 1 : (n) - 1)
13 static void fill_scanline_pal(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
14 int32_t v0, int32_t v1, uint8_t color);
15 static void fill_scanline_rgb(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
16 int32_t v0, int32_t v1, uint16_t color, struct texture *tex);
17 static int winding(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
19 void draw_poly(int num, const pvec3 *verts, const pvec2 *texcoords, uint16_t color,
20 struct texture *tex)
21 {
22 int i, topidx = 0, botidx = 0;
23 int lidx[2] = {-1, -1}, ridx[2] = {-1, -1};
24 int32_t y, topy, boty;
25 int32_t ldy = 0, rdy = 0, ldxdy, rdxdy;
26 int32_t lx, rx;
27 int start, end;
28 pvec3 v0, v1;
30 int32_t lu, lv, ru, rv, ldudy, ldvdy, rdudy, rdvdy; /* texture interpolants */
32 v0.x = verts[1].x - verts[0].x;
33 v0.y = verts[1].y - verts[0].y;
35 v1.x = verts[2].x - verts[0].x;
36 v1.y = verts[2].y - verts[0].y;
38 if(winding(v0.x, v0.y, v1.x, v1.y) < 0) {
39 return; /* backface */
40 }
42 topy = boty = verts[0].y;
43 for(i=1; i<num; i++) {
44 int32_t y = verts[i].y;
45 if(y < topy) {
46 topy = y;
47 topidx = i;
48 }
49 if(y > boty) {
50 boty = y;
51 botidx = i;
52 }
53 }
55 lidx[0] = ridx[0] = topidx;
57 /* find starting left edge */
58 lidx[1] = VPREV(lidx[0], num);
59 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
61 while(ldy == 0) {
62 lidx[0] = lidx[1];
63 lidx[1] = VPREV(lidx[1], num);
65 if(lidx[1] == topidx) {
66 return; /* degenerate */
67 }
69 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
70 }
71 lx = verts[lidx[0]].x;
72 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
73 lu = texcoords[lidx[0]].x;
74 ldudy = x16div(texcoords[lidx[1]].x - lu, ldy);
75 lv = texcoords[lidx[0]].y;
76 ldvdy = x16div(texcoords[lidx[1]].y - lv, ldy);
78 /* find starting right edge */
79 ridx[1] = VNEXT(ridx[0], num);
80 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
82 while(rdy == 0) {
83 ridx[0] = ridx[1];
84 ridx[1] = VNEXT(ridx[1], num);
86 if(ridx[1] == topidx) {
87 return; /* degenerate */
88 }
90 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
91 }
92 rx = verts[ridx[0]].x;
93 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
94 ru = texcoords[ridx[0]].x;
95 rdudy = x16div(texcoords[ridx[1]].x - ru, rdy);
96 rv = texcoords[ridx[0]].y;
97 rdvdy = x16div(texcoords[ridx[1]].y - rv, rdy);
99 start = topy >> 16;
100 end = boty >> 16;
102 if(end >= HEIGHT) end = HEIGHT - 1;
104 y = topy;
105 for(i=start; i<=end; i++) {
107 if(y >= verts[lidx[1]].y) {
108 lx = verts[lidx[1]].x;
109 lidx[0] = lidx[1];
110 lidx[1] = VPREV(lidx[1], num);
111 ldy = verts[lidx[1]].y - verts[lidx[0]].y;
112 if(ldy < 0) {
113 break;
114 }
116 lu = texcoords[lidx[0]].x;
117 lv = texcoords[lidx[0]].y;
119 if(ldy) {
120 ldxdy = x16div(verts[lidx[1]].x - lx, ldy);
121 ldudy = x16div(texcoords[lidx[1]].x - lu, ldy);
122 ldvdy = x16div(texcoords[lidx[1]].y - lv, ldy);
123 } else {
124 ldxdy = verts[lidx[1]].x - lx;
125 ldudy = texcoords[lidx[1]].x - lu;
126 ldvdy = texcoords[lidx[1]].y - lv;
127 }
128 }
129 if(y >= verts[ridx[1]].y) {
130 rx = verts[ridx[1]].x;
131 ridx[0] = ridx[1];
132 ridx[1] = VNEXT(ridx[1], num);
133 rdy = verts[ridx[1]].y - verts[ridx[0]].y;
134 if(rdy < 0) {
135 break;
136 }
138 ru = texcoords[ridx[0]].x;
139 rv = texcoords[ridx[0]].y;
141 if(rdy) {
142 rdxdy = x16div(verts[ridx[1]].x - rx, rdy);
143 rdudy = x16div(texcoords[ridx[1]].x - ru, rdy);
144 rdvdy = x16div(texcoords[ridx[1]].y - rv, rdy);
145 } else {
146 rdxdy = verts[ridx[1]].x - rx;
147 rdudy = texcoords[ridx[1]].x - ru;
148 rdvdy = texcoords[ridx[1]].y - rv;
149 }
150 }
152 if(i >= 0) {
153 #ifdef PALMODE
154 fill_scanline_pal(i, lx, rx, lu, ru, lv, rv, (uint8_t)color);
155 #else
156 fill_scanline_rgb(i, lx, rx, lu, ru, lv, rv, color, tex);
157 #endif
158 }
160 lx += ldxdy;
161 rx += rdxdy;
162 y += 65536;
164 lu += ldudy;
165 lv += ldvdy;
166 ru += rdudy;
167 rv += rdvdy;
168 }
169 }
172 static void fill_scanline_pal(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
173 int32_t v0, int32_t v1, uint8_t color)
174 {
175 int ix0, ix1;
176 int32_t dx;
177 int32_t u, v, dudx, dvdx;
179 int i, num_pairs, num_pix;
180 uint16_t *pixels;
181 uint16_t colpair = (uint16_t)color | ((uint16_t)color << 8);
183 if(x0 > x1) {
184 int32_t tmp = x0;
185 x0 = x1;
186 x1 = tmp;
187 }
189 dx = x1 - x0;
191 u = u0;
192 v = v0;
193 if(dx) {
194 dudx = x16div(u1 - u0, dx);
195 dvdx = x16div(v1 - v0, dx);
196 } else {
197 dudx = u1 - u0;
198 dvdx = v1 - v0;
199 }
201 ix0 = (x0 + 32768) >> 16;
202 ix1 = (x1 + 32768) >> 16;
204 if(ix0 < 0) ix0 = 0;
205 if(ix1 >= WIDTH - 1) ix1 = WIDTH - 1;
207 num_pix = ix1 - ix0;
208 pixels = (uint16_t*)back_buffer->pixels + (y * WIDTH + ix0) / 2;
210 if(ix0 & 1) {
211 uint16_t pix = *pixels & 0xff;
212 *pixels++ = pix | ((uint16_t)color << 8);
213 --num_pix;
214 u += dudx;
215 v += dvdx;
216 }
218 num_pairs = (num_pix & 0xfffe) / 2;
220 for(i=0; i<num_pairs; i++) {
221 *pixels++ = colpair;
222 u += dudx * 2;
223 v += dvdx * 2;
224 }
226 if(num_pix & 1) {
227 uint16_t pix = *pixels & 0xff00;
228 *pixels = pix | color;
229 }
230 }
232 static void fill_scanline_rgb(int y, int32_t x0, int32_t x1, int32_t u0, int32_t u1,
233 int32_t v0, int32_t v1, uint16_t color, struct texture *tex)
234 {
235 int i, ix0, ix1;
236 uint16_t *pixels;
237 int32_t dx;
238 int32_t u, v, dudx, dvdx;
240 if(x0 > x1) {
241 int32_t tmp = x0;
242 x0 = x1;
243 x1 = tmp;
244 }
246 dx = x1 - x0;
248 u = u0;
249 v = v0;
250 if(dx) {
251 dudx = x16div(u1 - u0, dx);
252 dvdx = x16div(v1 - v0, dx);
253 } else {
254 dudx = u1 - u0;
255 dvdx = v1 - v0;
256 }
258 ix0 = (x0 + 32768) >> 16;
259 ix1 = (x1 + 32768) >> 16;
261 if(ix0 < 0) ix0 = 0;
262 if(ix1 >= WIDTH - 1) ix1 = WIDTH - 1;
264 pixels = (uint16_t*)back_buffer->pixels + y * WIDTH + ix0;
265 for(i=ix0; i<ix1; i++) {
266 /**pixels++ = color;*/
267 int cr = u >> 8;
268 int cg = v >> 8;
269 if(cr > 255) cr = 255;
270 if(cg > 255) cg = 255;
272 if(tex) {
273 int tx = (u >> (16 - tex->ushift)) & tex->umask;
274 int ty = (v >> (16 - tex->vshift)) & tex->vmask;
275 uint16_t texel = ((uint16_t*)tex->pixels)[ty * tex->xsz + tx];
276 *pixels++ = texel;
277 } else {
278 *pixels++ = color;
279 }
281 u += dudx;
282 v += dvdx;
283 }
284 }
287 void draw_point(const pvec3 *v, uint16_t color)
288 {
289 int x = v->x >> 16;
290 int y = v->y >> 16;
291 uint16_t *pixels = (uint16_t*)back_buffer->pixels;
293 if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
294 return;
295 }
297 #ifdef PALMODE
298 pixels += (y * WIDTH + x) / 2;
299 if(x & 1) {
300 *pixels = (*pixels & 0xff) | (color << 8);
301 } else {
302 *pixels = (*pixels & 0xff00) | color;
303 }
304 #else
305 pixels[y * WIDTH + x] = color;
306 #endif
307 }
309 static int winding(int32_t x0, int32_t y0, int32_t x1, int32_t y1)
310 {
311 return x16mul(x0, y1) - x16mul(y0, x1);
312 }