unlit cube
[gba_blender] / src / xgl.c
index 376d792..515919e 100644 (file)
--- a/src/xgl.c
+++ b/src/xgl.c
@@ -157,23 +157,31 @@ static void xform(struct xvertex *out, const struct xvertex *in, const int32_t *
        out->z = XMUL(m[2], in->x) + XMUL(m[6], in->y) + XMUL(m[10], in->z) + m[14];
 }
 
+/* d = 1.0 / tan(fov/2) */
+#define PROJ_D 2.0f
+
 void xgl_draw(int prim, const struct xvertex *varr, int vcount)
 {
-       int i;
+       int i, cidx;
        struct xvertex xv[4];
        struct pvertex pv[4];
 
        while(vcount >= prim) {
+               cidx = varr->cidx;
                for(i=0; i<prim; i++) {
                        xform(xv + i, varr, mat[mtop]);
                        varr++;
 
+                       xv[i].x = xv[i].x / (xv[i].z >> 8);     /* assume aspect: ~2 */
+                       xv[i].y = (xv[i].y << 1) / (xv[i].z >> 8);
+                       /* projection result is 24.8 */
+
                        /* viewport */
-                       pv[i].x = ((((xv[i].x + 0x10000) >> 1) * vp[2]) >> 8) + (vp[0] << 8);
-                       pv[i].y = ((((xv[i].y + 0x10000) >> 1) * vp[3]) >> 8) + (vp[1] << 8);
+                       pv[i].x = (((xv[i].x + 0x100) >> 1) * vp[2]) + (vp[0] << 8);
+                       pv[i].y = (((0x100 - xv[i].y) >> 1) * vp[3]) + (vp[1] << 8);
                }
                vcount -= prim;
 
-               polyfill_flat(pv, prim, 0xff);
+               polyfill_flat(pv, prim, cidx);
        }
 }