Basic 3D graphics for the MBED application-shield on-board LCD (initial/incomplete).

Dependents:   co657_lcdplay

Files at this revision

API Documentation at this revision

Comitter:
co657_frmb
Date:
Sun Nov 29 00:03:41 2015 +0000
Parent:
8:55ee7af49f47
Commit message:
Font updates.

Changed in this revision

g3d_font04b.cpp Show annotated file Show diff for this revision Revisions of this file
g3d_render.cpp Show annotated file Show diff for this revision Revisions of this file
g3d_textures.h Show annotated file Show diff for this revision Revisions of this file
gfx3d.h Show annotated file Show diff for this revision Revisions of this file
--- a/g3d_font04b.cpp	Sat Nov 28 01:40:16 2015 +0000
+++ b/g3d_font04b.cpp	Sun Nov 29 00:03:41 2015 +0000
@@ -17,6 +17,7 @@
         0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2e, 0x2d, 0x2d, 0x2d, 0x2a, 0x37, 0x2a, 0x38, 0x00,
     };
 
+/* 2x8-bit vertical slices, LSB=topmost (big-endian pairs) */
 static uint8_t font_data_04b[96*32] = {
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -213,12 +214,113 @@
     };
 
 
-void gfx3d_font04b_tx_putchar (uint8_t *txbuf, int *xptr, int y, char ch)
+int gfx3d_font04b_char_dpw (const char ch)
+{
+    int sel, cw, sw;
+
+    if ((ch < 32) || (ch > 127)) {
+        return 0;
+    }
+    sel = ch - 32;
+    cw = (font_metrics_04b[sel] & 0x0f);
+    if (!cw) {
+        cw = 16;
+    }
+    sw = (font_metrics_04b[sel] >> 4) & 0x0f;
+    if (!sw) {
+        sw = 16;
+    }
+    return cw + sw;
+}
+
+
+void gfx3d_font04b_tx_putchar (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char ch, const bool inv)
 {
+    int sel, cw, sw, i, dcs, dcw;
+    
+    if ((ch < 32) || (ch > 127)) {
+        return;
+    }
+    sel = ch - 32;
+    cw = (font_metrics_04b[sel] & 0x0f);
+    if (!cw) {
+        cw = 16;
+    }
+    sw = (font_metrics_04b[sel] >> 4) & 0x0f;
+    if (!sw) {
+        sw = 16;
+    }
+    
+    if (*xptr >= txwidth) {
+        *xptr += cw+sw;     /* advance space but do nothing else */
+        return;
+    } else if ((*xptr + cw) < 0) {
+        /* all character data off-left, advance space and nothing else */
+        *xptr += cw+sw;
+        return;
+    }
+    sel <<= 5;              /* in 32-byte steps */
+    
+    if ((*xptr + cw) > txwidth) {
+        /* means it won't all fit */
+        dcw = (txwidth - *xptr);
+    } else {
+        dcw = cw;
+    }
+    if (*xptr < 0) {
+        dcs = -*xptr;
+    } else {
+        dcs = 0;
+    }
+    
+    /* copy in character data */
+    for (i=dcs; i<dcw; i++) {
+        uint8_t chigh = font_data_04b[sel + (i << 1) + 0];
+        uint8_t clow = font_data_04b[sel + (i << 1) + 1];
+        
+        if (inv) {
+            chigh = ~chigh;
+            clow = ~clow;
+        }
+        
+        if (y == 0) {
+            txbuf[((*xptr + i) << 2) + 0] = clow;
+            txbuf[((*xptr + i) << 2) + 1] = chigh;
+        } else if (y == 8) {
+            txbuf[((*xptr + i) << 2) + 1] = clow;
+            txbuf[((*xptr + i) << 2) + 2] = chigh;
+        } else if (y == 16) {
+            txbuf[((*xptr + i) << 2) + 2] = clow;
+            txbuf[((*xptr + i) << 2) + 3] = chigh;
+        } else {
+            /* FIXME: all the other cases */
+        }
+    }
+    *xptr += cw + sw;
+
     return;
 }
 
-void gfx3d_font04b_tx_putstr (uint8_t *txbuf, int *xptr, int y, char *str)
+
+void gfx3d_font04b_tx_putstr (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char *str, const bool inv)
 {
+    const char *ch;
+    
+    for (ch=str; (*ch != '\0'); ch++) {
+        gfx3d_font04b_tx_putchar (txbuf, txwidth, xptr, y, *ch, inv);
+    }
     return;
 }
