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