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

Dependents:   co657_lcdplay

Committer:
co657_frmb
Date:
Sun Nov 29 00:03:41 2015 +0000
Revision:
9:db4ec6f7d8b2
Parent:
8:55ee7af49f47
Font updates.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co657_frmb 0:215c9308dc52 1 /*
co657_frmb 0:215c9308dc52 2 * gfx3d.h -- basic 3D graphics
co657_frmb 0:215c9308dc52 3 * Copyright (C) 2015 Fred Barnes, University of Kent <frmb@kent.ac.uk>
co657_frmb 0:215c9308dc52 4 */
co657_frmb 0:215c9308dc52 5
co657_frmb 5:2aaaf4e78a53 6
co657_frmb 0:215c9308dc52 7 #ifndef __GFX3D_H
co657_frmb 0:215c9308dc52 8 #define __GFX3D_H
co657_frmb 0:215c9308dc52 9
co657_frmb 0:215c9308dc52 10 #include "C12832.h"
co657_frmb 0:215c9308dc52 11 #include "g3d_sintab.h"
co657_frmb 0:215c9308dc52 12
co657_frmb 0:215c9308dc52 13 /* NOTE: this assumes an ARM Cortex-M4 ish FPU, but might work elsewhere */
co657_frmb 0:215c9308dc52 14
co657_frmb 0:215c9308dc52 15 /* some basic types */
co657_frmb 0:215c9308dc52 16
co657_frmb 0:215c9308dc52 17 /** g3d_p3_t 3D single-precision point */
co657_frmb 0:215c9308dc52 18 typedef struct {
co657_frmb 0:215c9308dc52 19 float x, y, z;
co657_frmb 0:215c9308dc52 20 } g3d_p3_t;
co657_frmb 0:215c9308dc52 21
co657_frmb 0:215c9308dc52 22 /** g3d_2p3_t 2D integer point with original Z scaled */
co657_frmb 0:215c9308dc52 23 typedef struct {
co657_frmb 2:f62652cae975 24 int16_t x, y;
co657_frmb 3:2d8982c06eee 25 int16_t z;
co657_frmb 3:2d8982c06eee 26 int16_t dummy; /* fill out to 64 bits */
co657_frmb 3:2d8982c06eee 27 } __attribute__ ((packed)) g3d_2p3_t;
co657_frmb 3:2d8982c06eee 28
co657_frmb 3:2d8982c06eee 29 #define G3D_MAX_POLY_POINTS (3)
co657_frmb 3:2d8982c06eee 30
co657_frmb 3:2d8982c06eee 31 /** g3d_poly_t 2D integer triangular polygon, original Z scaled */
co657_frmb 3:2d8982c06eee 32 typedef struct {
co657_frmb 3:2d8982c06eee 33 g3d_2p3_t pts[G3D_MAX_POLY_POINTS];
co657_frmb 5:2aaaf4e78a53 34 uint16_t tx_pts[G3D_MAX_POLY_POINTS]; /* texture co-ords are 0xYYXX */
co657_frmb 3:2d8982c06eee 35 int32_t norm; /** face normal */
co657_frmb 8:55ee7af49f47 36 uint8_t *txptr; /** texture pointer (null if unset) */
co657_frmb 3:2d8982c06eee 37 } g3d_poly_t;
co657_frmb 3:2d8982c06eee 38
co657_frmb 3:2d8982c06eee 39 /** g3d_edgebuf_t polygon edge-buffer */
co657_frmb 3:2d8982c06eee 40 typedef struct {
co657_frmb 3:2d8982c06eee 41 uint8_t start[64];
co657_frmb 3:2d8982c06eee 42 uint8_t end[64];
co657_frmb 3:2d8982c06eee 43 uint16_t zstart[64];
co657_frmb 3:2d8982c06eee 44 uint16_t zend[64];
co657_frmb 3:2d8982c06eee 45 uint16_t xoff, s_end;
co657_frmb 3:2d8982c06eee 46 } g3d_edgebuf_t;
co657_frmb 0:215c9308dc52 47
co657_frmb 2:f62652cae975 48 /** g3d_polyscan_t polygon scan-line type */
co657_frmb 2:f62652cae975 49 typedef struct {
co657_frmb 2:f62652cae975 50 uint8_t scans[32][2]; /* pairs of X-start X-end for each line of the display */
co657_frmb 3:2d8982c06eee 51 int32_t zscan[32][2]; /* similar, but with Z-depth information */
co657_frmb 3:2d8982c06eee 52 int scan_s, scan_e; /* scan start and end indices */
co657_frmb 2:f62652cae975 53 int32_t norm; /* calculated normal for the polygon (back-face cull) */
co657_frmb 2:f62652cae975 54 } g3d_polyscan_t;
co657_frmb 2:f62652cae975 55
co657_frmb 0:215c9308dc52 56 /** angle_t Type for angle (0-255 in unsigned char) */
co657_frmb 0:215c9308dc52 57 typedef uint8_t angle_t; /* working in a coarsely grained world: circle is 256 degrees */
co657_frmb 0:215c9308dc52 58
co657_frmb 0:215c9308dc52 59 /** g3d_cubepnts A set of 8 points representing a cube */
co657_frmb 6:0bd002c936bb 60 static const g3d_p3_t g3d_cubepnts[] = {{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0}, {1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0},
co657_frmb 6:0bd002c936bb 61 {-1.0, -1.0, 1.0}, {1.0, -1.0, 1.0}, {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0}};
co657_frmb 0:215c9308dc52 62
co657_frmb 5:2aaaf4e78a53 63 /** g3d_xyfacepnts A set of 4 points representing a square in X-Y */
co657_frmb 5:2aaaf4e78a53 64 static const g3d_p3_t g3d_xyfacepnts[] = {{-1.0, -1.0, 0.0}, {1.0, -1.0, 0.0}, {1.0, 1.0, 0.0}, {-1.0, 1.0, 0.0}};
co657_frmb 5:2aaaf4e78a53 65
co657_frmb 0:215c9308dc52 66 /* some constants that describe the render target layout -- for the 128x32 LCD */
co657_frmb 0:215c9308dc52 67 #define G3D_X2_SHIFT (64)
co657_frmb 0:215c9308dc52 68 #define G3D_Y2_SHIFT (16)
co657_frmb 0:215c9308dc52 69
co657_frmb 3:2d8982c06eee 70 #define G3D_ZBADD (32.0f)
co657_frmb 4:7a9f0515d0a0 71 #define G3D_ZBSCALE (256.0f)
co657_frmb 0:215c9308dc52 72
co657_frmb 9:db4ec6f7d8b2 73 #define G3D_Z_DEPTH_MIN (6.0f)
co657_frmb 8:55ee7af49f47 74 #define G3D_Z_DEPTH_MAX (64.0f)
co657_frmb 8:55ee7af49f47 75 #define G3D_X_SCALE_MIN (32.0f)
co657_frmb 8:55ee7af49f47 76 #define G3D_X_SCALE_MAX (512.0f)
co657_frmb 8:55ee7af49f47 77 #define G3D_Y_SCALE_MIN (32.0f)
co657_frmb 8:55ee7af49f47 78 #define G3D_Y_SCALE_MAX (512.f)
co657_frmb 8:55ee7af49f47 79
co657_frmb 0:215c9308dc52 80 #define G3D_Z_DEPTH (12.0f)
co657_frmb 0:215c9308dc52 81 #define G3D_X_SCALE (120.0f)
co657_frmb 0:215c9308dc52 82 #define G3D_Y_SCALE (80.0f)
co657_frmb 0:215c9308dc52 83
co657_frmb 8:55ee7af49f47 84 extern "C" void gfx3d_set_z_depth (const float zd);
co657_frmb 0:215c9308dc52 85
co657_frmb 0:215c9308dc52 86 extern "C" void gfx3d_rotate_demo (const g3d_p3_t *src, g3d_p3_t *dst, const int npnts, const angle_t a);
co657_frmb 5:2aaaf4e78a53 87 extern "C" void gfx3d_rotate_x (const g3d_p3_t *src, g3d_p3_t *dst, const int npnts, const angle_t a);
co657_frmb 5:2aaaf4e78a53 88 extern "C" void gfx3d_rotate_y (const g3d_p3_t *src, g3d_p3_t *dst, const int npnts, const angle_t a);
co657_frmb 5:2aaaf4e78a53 89 extern "C" void gfx3d_rotate_z (const g3d_p3_t *src, g3d_p3_t *dst, const int npnts, const angle_t a);
co657_frmb 5:2aaaf4e78a53 90 extern "C" void gfx3d_scale (const g3d_p3_t *src, g3d_p3_t *dst, const int npnts, const g3d_p3_t scl);
co657_frmb 0:215c9308dc52 91 extern "C" void gfx3d_translate (const g3d_p3_t *src, g3d_p3_t *dst, const int npnts, const g3d_p3_t tx);
co657_frmb 0:215c9308dc52 92 extern "C" void gfx3d_project (const g3d_p3_t *src, g3d_2p3_t *dst, const int npnts);
co657_frmb 8:55ee7af49f47 93 extern "C" void gfx3d_cubify_points (const g3d_2p3_t *src, g3d_poly_t *dst, int *npoly, const int backfaces, const uint8_t **txptrs);
co657_frmb 5:2aaaf4e78a53 94 extern "C" void gfx3d_squarify_points (const g3d_2p3_t *src, g3d_poly_t *dst, int *npoly, const int backfaces);
co657_frmb 3:2d8982c06eee 95
co657_frmb 3:2d8982c06eee 96 extern "C" void gfx3d_clear_zb (void);
co657_frmb 3:2d8982c06eee 97 extern "C" void gfx3d_sort_poly (g3d_poly_t *p);
co657_frmb 3:2d8982c06eee 98
co657_frmb 3:2d8982c06eee 99 extern "C" void gfx3d_wirepoly (const g3d_poly_t *src, C12832 &lcd);
co657_frmb 3:2d8982c06eee 100 extern "C" void gfx3d_wirepoly_z (const g3d_poly_t *src, C12832 &lcd);
co657_frmb 3:2d8982c06eee 101
co657_frmb 3:2d8982c06eee 102 extern "C" void gfx3d_edgebuf_z (const g3d_poly_t *src, g3d_edgebuf_t *dst);
co657_frmb 3:2d8982c06eee 103 extern "C" void gfx3d_wireedge (const g3d_edgebuf_t *edge, C12832 &lcd);
co657_frmb 3:2d8982c06eee 104
co657_frmb 8:55ee7af49f47 105 extern "C" void gfx3d_polytxmap (const g3d_poly_t *src, C12832 &lcd);
co657_frmb 6:0bd002c936bb 106 extern "C" void gfx3d_polynormmap (const g3d_poly_t *src, C12832 &lcd);
co657_frmb 5:2aaaf4e78a53 107
co657_frmb 3:2d8982c06eee 108
co657_frmb 3:2d8982c06eee 109 #if 0
co657_frmb 3:2d8982c06eee 110 extern "C" void gfx3d_polyscan (const g3d_poly_t *src, g3d_polyscan_t *dst);
co657_frmb 3:2d8982c06eee 111 #endif
co657_frmb 0:215c9308dc52 112
co657_frmb 0:215c9308dc52 113 extern "C" void gfx3d_wirecube (const g3d_2p3_t *src, C12832 &lcd);
co657_frmb 0:215c9308dc52 114
co657_frmb 8:55ee7af49f47 115 /* some hacky font stuff */
co657_frmb 9:db4ec6f7d8b2 116 extern "C" int gfx3d_font04b_char_dpw (const char ch);
co657_frmb 9:db4ec6f7d8b2 117 extern "C" void gfx3d_font04b_tx_putchar (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char ch, const bool inv);
co657_frmb 9:db4ec6f7d8b2 118 extern "C" void gfx3d_font04b_tx_putstr (uint8_t *txbuf, const int txwidth, int *xptr, const int y, const char *str, const bool inv);
co657_frmb 9:db4ec6f7d8b2 119 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);
co657_frmb 5:2aaaf4e78a53 120
co657_frmb 5:2aaaf4e78a53 121 static inline uint8_t g3d_texture_bit (const uint8_t *tx, int x, int y)
co657_frmb 5:2aaaf4e78a53 122 {
co657_frmb 6:0bd002c936bb 123 return (tx[(x << 2) | (y >> 3)] & (0x01 << (y & 0x07)));
co657_frmb 5:2aaaf4e78a53 124 }
co657_frmb 5:2aaaf4e78a53 125
co657_frmb 5:2aaaf4e78a53 126
co657_frmb 0:215c9308dc52 127 #endif /* !__GFX3D_H */