+
+
+void gfx3d_font04b_tx_putstrn (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char *str, const int slen, const bool inv)
+{
+    const char *ch;
+    int left = slen;
+    
+    for (ch=str; (*ch != '\0') && left; ch++, left--) {
+        gfx3d_font04b_tx_putchar (txbuf, txwidth, xptr, y, *ch, inv);
+    }
+    return;
+}
+
--- a/g3d_render.cpp	Sat Nov 28 01:40:16 2015 +0000
+++ b/g3d_render.cpp	Sun Nov 29 00:03:41 2015 +0000
@@ -667,6 +667,14 @@
         int16_t z1 = edge->zend[0];
         int32_t zval = (z0 << 16) | 0x8000;    /* half-way.. */
         
+        if (xp >= DISPLAY_WIDTH) {
+            /* off right-hand edge */
+            return;
+        } else if (xp < 0) {
+            /* off left-hand edge */
+            return;
+        }
+        
         if ((y0 == y1) || ((y0 + ys) == y1)) {
             /* one or two point, since we're narrow, just y0 */
             if (z0 <= ZBUFFER (xp, y0)) {
@@ -701,7 +709,11 @@
         int16_t z1 = edge->zend[x];
         
         int32_t zval = (z0 << 16) | 0x8000;     /* half-way.. */
-        
+
+        if ((xp >= DISPLAY_WIDTH) || (xp < 0)) {
+            continue;
+        }
+
         if (y0 == y1) {
             /* point, but we don't plot assuming it belongs to the adjoining polygon */
         } else if ((y0 + ys) == y1) {
--- a/g3d_textures.h	Sat Nov 28 01:40:16 2015 +0000
+++ b/g3d_textures.h	Sun Nov 29 00:03:41 2015 +0000
@@ -70,4 +70,18 @@
     };
 
 
+uint8_t g3d_texture_wcube[128] = {
+        0xf8, 0x0f, 0xf0, 0x1f, 0xfc, 0x0f, 0xf0, 0x3f, 0xfe, 0x0f, 0xf0, 0x7f,
+        0xff, 0xef, 0xf7, 0xff, 0xff, 0xef, 0xf7, 0xff, 0xff, 0xef, 0xf7, 0xff,
+        0xff, 0xef, 0xf7, 0xff, 0xff, 0x0f, 0xf0, 0xff, 0xff, 0x77, 0xee, 0xff,
+        0xff, 0x7b, 0xde, 0xff, 0xff, 0x7d, 0xbe, 0xff, 0xff, 0x7e, 0x7e, 0xff,
+        0x00, 0x3f, 0xfc, 0x00, 0x78, 0x1f, 0xf8, 0x1e, 0x78, 0x0f, 0xf0, 0x1e,
+        0x78, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x1e, 0x78, 0x0f, 0xf0, 0x1e,
+        0x78, 0x1f, 0xf8, 0x1e, 0x00, 0x3e, 0x7c, 0x00, 0xff, 0x7d, 0x3e, 0xff,
+        0xff, 0x7b, 0x9e, 0xff, 0xff, 0x77, 0xce, 0xff, 0xff, 0x6f, 0xe6, 0xff,
+        0xff, 0x0f, 0xf0, 0xff, 0xff, 0xef, 0xf7, 0xff, 0xff, 0xef, 0xf7, 0xff,
+        0xff, 0xef, 0xf7, 0xff, 0xfe, 0xef, 0xf7, 0x7f, 0xfc, 0x0f, 0xf0, 0x3f,
+        0xf8, 0x0f, 0xf0, 0x1f, 0xf0, 0x0f, 0xf0, 0x0f
+    };
+
 #endif  /* !__G3D_TEXTURES_H */
--- a/gfx3d.h	Sat Nov 28 01:40:16 2015 +0000
+++ b/gfx3d.h	Sun Nov 29 00:03:41 2015 +0000
@@ -70,7 +70,7 @@
 #define G3D_ZBADD       (32.0f)
 #define G3D_ZBSCALE     (256.0f)
 
-#define G3D_Z_DEPTH_MIN (10.0f)
+#define G3D_Z_DEPTH_MIN (6.0f)
 #define G3D_Z_DEPTH_MAX (64.0f)
 #define G3D_X_SCALE_MIN (32.0f)
 #define G3D_X_SCALE_MAX (512.0f)
@@ -113,8 +113,10 @@
 extern "C" void gfx3d_wirecube (const g3d_2p3_t *src, C12832 &lcd);
 
 /* some hacky font stuff */
-extern "C" void gfx3d_font04b_tx_putchar (uint8_t *txbuf, int *xptr, int y, char ch);
-extern "C" void gfx3d_font04b_tx_putstr (uint8_t *txbuf, int *xptr, int y, char *str);
+extern "C" int gfx3d_font04b_char_dpw (const char ch);
+extern "C" void gfx3d_font04b_tx_putchar (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char ch, const bool inv);
+extern "C" void gfx3d_font04b_tx_putstr (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char *str, const bool inv);
+extern "C" void gfx3d_font04b_tx_putstrn (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char *str, const int slen, const bool inv);
 
 static inline uint8_t g3d_texture_bit (const uint8_t *tx, int x, int y)
 {