Basic 3D graphics for the MBED application-shield on-board LCD (initial/incomplete).
gfx3d.h@5:2aaaf4e78a53, 2015-11-25 (annotated)
- Committer:
- co657_frmb
- Date:
- Wed Nov 25 23:58:07 2015 +0000
- Revision:
- 5:2aaaf4e78a53
- Parent:
- 4:7a9f0515d0a0
- Child:
- 6:0bd002c936bb
Various updates, commit before optimising some stuff.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:215c9308dc52 | 6 | /** @file gfx3d.h */ |
co657_frmb | 0:215c9308dc52 | 7 | |
co657_frmb | 5:2aaaf4e78a53 | 8 | |
co657_frmb | 0:215c9308dc52 | 9 | #ifndef __GFX3D_H |
co657_frmb | 0:215c9308dc52 | 10 | #define __GFX3D_H |
co657_frmb | 0:215c9308dc52 | 11 | |
co657_frmb | 0:215c9308dc52 | 12 | #include "C12832.h" |
co657_frmb | 0:215c9308dc52 | 13 | #include "g3d_sintab.h" |
co657_frmb | 0:215c9308dc52 | 14 | |
co657_frmb | 0:215c9308dc52 | 15 | /* NOTE: this assumes an ARM Cortex-M4 ish FPU, but might work elsewhere */ |
co657_frmb | 0:215c9308dc52 | 16 | |
co657_frmb | 0:215c9308dc52 | 17 | /* some basic types */ |
co657_frmb | 0:215c9308dc52 | 18 | |
co657_frmb | 0:215c9308dc52 | 19 | /** g3d_p3_t 3D single-precision point */ |
co657_frmb | 0:215c9308dc52 | 20 | typedef struct { |
co657_frmb | 0:215c9308dc52 | 21 | float x, y, z; |
co657_frmb | 0:215c9308dc52 | 22 | } g3d_p3_t; |
co657_frmb | 0:215c9308dc52 | 23 | |
co657_frmb | 0:215c9308dc52 | 24 | /** g3d_2p3_t 2D integer point with original Z scaled */ |
co657_frmb | 0:215c9308dc52 | 25 | typedef struct { |
co657_frmb | 2:f62652cae975 | 26 | int16_t x, y; |
co657_frmb | 3:2d8982c06eee | 27 | int16_t z; |
co657_frmb | 3:2d8982c06eee | 28 | int16_t dummy; /* fill out to 64 bits */ |
co657_frmb | 3:2d8982c06eee | 29 | } __attribute__ ((packed)) g3d_2p3_t; |
co657_frmb | 3:2d8982c06eee | 30 | |
co657_frmb | 3:2d8982c06eee | 31 | #define G3D_MAX_POLY_POINTS (3) |
co657_frmb | 3:2d8982c06eee | 32 | |
co657_frmb | 3:2d8982c06eee | 33 | /** g3d_poly_t 2D integer triangular polygon, original Z scaled */ |
co657_frmb | 3:2d8982c06eee | 34 | typedef struct { |
co657_frmb | 3:2d8982c06eee | 35 | g3d_2p3_t pts[G3D_MAX_POLY_POINTS]; |
co657_frmb | 5:2aaaf4e78a53 | 36 | uint16_t tx_pts[G3D_MAX_POLY_POINTS]; /* texture co-ords are 0xYYXX */ |
co657_frmb | 3:2d8982c06eee | 37 | int32_t norm; /** face normal */ |
co657_frmb | 3:2d8982c06eee | 38 | } g3d_poly_t; |
co657_frmb | 3:2d8982c06eee | 39 | |
co657_frmb | 3:2d8982c06eee | 40 | /** g3d_edgebuf_t polygon edge-buffer */ |
co657_frmb | 3:2d8982c06eee | 41 | typedef struct { |
co657_frmb | 3:2d8982c06eee | 42 | uint8_t start[64]; |
co657_frmb | 3:2d8982c06eee | 43 | uint8_t end[64]; |
co657_frmb | 3:2d8982c06eee | 44 | uint16_t zstart[64]; |
co657_frmb | 3:2d8982c06eee | 45 | uint16_t zend[64]; |
co657_frmb | 3:2d8982c06eee | 46 | uint16_t xoff, s_end; |
co657_frmb | 3:2d8982c06eee | 47 | } g3d_edgebuf_t; |
co657_frmb | 0:215c9308dc52 | 48 | |
co657_frmb | 2:f62652cae975 | 49 | /** g3d_polyscan_t polygon scan-line type */ |
co657_frmb | 2:f62652cae975 | 50 | typedef struct { |
co657_frmb | 2:f62652cae975 | 51 | uint8_t scans[32][2]; /* pairs of X-start X-end for each line of the display */ |
co657_frmb | 3:2d8982c06eee | 52 | int32_t zscan[32][2]; /* similar, but with Z-depth information */ |
co657_frmb | 3:2d8982c06eee | 53 | int scan_s, scan_e; /* scan start and end indices */ |
co657_frmb | 2:f62652cae975 | 54 | int32_t norm; /* calculated normal for the polygon (back-face cull) */ |
co657_frmb | 2:f62652cae975 | 55 | } g3d_polyscan_t; |
co657_frmb | 2:f62652cae975 | 56 | |
co657_frmb | 0:215c9308dc52 | 57 | /** angle_t Type for angle (0-255 in unsigned char) */ |
co657_frmb | 0:215c9308dc52 | 58 | typedef uint8_t angle_t; /* working in a coarsely grained world: circle is 256 degrees */ |
co657_frmb | 0:215c9308dc52 | 59 | |
co657_frmb | 0:215c9308dc52 | 60 | /** g3d_cubepnts A set of 8 points representing a cube */ |
co657_frmb | 0:215c9308dc52 | 61 | 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 | 0:215c9308dc52 | 62 | {-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 | 63 | |
co657_frmb | 5:2aaaf4e78a53 | 64 | /** g3d_xyfacepnts A set of 4 points representing a square in X-Y */ |
co657_frmb | 5:2aaaf4e78a53 | 65 | 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 | 66 | |
co657_frmb | 0:215c9308dc52 | 67 | /* some constants that describe the render target layout -- for the 128x32 LCD */ |
co657_frmb | 0:215c9308dc52 | 68 | #define G3D_X2_SHIFT (64) |
co657_frmb | 0:215c9308dc52 | 69 | #define G3D_Y2_SHIFT (16) |
co657_frmb | 0:215c9308dc52 | 70 | |
co657_frmb | 3:2d8982c06eee | 71 | #define G3D_ZBADD (32.0f) |
co657_frmb | 4:7a9f0515d0a0 | 72 | #define G3D_ZBSCALE (256.0f) |
co657_frmb | 0:215c9308dc52 | 73 | |
co657_frmb | 0:215c9308dc52 | 74 | #define G3D_Z_DEPTH (12.0f) |
co657_frmb | 0:215c9308dc52 | 75 | #define G3D_X_SCALE (120.0f) |
co657_frmb | 0:215c9308dc52 | 76 | #define G3D_Y_SCALE (80.0f) |
co657_frmb | 0:215c9308dc52 | 77 | |
co657_frmb | 0:215c9308dc52 | 78 | |
co657_frmb | 0:215c9308dc52 | 79 | 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 | 80 | 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 | 81 | 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 | 82 | 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 | 83 | 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 | 84 | 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 | 85 | extern "C" void gfx3d_project (const g3d_p3_t *src, g3d_2p3_t *dst, const int npnts); |
co657_frmb | 3:2d8982c06eee | 86 | extern "C" void gfx3d_cubify_points (const g3d_2p3_t *src, g3d_poly_t *dst, int *npoly, const int backfaces); |
co657_frmb | 5:2aaaf4e78a53 | 87 | extern "C" void gfx3d_squarify_points (const g3d_2p3_t *src, g3d_poly_t *dst, int *npoly, const int backfaces); |
co657_frmb | 3:2d8982c06eee | 88 | |
co657_frmb | 3:2d8982c06eee | 89 | extern "C" void gfx3d_clear_zb (void); |
co657_frmb | 3:2d8982c06eee | 90 | extern "C" void gfx3d_sort_poly (g3d_poly_t *p); |
co657_frmb | 3:2d8982c06eee | 91 | |
co657_frmb | 3:2d8982c06eee | 92 | extern "C" void gfx3d_wirepoly (const g3d_poly_t *src, C12832 &lcd); |
co657_frmb | 3:2d8982c06eee | 93 | extern "C" void gfx3d_wirepoly_z (const g3d_poly_t *src, C12832 &lcd); |
co657_frmb | 3:2d8982c06eee | 94 | |
co657_frmb | 3:2d8982c06eee | 95 | extern "C" void gfx3d_edgebuf_z (const g3d_poly_t *src, g3d_edgebuf_t *dst); |
co657_frmb | 3:2d8982c06eee | 96 | extern "C" void gfx3d_wireedge (const g3d_edgebuf_t *edge, C12832 &lcd); |
co657_frmb | 3:2d8982c06eee | 97 | |
co657_frmb | 5:2aaaf4e78a53 | 98 | extern "C" void gfx3d_polytxmap (const g3d_poly_t *src, uint8_t *txbuf, C12832 &lcd); |
co657_frmb | 5:2aaaf4e78a53 | 99 | |
co657_frmb | 3:2d8982c06eee | 100 | |
co657_frmb | 3:2d8982c06eee | 101 | #if 0 |
co657_frmb | 3:2d8982c06eee | 102 | extern "C" void gfx3d_polyscan (const g3d_poly_t *src, g3d_polyscan_t *dst); |
co657_frmb | 3:2d8982c06eee | 103 | #endif |
co657_frmb | 0:215c9308dc52 | 104 | |
co657_frmb | 0:215c9308dc52 | 105 | extern "C" void gfx3d_wirecube (const g3d_2p3_t *src, C12832 &lcd); |
co657_frmb | 0:215c9308dc52 | 106 | |
co657_frmb | 5:2aaaf4e78a53 | 107 | |
co657_frmb | 5:2aaaf4e78a53 | 108 | static inline uint8_t g3d_texture_bit (const uint8_t *tx, int x, int y) |
co657_frmb | 5:2aaaf4e78a53 | 109 | { |
co657_frmb | 5:2aaaf4e78a53 | 110 | return (tx[(y << 2) | (x >> 3)] & (0x01 << (x & 0x07))); |
co657_frmb | 5:2aaaf4e78a53 | 111 | } |
co657_frmb | 5:2aaaf4e78a53 | 112 | |
co657_frmb | 5:2aaaf4e78a53 | 113 | |
co657_frmb | 0:215c9308dc52 | 114 | #endif /* !__GFX3D_H */ |