![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This demo shows a rotating 3D cube with and without textures.
CubeDemo.cpp@0:c828045bbe69, 2014-10-03 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Oct 03 12:48:37 2014 +0000
- Revision:
- 0:c828045bbe69
First version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:c828045bbe69 | 1 | /****************************************************************************** |
embeddedartists | 0:c828045bbe69 | 2 | * Includes |
embeddedartists | 0:c828045bbe69 | 3 | *****************************************************************************/ |
embeddedartists | 0:c828045bbe69 | 4 | |
embeddedartists | 0:c828045bbe69 | 5 | #include "mbed.h" |
embeddedartists | 0:c828045bbe69 | 6 | |
embeddedartists | 0:c828045bbe69 | 7 | #include "LcdController.h" |
embeddedartists | 0:c828045bbe69 | 8 | #include "EaLcdBoard.h" |
embeddedartists | 0:c828045bbe69 | 9 | #include "CubeDemo.h" |
embeddedartists | 0:c828045bbe69 | 10 | |
embeddedartists | 0:c828045bbe69 | 11 | #include "Image.h" |
embeddedartists | 0:c828045bbe69 | 12 | |
embeddedartists | 0:c828045bbe69 | 13 | //#include "wchar.h" |
embeddedartists | 0:c828045bbe69 | 14 | #include <math.h> |
embeddedartists | 0:c828045bbe69 | 15 | |
embeddedartists | 0:c828045bbe69 | 16 | |
embeddedartists | 0:c828045bbe69 | 17 | extern const unsigned char cube_image1[]; |
embeddedartists | 0:c828045bbe69 | 18 | extern int cube_image1_sz; |
embeddedartists | 0:c828045bbe69 | 19 | extern const unsigned char cube_image2[]; |
embeddedartists | 0:c828045bbe69 | 20 | extern int cube_image2_sz; |
embeddedartists | 0:c828045bbe69 | 21 | |
embeddedartists | 0:c828045bbe69 | 22 | |
embeddedartists | 0:c828045bbe69 | 23 | /****************************************************************************** |
embeddedartists | 0:c828045bbe69 | 24 | * Typedefs and defines |
embeddedartists | 0:c828045bbe69 | 25 | *****************************************************************************/ |
embeddedartists | 0:c828045bbe69 | 26 | |
embeddedartists | 0:c828045bbe69 | 27 | /****************************************************************************** |
embeddedartists | 0:c828045bbe69 | 28 | * Local variables |
embeddedartists | 0:c828045bbe69 | 29 | *****************************************************************************/ |
embeddedartists | 0:c828045bbe69 | 30 | |
embeddedartists | 0:c828045bbe69 | 31 | |
embeddedartists | 0:c828045bbe69 | 32 | /****************************************************************************** |
embeddedartists | 0:c828045bbe69 | 33 | * External variables |
embeddedartists | 0:c828045bbe69 | 34 | *****************************************************************************/ |
embeddedartists | 0:c828045bbe69 | 35 | |
embeddedartists | 0:c828045bbe69 | 36 | |
embeddedartists | 0:c828045bbe69 | 37 | /****************************************************************************** |
embeddedartists | 0:c828045bbe69 | 38 | * Local functions |
embeddedartists | 0:c828045bbe69 | 39 | *****************************************************************************/ |
embeddedartists | 0:c828045bbe69 | 40 | |
embeddedartists | 0:c828045bbe69 | 41 | |
embeddedartists | 0:c828045bbe69 | 42 | |
embeddedartists | 0:c828045bbe69 | 43 | |
embeddedartists | 0:c828045bbe69 | 44 | /* (c) 2008 by Denis Markovic |
embeddedartists | 0:c828045bbe69 | 45 | I give hereby permission to Anders Rosvall of Embedded Artists to use this function in his ARM7 board |
embeddedartists | 0:c828045bbe69 | 46 | demo program, as long as I'm not hold responsible for bugs in the code and I take not responsibility |
embeddedartists | 0:c828045bbe69 | 47 | for any damage used by that code :-) */ |
embeddedartists | 0:c828045bbe69 | 48 | |
embeddedartists | 0:c828045bbe69 | 49 | #define TRIANG_PROJ_SFT 16 |
embeddedartists | 0:c828045bbe69 | 50 | |
embeddedartists | 0:c828045bbe69 | 51 | static unsigned short DivTab[] = |
embeddedartists | 0:c828045bbe69 | 52 | {0xffff, 0x7fff, 0x5554, 0x3fff, 0x3332, 0x2aa9, 0x2491, |
embeddedartists | 0:c828045bbe69 | 53 | 0x1fff, 0x1c70, 0x1998, 0x1744, 0x1554, 0x13b0, 0x1248, 0x1110, |
embeddedartists | 0:c828045bbe69 | 54 | 0x0fff, 0x0f0e, 0x0e37, 0x0d78, 0x0ccb, 0x0c2f, 0x0ba1, 0x0b20, |
embeddedartists | 0:c828045bbe69 | 55 | 0x0aa9, 0x0a3c, 0x09d7, 0x097a, 0x0923, 0x08d2, 0x0887, 0x0841, |
embeddedartists | 0:c828045bbe69 | 56 | 0x07ff, 0x07c0, 0x0786, 0x074f, 0x071b, 0x06ea, 0x06bb, 0x068f, |
embeddedartists | 0:c828045bbe69 | 57 | 0x0665, 0x063d, 0x0617, 0x05f3, 0x05d0, 0x05af, 0x058f, 0x0571, |
embeddedartists | 0:c828045bbe69 | 58 | 0x0554, 0x0538, 0x051d, 0x0504, 0x04eb, 0x04d3, 0x04bc, 0x04a6, |
embeddedartists | 0:c828045bbe69 | 59 | 0x0491, 0x047c, 0x0468, 0x0455, 0x0443, 0x0431, 0x0420, 0x040f, |
embeddedartists | 0:c828045bbe69 | 60 | 0x03ff, 0x03ef, 0x03df, 0x03d1, 0x03c2, 0x03b4, 0x03a7, 0x039a, |
embeddedartists | 0:c828045bbe69 | 61 | 0x038d, 0x0380, 0x0374, 0x0368, 0x035d, 0x0352, 0x0347, 0x033c, |
embeddedartists | 0:c828045bbe69 | 62 | 0x0332, 0x0328, 0x031e, 0x0314, 0x030b, 0x0302, 0x02f9, 0x02f0, |
embeddedartists | 0:c828045bbe69 | 63 | 0x02e7, 0x02df, 0x02d7, 0x02cf, 0x02c7, 0x02bf, 0x02b8, 0x02b0, |
embeddedartists | 0:c828045bbe69 | 64 | 0x02a9, 0x02a2, 0x029b, 0x0294, 0x028e, 0x0287, 0x0281, 0x027b, |
embeddedartists | 0:c828045bbe69 | 65 | 0x0275, 0x026f, 0x0269, 0x0263, 0x025d, 0x0258, 0x0252, 0x024d, |
embeddedartists | 0:c828045bbe69 | 66 | 0x0248, 0x0242, 0x023d, 0x0238, 0x0233, 0x022f, 0x022a, 0x0225, |
embeddedartists | 0:c828045bbe69 | 67 | 0x0221, 0x021c, 0x0218, 0x0213, 0x020f, 0x020b, 0x0207, 0x0203, |
embeddedartists | 0:c828045bbe69 | 68 | 0x01ff, 0x01fb, 0x01f7, 0x01f3, 0x01ef, 0x01eb, 0x01e8, 0x01e4, |
embeddedartists | 0:c828045bbe69 | 69 | 0x01e0, 0x01dd, 0x01d9, 0x01d6, 0x01d3, 0x01cf, 0x01cc, 0x01c9, |
embeddedartists | 0:c828045bbe69 | 70 | 0x01c6, 0x01c2, 0x01bf, 0x01bc, 0x01b9, 0x01b6, 0x01b3, 0x01b1, |
embeddedartists | 0:c828045bbe69 | 71 | 0x01ae, 0x01ab, 0x01a8, 0x01a5, 0x01a3, 0x01a0, 0x019d, 0x019b, |
embeddedartists | 0:c828045bbe69 | 72 | 0x0198, 0x0196, 0x0193, 0x0191, 0x018e, 0x018c, 0x0189, 0x0187, |
embeddedartists | 0:c828045bbe69 | 73 | 0x0185, 0x0182, 0x0180, 0x017e, 0x017c, 0x0179, 0x0177, 0x0175, |
embeddedartists | 0:c828045bbe69 | 74 | 0x0173, 0x0171, 0x016f, 0x016d, 0x016b, 0x0169, 0x0167, 0x0165, |
embeddedartists | 0:c828045bbe69 | 75 | 0x0163, 0x0161, 0x015f, 0x015d, 0x015b, 0x0159, 0x0157, 0x0156, |
embeddedartists | 0:c828045bbe69 | 76 | 0x0154, 0x0152, 0x0150, 0x014f, 0x014d, 0x014b, 0x0149, 0x0148, |
embeddedartists | 0:c828045bbe69 | 77 | 0x0146, 0x0145, 0x0143, 0x0141, 0x0140, 0x013e, 0x013d, 0x013b, |
embeddedartists | 0:c828045bbe69 | 78 | 0x013a, 0x0138, 0x0137, 0x0135, 0x0134, 0x0132, 0x0131, 0x012f, |
embeddedartists | 0:c828045bbe69 | 79 | 0x012e, 0x012d, 0x012b, 0x012a, 0x0128, 0x0127, 0x0126, 0x0124, |
embeddedartists | 0:c828045bbe69 | 80 | 0x0123, 0x0122, 0x0120, 0x011f, 0x011e, 0x011d, 0x011b, 0x011a, |
embeddedartists | 0:c828045bbe69 | 81 | 0x0119, 0x0118, 0x0117, 0x0115, 0x0114, 0x0113, 0x0112, 0x0111, |
embeddedartists | 0:c828045bbe69 | 82 | 0x0110, 0x010e, 0x010d, 0x010c, 0x010b, 0x010a, 0x0109, 0x0108, |
embeddedartists | 0:c828045bbe69 | 83 | 0x0107, 0x0106, 0x0105, 0x0104, 0x0103, 0x0102, 0x0101, 0x0100, |
embeddedartists | 0:c828045bbe69 | 84 | 0x00ff, 0x00fe, 0x00fd, 0x00fc, 0x00fb, 0x00fa, 0x00f9, 0x00f8, |
embeddedartists | 0:c828045bbe69 | 85 | 0x00f7, 0x00f6, 0x00f5, 0x00f4, 0x00f3, 0x00f2, 0x00f1, 0x00f0, |
embeddedartists | 0:c828045bbe69 | 86 | 0x00ef, 0x00ef, 0x00ee, 0x00ed, 0x00ec, 0x00eb, 0x00ea, 0x00e9, |
embeddedartists | 0:c828045bbe69 | 87 | 0x00e9, 0x00e8, 0x00e7, 0x00e6, 0x00e5, 0x00e4, 0x00e4, 0x00e3, |
embeddedartists | 0:c828045bbe69 | 88 | 0x00e2, 0x00e1, 0x00e0, 0x00e0, 0x00df, 0x00de, 0x00dd, 0x00dd, |
embeddedartists | 0:c828045bbe69 | 89 | 0x00dc, 0x00db, 0x00da, 0x00da, 0x00d9, 0x00d8, 0x00d8, 0x00d7, |
embeddedartists | 0:c828045bbe69 | 90 | 0x00d6, 0x00d5, 0x00d5, 0x00d4, 0x00d3, 0x00d3, 0x00d2, 0x00d1, |
embeddedartists | 0:c828045bbe69 | 91 | 0x00d1, 0x00d0, 0x00cf, 0x00cf, 0x00ce, 0x00cd, 0x00cd, 0x00cc, |
embeddedartists | 0:c828045bbe69 | 92 | 0x00cb, 0x00cb, 0x00ca, 0x00c9, 0x00c9, 0x00c8, 0x00c8, 0x00c7, |
embeddedartists | 0:c828045bbe69 | 93 | 0x00c6, 0x00c6, 0x00c5, 0x00c4, 0x00c4, 0x00c3, 0x00c3, 0x00c2, |
embeddedartists | 0:c828045bbe69 | 94 | 0x00c2, 0x00c1, 0x00c0, 0x00c0, 0x00bf, 0x00bf, 0x00be, 0x00be, |
embeddedartists | 0:c828045bbe69 | 95 | 0x00bd, 0x00bc, 0x00bc, 0x00bb, 0x00bb, 0x00ba, 0x00ba, 0x00b9, |
embeddedartists | 0:c828045bbe69 | 96 | 0x00b9, 0x00b8, 0x00b8, 0x00b7, 0x00b7, 0x00b6, 0x00b6, 0x00b5, |
embeddedartists | 0:c828045bbe69 | 97 | 0x00b5, 0x00b4, 0x00b4, 0x00b3, 0x00b3, 0x00b2, 0x00b2, 0x00b1, |
embeddedartists | 0:c828045bbe69 | 98 | 0x00b1, 0x00b0, 0x00b0, 0x00af, 0x00af, 0x00ae, 0x00ae, 0x00ad, |
embeddedartists | 0:c828045bbe69 | 99 | 0x00ad, 0x00ac, 0x00ac, 0x00ab, 0x00ab, 0x00ab, 0x00aa, 0x00aa, |
embeddedartists | 0:c828045bbe69 | 100 | 0x00a9, 0x00a9, 0x00a8, 0x00a8, 0x00a7, 0x00a7, 0x00a7, 0x00a6, |
embeddedartists | 0:c828045bbe69 | 101 | 0x00a6, 0x00a5, 0x00a5, 0x00a4, 0x00a4, 0x00a4, 0x00a3, 0x00a3, |
embeddedartists | 0:c828045bbe69 | 102 | 0x00a2, 0x00a2, 0x00a2, 0x00a1, 0x00a1, 0x00a0, 0x00a0, 0x00a0, |
embeddedartists | 0:c828045bbe69 | 103 | 0x009f, 0x009f, 0x009e, 0x009e, 0x009e, 0x009d, 0x009d, 0x009c, |
embeddedartists | 0:c828045bbe69 | 104 | 0x009c, 0x009c, 0x009b, 0x009b, 0x009b, 0x009a, 0x009a, 0x0099, |
embeddedartists | 0:c828045bbe69 | 105 | 0x0099, 0x0099, 0x0098, 0x0098, 0x0098, 0x0097, 0x0097, 0x0097, |
embeddedartists | 0:c828045bbe69 | 106 | 0x0096, 0x0096, 0x0096, 0x0095, 0x0095, 0x0094, 0x0094, 0x0094, |
embeddedartists | 0:c828045bbe69 | 107 | 0x0093, 0x0093, 0x0093, 0x0092, 0x0092, 0x0092, 0x0091, 0x0091, |
embeddedartists | 0:c828045bbe69 | 108 | 0x0091, 0x0090, 0x0090, 0x0090, 0x008f, 0x008f, 0x008f, 0x008f, |
embeddedartists | 0:c828045bbe69 | 109 | 0x008e, 0x008e, 0x008e, 0x008d, 0x008d, 0x008d, 0x008c, 0x008c, |
embeddedartists | 0:c828045bbe69 | 110 | 0x008c, 0x008b, 0x008b, 0x008b, 0x008b, 0x008a, 0x008a, 0x008a, |
embeddedartists | 0:c828045bbe69 | 111 | 0x0089, 0x0089, 0x0089, 0x0088, 0x0088, 0x0088, 0x0088, 0x0087, |
embeddedartists | 0:c828045bbe69 | 112 | 0x0087, 0x0087, 0x0086, 0x0086, 0x0086, 0x0086, 0x0085, 0x0085, |
embeddedartists | 0:c828045bbe69 | 113 | 0x0085, 0x0085, 0x0084, 0x0084, 0x0084, 0x0083, 0x0083, 0x0083, |
embeddedartists | 0:c828045bbe69 | 114 | 0x0083, 0x0082, 0x0082, 0x0082, 0x0082, 0x0081, 0x0081, 0x0081, |
embeddedartists | 0:c828045bbe69 | 115 | 0x0081, 0x0080, 0x0080, 0x0080, 0x0080, 0x007f, 0x007f, 0x007f, |
embeddedartists | 0:c828045bbe69 | 116 | }; |
embeddedartists | 0:c828045bbe69 | 117 | |
embeddedartists | 0:c828045bbe69 | 118 | /* (c) 2008 by Denis Markovic |
embeddedartists | 0:c828045bbe69 | 119 | I give hereby permission to Anders Rosvall of Embedded Artists to use this function in his ARM7 board |
embeddedartists | 0:c828045bbe69 | 120 | demo program, as long as I'm not hold responsible for bugs in the code and I take not responsibility |
embeddedartists | 0:c828045bbe69 | 121 | for any damage used by that code :-) */ |
embeddedartists | 0:c828045bbe69 | 122 | void CubeDemo::CpPixel16Fast(int xSrc, int ySrc, int x, int y, Surface_t *SrcRP_p, Surface_t *DstRP_p) |
embeddedartists | 0:c828045bbe69 | 123 | { |
embeddedartists | 0:c828045bbe69 | 124 | unsigned short *src_p, *dst_p; |
embeddedartists | 0:c828045bbe69 | 125 | |
embeddedartists | 0:c828045bbe69 | 126 | src_p = (unsigned short *) SrcRP_p->pixels; |
embeddedartists | 0:c828045bbe69 | 127 | dst_p = (unsigned short *) DstRP_p->pixels; |
embeddedartists | 0:c828045bbe69 | 128 | |
embeddedartists | 0:c828045bbe69 | 129 | dst_p[y * DstRP_p->w + x] = src_p[ySrc * SrcRP_p->w + xSrc]; |
embeddedartists | 0:c828045bbe69 | 130 | |
embeddedartists | 0:c828045bbe69 | 131 | return; |
embeddedartists | 0:c828045bbe69 | 132 | } |
embeddedartists | 0:c828045bbe69 | 133 | |
embeddedartists | 0:c828045bbe69 | 134 | /* (c) 2008 by Denis Markovic |
embeddedartists | 0:c828045bbe69 | 135 | I give hereby permission to Anders Rosvall of Embedded Artists to use this function in his ARM7 board |
embeddedartists | 0:c828045bbe69 | 136 | demo program, as long as I'm not hold responsible for bugs in the code and I take not responsibility |
embeddedartists | 0:c828045bbe69 | 137 | for any damage used by that code :-) |
embeddedartists | 0:c828045bbe69 | 138 | |
embeddedartists | 0:c828045bbe69 | 139 | Input: SrcRP_p: pointer to type holding width and height and ptr to source pixel data |
embeddedartists | 0:c828045bbe69 | 140 | DstRP_p: pointer to type holding width and height and ptr to destination pixel data (i.e. gfx memory to draw to) |
embeddedartists | 0:c828045bbe69 | 141 | SrcCoords_p: pointer to array holding 3 source coordinates into triangle source accessed through SrcRP_p |
embeddedartists | 0:c828045bbe69 | 142 | DstCoords_p: pointer to array holding 3 destination triangle coordinates in draw buffer which is accessed through DstRP_p |
embeddedartists | 0:c828045bbe69 | 143 | |
embeddedartists | 0:c828045bbe69 | 144 | Description: take triangle from source rectangular picture (raw data, 16 bit) and project it into a triangle |
embeddedartists | 0:c828045bbe69 | 145 | in the destination picture (also 16 bit) |
embeddedartists | 0:c828045bbe69 | 146 | */ |
embeddedartists | 0:c828045bbe69 | 147 | void CubeDemo::TriangleProjectFast(Surface_t *SrcRP_p, Surface_t *DstRP_p, Coord2D_t *SrcCoords_p, Coord2D_t *DstCoords_p) |
embeddedartists | 0:c828045bbe69 | 148 | { |
embeddedartists | 0:c828045bbe69 | 149 | int TopCoord = 0, MiddleCoord = 1, BottomCoord = 2; |
embeddedartists | 0:c828045bbe69 | 150 | int /*SrcStartX, SrcStartY,*/ x, y, Direction, i, j; |
embeddedartists | 0:c828045bbe69 | 151 | long dxDstBig, dxDstSmall, dxSrcBig, dxSrcSmall, dySrcBig, dySrcSmall, DeltaY; |
embeddedartists | 0:c828045bbe69 | 152 | int DivMultVal; |
embeddedartists | 0:c828045bbe69 | 153 | |
embeddedartists | 0:c828045bbe69 | 154 | /* 1. step: sort dst triangle points */ |
embeddedartists | 0:c828045bbe69 | 155 | if(DstCoords_p[1].y < DstCoords_p[TopCoord].y) |
embeddedartists | 0:c828045bbe69 | 156 | { |
embeddedartists | 0:c828045bbe69 | 157 | TopCoord = 1; |
embeddedartists | 0:c828045bbe69 | 158 | MiddleCoord = 0; |
embeddedartists | 0:c828045bbe69 | 159 | } |
embeddedartists | 0:c828045bbe69 | 160 | |
embeddedartists | 0:c828045bbe69 | 161 | if(DstCoords_p[2].y < DstCoords_p[TopCoord].y) |
embeddedartists | 0:c828045bbe69 | 162 | { |
embeddedartists | 0:c828045bbe69 | 163 | TopCoord = 2; |
embeddedartists | 0:c828045bbe69 | 164 | BottomCoord = 1; |
embeddedartists | 0:c828045bbe69 | 165 | MiddleCoord = 0; |
embeddedartists | 0:c828045bbe69 | 166 | } |
embeddedartists | 0:c828045bbe69 | 167 | |
embeddedartists | 0:c828045bbe69 | 168 | if(DstCoords_p[BottomCoord].y < DstCoords_p[MiddleCoord].y) |
embeddedartists | 0:c828045bbe69 | 169 | { |
embeddedartists | 0:c828045bbe69 | 170 | int tmp; |
embeddedartists | 0:c828045bbe69 | 171 | tmp = MiddleCoord; |
embeddedartists | 0:c828045bbe69 | 172 | MiddleCoord = BottomCoord; |
embeddedartists | 0:c828045bbe69 | 173 | BottomCoord = tmp; |
embeddedartists | 0:c828045bbe69 | 174 | } |
embeddedartists | 0:c828045bbe69 | 175 | |
embeddedartists | 0:c828045bbe69 | 176 | /* so now we have the 3 sorted dst triangle points in TopCoord, MiddleCoord and BottomCoord |
embeddedartists | 0:c828045bbe69 | 177 | (can be done much more efficient with arithmetics instead of if or use pos/neg direction |
embeddedartists | 0:c828045bbe69 | 178 | vectors later?) */ |
embeddedartists | 0:c828045bbe69 | 179 | |
embeddedartists | 0:c828045bbe69 | 180 | /* 2. step: find start and end points for this line in src and dst triangle for each |
embeddedartists | 0:c828045bbe69 | 181 | line; start pt always on a line that originates from middle point; later replace |
embeddedartists | 0:c828045bbe69 | 182 | all div by mult with 1/DeltaY, i.e. only one division, maybe even save 1/... in |
embeddedartists | 0:c828045bbe69 | 183 | Tab as there are not so many possible DeltaY */ |
embeddedartists | 0:c828045bbe69 | 184 | DeltaY = (long) DstCoords_p[BottomCoord].y - DstCoords_p[TopCoord].y + 1; |
embeddedartists | 0:c828045bbe69 | 185 | |
embeddedartists | 0:c828045bbe69 | 186 | DivMultVal = ((int) DivTab[DeltaY-1] + 1); |
embeddedartists | 0:c828045bbe69 | 187 | |
embeddedartists | 0:c828045bbe69 | 188 | dxDstBig = ((long) DstCoords_p[BottomCoord].x - DstCoords_p[TopCoord].x + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 189 | dySrcBig = ((long) SrcCoords_p[BottomCoord].y - SrcCoords_p[TopCoord].y + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 190 | dxSrcBig = ((long) SrcCoords_p[BottomCoord].x - SrcCoords_p[TopCoord].x + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 191 | |
embeddedartists | 0:c828045bbe69 | 192 | DeltaY = (long) DstCoords_p[MiddleCoord].y - DstCoords_p[TopCoord].y + 1; |
embeddedartists | 0:c828045bbe69 | 193 | |
embeddedartists | 0:c828045bbe69 | 194 | DivMultVal = ((int) DivTab[DeltaY-1] + 1); |
embeddedartists | 0:c828045bbe69 | 195 | |
embeddedartists | 0:c828045bbe69 | 196 | dxDstSmall = ((long) DstCoords_p[MiddleCoord].x - DstCoords_p[TopCoord].x + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 197 | dySrcSmall = ((long) SrcCoords_p[MiddleCoord].y - SrcCoords_p[TopCoord].y + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 198 | dxSrcSmall = ((long) SrcCoords_p[MiddleCoord].x - SrcCoords_p[TopCoord].x + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 199 | |
embeddedartists | 0:c828045bbe69 | 200 | Direction = 1; |
embeddedartists | 0:c828045bbe69 | 201 | if(dxDstSmall > dxDstBig) |
embeddedartists | 0:c828045bbe69 | 202 | Direction = -1; |
embeddedartists | 0:c828045bbe69 | 203 | |
embeddedartists | 0:c828045bbe69 | 204 | for(y = DstCoords_p[TopCoord].y, i=0; y <= DstCoords_p[MiddleCoord].y; y++, i++) |
embeddedartists | 0:c828045bbe69 | 205 | { /* for each row/line */ |
embeddedartists | 0:c828045bbe69 | 206 | long MoveSrcX, MoveSrcY, P1x, P2x, P1y, P2y, SrcDeltaSteps; |
embeddedartists | 0:c828045bbe69 | 207 | |
embeddedartists | 0:c828045bbe69 | 208 | //SrcStartX = SrcCoords_p[TopCoord].x; |
embeddedartists | 0:c828045bbe69 | 209 | |
embeddedartists | 0:c828045bbe69 | 210 | P1x = SrcCoords_p[TopCoord].x + ((i * dxSrcSmall)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 211 | P1y = SrcCoords_p[TopCoord].y + ((i * dySrcSmall)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 212 | P2x = SrcCoords_p[TopCoord].x + ((i * dxSrcBig)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 213 | P2y = SrcCoords_p[TopCoord].y + ((i * dySrcBig)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 214 | |
embeddedartists | 0:c828045bbe69 | 215 | { |
embeddedartists | 0:c828045bbe69 | 216 | int xx; |
embeddedartists | 0:c828045bbe69 | 217 | |
embeddedartists | 0:c828045bbe69 | 218 | x = DstCoords_p[TopCoord].x + ((i * dxDstSmall)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 219 | xx = DstCoords_p[TopCoord].x + ((i * dxDstBig) >>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 220 | |
embeddedartists | 0:c828045bbe69 | 221 | SrcDeltaSteps = xx - x; |
embeddedartists | 0:c828045bbe69 | 222 | if(SrcDeltaSteps < 0) |
embeddedartists | 0:c828045bbe69 | 223 | SrcDeltaSteps = -SrcDeltaSteps; |
embeddedartists | 0:c828045bbe69 | 224 | |
embeddedartists | 0:c828045bbe69 | 225 | SrcDeltaSteps++; |
embeddedartists | 0:c828045bbe69 | 226 | |
embeddedartists | 0:c828045bbe69 | 227 | DivMultVal = ((int) DivTab[SrcDeltaSteps-1] + 1); |
embeddedartists | 0:c828045bbe69 | 228 | |
embeddedartists | 0:c828045bbe69 | 229 | MoveSrcX = (P2x - P1x) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 230 | MoveSrcY = (P2y - P1y) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 231 | |
embeddedartists | 0:c828045bbe69 | 232 | x-= Direction; |
embeddedartists | 0:c828045bbe69 | 233 | |
embeddedartists | 0:c828045bbe69 | 234 | P1x <<= TRIANG_PROJ_SFT; |
embeddedartists | 0:c828045bbe69 | 235 | P1y <<= TRIANG_PROJ_SFT; |
embeddedartists | 0:c828045bbe69 | 236 | |
embeddedartists | 0:c828045bbe69 | 237 | do |
embeddedartists | 0:c828045bbe69 | 238 | { |
embeddedartists | 0:c828045bbe69 | 239 | x+= Direction; |
embeddedartists | 0:c828045bbe69 | 240 | |
embeddedartists | 0:c828045bbe69 | 241 | CpPixel16Fast(P1x >>TRIANG_PROJ_SFT, P1y >>TRIANG_PROJ_SFT, x, y, SrcRP_p, DstRP_p); |
embeddedartists | 0:c828045bbe69 | 242 | |
embeddedartists | 0:c828045bbe69 | 243 | P1x += MoveSrcX; |
embeddedartists | 0:c828045bbe69 | 244 | P1y += MoveSrcY; |
embeddedartists | 0:c828045bbe69 | 245 | |
embeddedartists | 0:c828045bbe69 | 246 | } while(x!=xx); |
embeddedartists | 0:c828045bbe69 | 247 | } |
embeddedartists | 0:c828045bbe69 | 248 | } |
embeddedartists | 0:c828045bbe69 | 249 | |
embeddedartists | 0:c828045bbe69 | 250 | /* second part of triangle from middle to bottom */ |
embeddedartists | 0:c828045bbe69 | 251 | DeltaY = (long) DstCoords_p[BottomCoord].y - DstCoords_p[MiddleCoord].y+1; |
embeddedartists | 0:c828045bbe69 | 252 | |
embeddedartists | 0:c828045bbe69 | 253 | DivMultVal = ((int) DivTab[DeltaY-1] + 1); |
embeddedartists | 0:c828045bbe69 | 254 | |
embeddedartists | 0:c828045bbe69 | 255 | dxDstSmall = ((long) DstCoords_p[BottomCoord].x - DstCoords_p[MiddleCoord].x + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 256 | dySrcSmall = ((long) SrcCoords_p[BottomCoord].y - SrcCoords_p[MiddleCoord].y + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 257 | dxSrcSmall = ((long) SrcCoords_p[BottomCoord].x - SrcCoords_p[MiddleCoord].x + 1) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 258 | |
embeddedartists | 0:c828045bbe69 | 259 | y--; |
embeddedartists | 0:c828045bbe69 | 260 | i--; |
embeddedartists | 0:c828045bbe69 | 261 | |
embeddedartists | 0:c828045bbe69 | 262 | for(j=0; y <= DstCoords_p[BottomCoord].y; y++, i++, j++) |
embeddedartists | 0:c828045bbe69 | 263 | { /* for each row/line */ |
embeddedartists | 0:c828045bbe69 | 264 | long MoveSrcX, MoveSrcY, P1x, P2x, P1y, P2y, SrcDeltaSteps; |
embeddedartists | 0:c828045bbe69 | 265 | |
embeddedartists | 0:c828045bbe69 | 266 | //SrcStartX = SrcCoords_p[MiddleCoord].x; |
embeddedartists | 0:c828045bbe69 | 267 | |
embeddedartists | 0:c828045bbe69 | 268 | P1x = SrcCoords_p[MiddleCoord].x + ((j * dxSrcSmall)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 269 | P1y = SrcCoords_p[MiddleCoord].y + ((j * dySrcSmall)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 270 | P2x = SrcCoords_p[TopCoord].x + ((i * dxSrcBig)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 271 | P2y = SrcCoords_p[TopCoord].y + ((i * dySrcBig)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 272 | |
embeddedartists | 0:c828045bbe69 | 273 | { |
embeddedartists | 0:c828045bbe69 | 274 | int xx; |
embeddedartists | 0:c828045bbe69 | 275 | |
embeddedartists | 0:c828045bbe69 | 276 | x = DstCoords_p[MiddleCoord].x + ((j * dxDstSmall)>>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 277 | xx = DstCoords_p[TopCoord].x + ((i * dxDstBig) >>TRIANG_PROJ_SFT); |
embeddedartists | 0:c828045bbe69 | 278 | |
embeddedartists | 0:c828045bbe69 | 279 | SrcDeltaSteps = xx - x; |
embeddedartists | 0:c828045bbe69 | 280 | |
embeddedartists | 0:c828045bbe69 | 281 | /* todo, fixme: direction should not have to be set here but if we do not |
embeddedartists | 0:c828045bbe69 | 282 | do it here, sometimes calc fails, investigate (problem case: dst coords |
embeddedartists | 0:c828045bbe69 | 283 | 250/299, 321/302, 472/308 and src coord 0/0, bike w-1, 0, bike w-1/bike h-1 */ |
embeddedartists | 0:c828045bbe69 | 284 | if(SrcDeltaSteps < 0) |
embeddedartists | 0:c828045bbe69 | 285 | { |
embeddedartists | 0:c828045bbe69 | 286 | SrcDeltaSteps = -SrcDeltaSteps; |
embeddedartists | 0:c828045bbe69 | 287 | Direction = -1; |
embeddedartists | 0:c828045bbe69 | 288 | } |
embeddedartists | 0:c828045bbe69 | 289 | else |
embeddedartists | 0:c828045bbe69 | 290 | Direction = 1; |
embeddedartists | 0:c828045bbe69 | 291 | |
embeddedartists | 0:c828045bbe69 | 292 | SrcDeltaSteps++; |
embeddedartists | 0:c828045bbe69 | 293 | |
embeddedartists | 0:c828045bbe69 | 294 | DivMultVal = ((int) DivTab[SrcDeltaSteps-1] + 1); |
embeddedartists | 0:c828045bbe69 | 295 | |
embeddedartists | 0:c828045bbe69 | 296 | MoveSrcX = (P2x - P1x) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 297 | MoveSrcY = (P2y - P1y) * DivMultVal; |
embeddedartists | 0:c828045bbe69 | 298 | |
embeddedartists | 0:c828045bbe69 | 299 | x-= Direction; |
embeddedartists | 0:c828045bbe69 | 300 | |
embeddedartists | 0:c828045bbe69 | 301 | P1x <<= TRIANG_PROJ_SFT; |
embeddedartists | 0:c828045bbe69 | 302 | P1y <<= TRIANG_PROJ_SFT; |
embeddedartists | 0:c828045bbe69 | 303 | |
embeddedartists | 0:c828045bbe69 | 304 | do |
embeddedartists | 0:c828045bbe69 | 305 | { |
embeddedartists | 0:c828045bbe69 | 306 | x+= Direction; |
embeddedartists | 0:c828045bbe69 | 307 | |
embeddedartists | 0:c828045bbe69 | 308 | CpPixel16Fast(P1x >>TRIANG_PROJ_SFT, P1y >>TRIANG_PROJ_SFT, x, y, SrcRP_p, DstRP_p); |
embeddedartists | 0:c828045bbe69 | 309 | |
embeddedartists | 0:c828045bbe69 | 310 | P1x += MoveSrcX; |
embeddedartists | 0:c828045bbe69 | 311 | P1y += MoveSrcY; |
embeddedartists | 0:c828045bbe69 | 312 | |
embeddedartists | 0:c828045bbe69 | 313 | } while(x!=xx); |
embeddedartists | 0:c828045bbe69 | 314 | } |
embeddedartists | 0:c828045bbe69 | 315 | } |
embeddedartists | 0:c828045bbe69 | 316 | |
embeddedartists | 0:c828045bbe69 | 317 | return; |
embeddedartists | 0:c828045bbe69 | 318 | } |
embeddedartists | 0:c828045bbe69 | 319 | |
embeddedartists | 0:c828045bbe69 | 320 | |
embeddedartists | 0:c828045bbe69 | 321 | short CubeDemo::_sin(short y) const { |
embeddedartists | 0:c828045bbe69 | 322 | static short s1 = 0x6487; |
embeddedartists | 0:c828045bbe69 | 323 | static short s3 = 0x2951; |
embeddedartists | 0:c828045bbe69 | 324 | static short s5 = 0x4f6; |
embeddedartists | 0:c828045bbe69 | 325 | long z, prod, sum; |
embeddedartists | 0:c828045bbe69 | 326 | |
embeddedartists | 0:c828045bbe69 | 327 | z = ((long)y * y) >> 12; |
embeddedartists | 0:c828045bbe69 | 328 | prod = (z * s5) >> 16; |
embeddedartists | 0:c828045bbe69 | 329 | sum = s3 - prod; |
embeddedartists | 0:c828045bbe69 | 330 | prod = (z * sum) >> 16; |
embeddedartists | 0:c828045bbe69 | 331 | sum = s1 - prod; |
embeddedartists | 0:c828045bbe69 | 332 | |
embeddedartists | 0:c828045bbe69 | 333 | // for better accuracy, round here |
embeddedartists | 0:c828045bbe69 | 334 | return (short)((y * sum) >> 13); |
embeddedartists | 0:c828045bbe69 | 335 | } |
embeddedartists | 0:c828045bbe69 | 336 | |
embeddedartists | 0:c828045bbe69 | 337 | short CubeDemo::_cos(short y) const { |
embeddedartists | 0:c828045bbe69 | 338 | static short c0 = 0x7fff; |
embeddedartists | 0:c828045bbe69 | 339 | static short c2 = 0x4eea; |
embeddedartists | 0:c828045bbe69 | 340 | static short c4 = 0x0fc4; |
embeddedartists | 0:c828045bbe69 | 341 | long z, prod, sum; |
embeddedartists | 0:c828045bbe69 | 342 | z = ((long)y * y) >> 12; |
embeddedartists | 0:c828045bbe69 | 343 | prod = (z * c4) >> 16; |
embeddedartists | 0:c828045bbe69 | 344 | sum = c2 - prod; |
embeddedartists | 0:c828045bbe69 | 345 | |
embeddedartists | 0:c828045bbe69 | 346 | // for better accuracy, round here |
embeddedartists | 0:c828045bbe69 | 347 | prod = (z * sum) >> 15; |
embeddedartists | 0:c828045bbe69 | 348 | return (short)(c0 - prod); |
embeddedartists | 0:c828045bbe69 | 349 | } |
embeddedartists | 0:c828045bbe69 | 350 | |
embeddedartists | 0:c828045bbe69 | 351 | short CubeDemo::isine(short x) const { |
embeddedartists | 0:c828045bbe69 | 352 | unsigned short n = (((unsigned short)x + 0x2000) >> 14) & 0x3; |
embeddedartists | 0:c828045bbe69 | 353 | x -= n * 0x4000; |
embeddedartists | 0:c828045bbe69 | 354 | switch(n){ |
embeddedartists | 0:c828045bbe69 | 355 | case 0: |
embeddedartists | 0:c828045bbe69 | 356 | return _sin(x); |
embeddedartists | 0:c828045bbe69 | 357 | case 1: |
embeddedartists | 0:c828045bbe69 | 358 | return _cos(x); |
embeddedartists | 0:c828045bbe69 | 359 | case 2: |
embeddedartists | 0:c828045bbe69 | 360 | return - _sin(x); |
embeddedartists | 0:c828045bbe69 | 361 | case 3: |
embeddedartists | 0:c828045bbe69 | 362 | return - _cos(x); |
embeddedartists | 0:c828045bbe69 | 363 | } |
embeddedartists | 0:c828045bbe69 | 364 | return 0; |
embeddedartists | 0:c828045bbe69 | 365 | } |
embeddedartists | 0:c828045bbe69 | 366 | |
embeddedartists | 0:c828045bbe69 | 367 | |
embeddedartists | 0:c828045bbe69 | 368 | short CubeDemo::icosine(short x) const { |
embeddedartists | 0:c828045bbe69 | 369 | return isine(x + 0x4000); |
embeddedartists | 0:c828045bbe69 | 370 | } |
embeddedartists | 0:c828045bbe69 | 371 | |
embeddedartists | 0:c828045bbe69 | 372 | int32_t CubeDemo::sgn(int32_t val) const |
embeddedartists | 0:c828045bbe69 | 373 | { |
embeddedartists | 0:c828045bbe69 | 374 | if (val > 0) return 1; |
embeddedartists | 0:c828045bbe69 | 375 | else if (val < 0) return -1; |
embeddedartists | 0:c828045bbe69 | 376 | else return 0; |
embeddedartists | 0:c828045bbe69 | 377 | } |
embeddedartists | 0:c828045bbe69 | 378 | |
embeddedartists | 0:c828045bbe69 | 379 | void CubeDemo::createCubeModel(uint32_t radius) |
embeddedartists | 0:c828045bbe69 | 380 | { |
embeddedartists | 0:c828045bbe69 | 381 | uint32_t i,j; |
embeddedartists | 0:c828045bbe69 | 382 | uint32_t theta, dTheta; |
embeddedartists | 0:c828045bbe69 | 383 | static uint8_t cubeConnect[24] = { |
embeddedartists | 0:c828045bbe69 | 384 | 0, 1, 2, 3, |
embeddedartists | 0:c828045bbe69 | 385 | 1, 5, 6, 2, |
embeddedartists | 0:c828045bbe69 | 386 | 5, 4, 7, 6, |
embeddedartists | 0:c828045bbe69 | 387 | 4, 0, 3, 7, |
embeddedartists | 0:c828045bbe69 | 388 | 4, 5, 1, 0, |
embeddedartists | 0:c828045bbe69 | 389 | 3, 2, 6, 7}; |
embeddedartists | 0:c828045bbe69 | 390 | |
embeddedartists | 0:c828045bbe69 | 391 | theta = 0x2000; //PI/4 |
embeddedartists | 0:c828045bbe69 | 392 | dTheta = 0x4000; //PI/2 |
embeddedartists | 0:c828045bbe69 | 393 | |
embeddedartists | 0:c828045bbe69 | 394 | for(i=0; i<8; i++) |
embeddedartists | 0:c828045bbe69 | 395 | { |
embeddedartists | 0:c828045bbe69 | 396 | cubeModel[i].x = radius * sgn(icosine(theta)); |
embeddedartists | 0:c828045bbe69 | 397 | cubeModel[i].y = radius * sgn(isine(theta)); |
embeddedartists | 0:c828045bbe69 | 398 | cubeModel[i].z = radius - (2 * radius) * (i / 4); |
embeddedartists | 0:c828045bbe69 | 399 | theta += dTheta; |
embeddedartists | 0:c828045bbe69 | 400 | } |
embeddedartists | 0:c828045bbe69 | 401 | |
embeddedartists | 0:c828045bbe69 | 402 | j=0; |
embeddedartists | 0:c828045bbe69 | 403 | for(i=0; i<6; i++) |
embeddedartists | 0:c828045bbe69 | 404 | { |
embeddedartists | 0:c828045bbe69 | 405 | cubePoly[j].p1 = cubeConnect[i*4 + 3]; |
embeddedartists | 0:c828045bbe69 | 406 | cubePoly[j].p2 = cubeConnect[i*4 + 1]; |
embeddedartists | 0:c828045bbe69 | 407 | cubePoly[j].p3 = cubeConnect[i*4 + 0]; |
embeddedartists | 0:c828045bbe69 | 408 | j++; |
embeddedartists | 0:c828045bbe69 | 409 | cubePoly[j].p1 = cubeConnect[i*4 + 3]; |
embeddedartists | 0:c828045bbe69 | 410 | cubePoly[j].p2 = cubeConnect[i*4 + 2]; |
embeddedartists | 0:c828045bbe69 | 411 | cubePoly[j].p3 = cubeConnect[i*4 + 1]; |
embeddedartists | 0:c828045bbe69 | 412 | j++; |
embeddedartists | 0:c828045bbe69 | 413 | } |
embeddedartists | 0:c828045bbe69 | 414 | } |
embeddedartists | 0:c828045bbe69 | 415 | |
embeddedartists | 0:c828045bbe69 | 416 | void CubeDemo::rotateAndProject(uint16_t rotX, uint16_t rotY, uint16_t rotZ) |
embeddedartists | 0:c828045bbe69 | 417 | { |
embeddedartists | 0:c828045bbe69 | 418 | uint32_t i; |
embeddedartists | 0:c828045bbe69 | 419 | long crx,srx,cry,sry,crz,srz; |
embeddedartists | 0:c828045bbe69 | 420 | long x,y,z,tx,ty,tz; |
embeddedartists | 0:c828045bbe69 | 421 | long distance; |
embeddedartists | 0:c828045bbe69 | 422 | |
embeddedartists | 0:c828045bbe69 | 423 | crx = icosine(rotX); |
embeddedartists | 0:c828045bbe69 | 424 | srx = isine(rotX); |
embeddedartists | 0:c828045bbe69 | 425 | cry = icosine(rotY); |
embeddedartists | 0:c828045bbe69 | 426 | sry = isine(rotY); |
embeddedartists | 0:c828045bbe69 | 427 | crz = icosine(rotZ); |
embeddedartists | 0:c828045bbe69 | 428 | srz = isine(rotZ); |
embeddedartists | 0:c828045bbe69 | 429 | |
embeddedartists | 0:c828045bbe69 | 430 | for (i=0;i<8;i++) |
embeddedartists | 0:c828045bbe69 | 431 | { |
embeddedartists | 0:c828045bbe69 | 432 | //--> rotate around y-axis |
embeddedartists | 0:c828045bbe69 | 433 | // var tempX = (this.x * Math.cos(rY)) - (this.z * Math.sin(rY)); |
embeddedartists | 0:c828045bbe69 | 434 | // var tempZ = (this.x * Math.sin(rY)) + (this.z * Math.cos(rY)); |
embeddedartists | 0:c828045bbe69 | 435 | |
embeddedartists | 0:c828045bbe69 | 436 | //--> rotate around x-axis |
embeddedartists | 0:c828045bbe69 | 437 | // this.dz = (tempZ * Math.cos(rX)) - (this.y * Math.sin(rX)); |
embeddedartists | 0:c828045bbe69 | 438 | // var tempY = (tempZ * Math.sin(rX)) + (this.y * Math.cos(rX)); |
embeddedartists | 0:c828045bbe69 | 439 | |
embeddedartists | 0:c828045bbe69 | 440 | //--> rotate around z-axis |
embeddedartists | 0:c828045bbe69 | 441 | // this.dx = (tempX * Math.cos(rZ)) + (tempY * Math.sin(rZ)); |
embeddedartists | 0:c828045bbe69 | 442 | // this.dy = (tempY * Math.cos(rZ)) - (tempX * Math.sin(rZ)); |
embeddedartists | 0:c828045bbe69 | 443 | |
embeddedartists | 0:c828045bbe69 | 444 | |
embeddedartists | 0:c828045bbe69 | 445 | x = cubeModel[i].x; |
embeddedartists | 0:c828045bbe69 | 446 | y = cubeModel[i].y; |
embeddedartists | 0:c828045bbe69 | 447 | z = cubeModel[i].z; |
embeddedartists | 0:c828045bbe69 | 448 | |
embeddedartists | 0:c828045bbe69 | 449 | //--> rotate around y-axis |
embeddedartists | 0:c828045bbe69 | 450 | tx = (z*sry + x*cry) / 0x7fff; |
embeddedartists | 0:c828045bbe69 | 451 | tz = (z*cry - x*sry) / 0x7fff; |
embeddedartists | 0:c828045bbe69 | 452 | |
embeddedartists | 0:c828045bbe69 | 453 | //--> rotate around x-axis |
embeddedartists | 0:c828045bbe69 | 454 | cubeModel[i].zr = (y*srx + tz*crx) / 0x7fff; |
embeddedartists | 0:c828045bbe69 | 455 | ty = (y*crx - tz*srx) / 0x7fff; |
embeddedartists | 0:c828045bbe69 | 456 | |
embeddedartists | 0:c828045bbe69 | 457 | //--> rotate around z-axis |
embeddedartists | 0:c828045bbe69 | 458 | cubeModel[i].xr = (tx*crz - ty*srz) / 0x7fff; |
embeddedartists | 0:c828045bbe69 | 459 | cubeModel[i].yr = (tx*srz + ty*crz) / 0x7fff; |
embeddedartists | 0:c828045bbe69 | 460 | |
embeddedartists | 0:c828045bbe69 | 461 | cubeModel[i].xr -= camX; |
embeddedartists | 0:c828045bbe69 | 462 | cubeModel[i].yr -= camY; |
embeddedartists | 0:c828045bbe69 | 463 | cubeModel[i].zr -= camZ; |
embeddedartists | 0:c828045bbe69 | 464 | |
embeddedartists | 0:c828045bbe69 | 465 | distance = LENS - cubeModel[i].zr; |
embeddedartists | 0:c828045bbe69 | 466 | if (distance > 0) |
embeddedartists | 0:c828045bbe69 | 467 | { |
embeddedartists | 0:c828045bbe69 | 468 | cubeModel[i].scrx = X_OFFSET + (LENS * cubeModel[i].xr) / distance; |
embeddedartists | 0:c828045bbe69 | 469 | cubeModel[i].scry = Y_OFFSET + (LENS * cubeModel[i].yr) / distance; |
embeddedartists | 0:c828045bbe69 | 470 | } |
embeddedartists | 0:c828045bbe69 | 471 | } |
embeddedartists | 0:c828045bbe69 | 472 | } |
embeddedartists | 0:c828045bbe69 | 473 | |
embeddedartists | 0:c828045bbe69 | 474 | void CubeDemo::drawCubeZ(Surface_t *pSourcePicture, uint8_t shades) |
embeddedartists | 0:c828045bbe69 | 475 | { |
embeddedartists | 0:c828045bbe69 | 476 | uint32_t i; |
embeddedartists | 0:c828045bbe69 | 477 | int32_t x1,x2,x3,y1,y2,y3,/*u1,u2,u3,v1,v2,v3,*/znormal; |
embeddedartists | 0:c828045bbe69 | 478 | |
embeddedartists | 0:c828045bbe69 | 479 | activeFrame.w = this->windowX; |
embeddedartists | 0:c828045bbe69 | 480 | activeFrame.h = this->windowY; |
embeddedartists | 0:c828045bbe69 | 481 | activeFrame.pixels = (uint16_t*)this->pFrmBuf; |
embeddedartists | 0:c828045bbe69 | 482 | |
embeddedartists | 0:c828045bbe69 | 483 | for(i=0; i<12; i++) |
embeddedartists | 0:c828045bbe69 | 484 | { |
embeddedartists | 0:c828045bbe69 | 485 | x1 = cubeModel[cubePoly[i].p1].scrx; //Get triangles from "projected" |
embeddedartists | 0:c828045bbe69 | 486 | x2 = cubeModel[cubePoly[i].p2].scrx; //X and Y coords since Znormal |
embeddedartists | 0:c828045bbe69 | 487 | x3 = cubeModel[cubePoly[i].p3].scrx; //Does not require a Z coord |
embeddedartists | 0:c828045bbe69 | 488 | y1 = cubeModel[cubePoly[i].p1].scry; //V1= Point1 connected to V2 then |
embeddedartists | 0:c828045bbe69 | 489 | y2 = cubeModel[cubePoly[i].p2].scry; //V2 to V3 and so on... |
embeddedartists | 0:c828045bbe69 | 490 | y3 = cubeModel[cubePoly[i].p3].scry; |
embeddedartists | 0:c828045bbe69 | 491 | |
embeddedartists | 0:c828045bbe69 | 492 | znormal = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3); |
embeddedartists | 0:c828045bbe69 | 493 | if (znormal > 0) |
embeddedartists | 0:c828045bbe69 | 494 | { |
embeddedartists | 0:c828045bbe69 | 495 | //no texture? |
embeddedartists | 0:c828045bbe69 | 496 | if (pSourcePicture == NULL) |
embeddedartists | 0:c828045bbe69 | 497 | { |
embeddedartists | 0:c828045bbe69 | 498 | if (shades == 0) |
embeddedartists | 0:c828045bbe69 | 499 | { |
embeddedartists | 0:c828045bbe69 | 500 | if ((cubePoly[i].p1 == 0) && (cubePoly[i].p2 == 1)) |
embeddedartists | 0:c828045bbe69 | 501 | graphics.put_line( cubeModel[cubePoly[i].p1].scrx, cubeModel[cubePoly[i].p1].scry, cubeModel[cubePoly[i].p2].scrx, cubeModel[cubePoly[i].p2].scry, RED); |
embeddedartists | 0:c828045bbe69 | 502 | else |
embeddedartists | 0:c828045bbe69 | 503 | graphics.put_line( cubeModel[cubePoly[i].p1].scrx, cubeModel[cubePoly[i].p1].scry, cubeModel[cubePoly[i].p2].scrx, cubeModel[cubePoly[i].p2].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 504 | graphics.put_line( cubeModel[cubePoly[i].p2].scrx, cubeModel[cubePoly[i].p2].scry, cubeModel[cubePoly[i].p3].scrx, cubeModel[cubePoly[i].p3].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 505 | graphics.put_line( cubeModel[cubePoly[i].p3].scrx, cubeModel[cubePoly[i].p3].scry, cubeModel[cubePoly[i].p1].scrx, cubeModel[cubePoly[i].p1].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 506 | } |
embeddedartists | 0:c828045bbe69 | 507 | else |
embeddedartists | 0:c828045bbe69 | 508 | { |
embeddedartists | 0:c828045bbe69 | 509 | Coord2D_t SrcCoords[3], DstCoords[3]; |
embeddedartists | 0:c828045bbe69 | 510 | Surface_t a; |
embeddedartists | 0:c828045bbe69 | 511 | static uint16_t pix; |
embeddedartists | 0:c828045bbe69 | 512 | |
embeddedartists | 0:c828045bbe69 | 513 | a.w = this->windowX; |
embeddedartists | 0:c828045bbe69 | 514 | a.h = this->windowY; |
embeddedartists | 0:c828045bbe69 | 515 | a.pixels = &pix; |
embeddedartists | 0:c828045bbe69 | 516 | if ((i&1) == 0) |
embeddedartists | 0:c828045bbe69 | 517 | { |
embeddedartists | 0:c828045bbe69 | 518 | if ((znormal / 400) > 31) |
embeddedartists | 0:c828045bbe69 | 519 | pix = 0x1f << 11; |
embeddedartists | 0:c828045bbe69 | 520 | else |
embeddedartists | 0:c828045bbe69 | 521 | pix = (znormal / 400 & 0x1f) << 11; |
embeddedartists | 0:c828045bbe69 | 522 | } |
embeddedartists | 0:c828045bbe69 | 523 | SrcCoords[0].x = 0; |
embeddedartists | 0:c828045bbe69 | 524 | SrcCoords[0].y = 0; |
embeddedartists | 0:c828045bbe69 | 525 | SrcCoords[1].x = 0; |
embeddedartists | 0:c828045bbe69 | 526 | SrcCoords[1].y = 0; |
embeddedartists | 0:c828045bbe69 | 527 | SrcCoords[2].x = 0; |
embeddedartists | 0:c828045bbe69 | 528 | SrcCoords[2].y = 0; |
embeddedartists | 0:c828045bbe69 | 529 | DstCoords[0].x = cubeModel[cubePoly[i].p1].scrx; |
embeddedartists | 0:c828045bbe69 | 530 | DstCoords[0].y = cubeModel[cubePoly[i].p1].scry; |
embeddedartists | 0:c828045bbe69 | 531 | DstCoords[1].x = cubeModel[cubePoly[i].p2].scrx; |
embeddedartists | 0:c828045bbe69 | 532 | DstCoords[1].y = cubeModel[cubePoly[i].p2].scry; |
embeddedartists | 0:c828045bbe69 | 533 | DstCoords[2].x = cubeModel[cubePoly[i].p3].scrx; |
embeddedartists | 0:c828045bbe69 | 534 | DstCoords[2].y = cubeModel[cubePoly[i].p3].scry; |
embeddedartists | 0:c828045bbe69 | 535 | TriangleProjectFast(&a, &activeFrame, SrcCoords, DstCoords); |
embeddedartists | 0:c828045bbe69 | 536 | } |
embeddedartists | 0:c828045bbe69 | 537 | } |
embeddedartists | 0:c828045bbe69 | 538 | |
embeddedartists | 0:c828045bbe69 | 539 | //Render with texture |
embeddedartists | 0:c828045bbe69 | 540 | else |
embeddedartists | 0:c828045bbe69 | 541 | { |
embeddedartists | 0:c828045bbe69 | 542 | Coord2D_t SrcCoords[3], DstCoords[3]; |
embeddedartists | 0:c828045bbe69 | 543 | #if 1 |
embeddedartists | 0:c828045bbe69 | 544 | /* |
embeddedartists | 0:c828045bbe69 | 545 | * Image in the file |
embeddedartists | 0:c828045bbe69 | 546 | * 0,0_______ _______ _______ |
embeddedartists | 0:c828045bbe69 | 547 | * | | | *| |
embeddedartists | 0:c828045bbe69 | 548 | * | | | | Image as it is used |
embeddedartists | 0:c828045bbe69 | 549 | * |_______|_______|_______| _______ _______ _______ _______ |
embeddedartists | 0:c828045bbe69 | 550 | * | | | | \ |* | | | | |
embeddedartists | 0:c828045bbe69 | 551 | * | | | | \ | | top | | | |
embeddedartists | 0:c828045bbe69 | 552 | * |_______|_______|_______| ====== |_______|_______|_______|_______| |
embeddedartists | 0:c828045bbe69 | 553 | * | | | | / | | | | | |
embeddedartists | 0:c828045bbe69 | 554 | * | | | | / | side1 | side2 | side3 | side4 | |
embeddedartists | 0:c828045bbe69 | 555 | * |_______|_______|_______| |_______|_______|_______|_______| |
embeddedartists | 0:c828045bbe69 | 556 | * | | | | | | | | | |
embeddedartists | 0:c828045bbe69 | 557 | * | | | | | | bottom| | | |
embeddedartists | 0:c828045bbe69 | 558 | * |_______|_______|_______| |_______|_______|_______|_______| |
embeddedartists | 0:c828045bbe69 | 559 | * 150,200 |
embeddedartists | 0:c828045bbe69 | 560 | * All coordinates below are in the left image above and with coordinates 0,0 in the |
embeddedartists | 0:c828045bbe69 | 561 | * upper left corner and 150,200 in the lower right corner. |
embeddedartists | 0:c828045bbe69 | 562 | * |
embeddedartists | 0:c828045bbe69 | 563 | */ |
embeddedartists | 0:c828045bbe69 | 564 | #define XYZ {49,0},{0,0},{0,50},{50,50} |
embeddedartists | 0:c828045bbe69 | 565 | static Coord2D_t a[24] = {{49,50}, {0,50}, {0,99}, {49,99}, //bottom 6 |
embeddedartists | 0:c828045bbe69 | 566 | {99,50}, {50,50}, {50,99}, {99,99}, //side2 2 |
embeddedartists | 0:c828045bbe69 | 567 | {149,50},{100,50},{100,99},{149,99}, //top 5 |
embeddedartists | 0:c828045bbe69 | 568 | {50,150},{99,150},{99,199},{50,199}, //side4 4 |
embeddedartists | 0:c828045bbe69 | 569 | {99,49}, {99,0}, {50,0}, {50,49}, //side1 1 |
embeddedartists | 0:c828045bbe69 | 570 | {99,100},{99,149},{50,149},{50,100}}; //side3 3 |
embeddedartists | 0:c828045bbe69 | 571 | #elif 0 |
embeddedartists | 0:c828045bbe69 | 572 | static Coord2D_t a[24] = {{49,50}, {0,50}, {0,99}, {49,99}, //bottom 6 |
embeddedartists | 0:c828045bbe69 | 573 | {99,50}, {50,50}, {50,99}, {99,99}, //side2 2 |
embeddedartists | 0:c828045bbe69 | 574 | {149,50},{100,50},{100,99},{149,99}, //top 5 |
embeddedartists | 0:c828045bbe69 | 575 | {99,150},{50,150},{50,199},{99,199}, //side4 4 |
embeddedartists | 0:c828045bbe69 | 576 | {99,49}, {99,0}, {50,0}, {50,49}, //side1 1 |
embeddedartists | 0:c828045bbe69 | 577 | {99,149},{99,100},{50,100},{50,149}}; //side3 3 |
embeddedartists | 0:c828045bbe69 | 578 | #else |
embeddedartists | 0:c828045bbe69 | 579 | static Coord2D_t a[24] = {{49,50}, {0,50}, {0,99}, {49,99}, |
embeddedartists | 0:c828045bbe69 | 580 | {99,50}, {50,50}, {50,99}, {99,99}, |
embeddedartists | 0:c828045bbe69 | 581 | {149,50},{100,50},{100,99},{149,99}, |
embeddedartists | 0:c828045bbe69 | 582 | {99,199},{50,199},{50,150},{99,150}, |
embeddedartists | 0:c828045bbe69 | 583 | {50,0}, {99,0}, {99,49}, {50,49}, //top |
embeddedartists | 0:c828045bbe69 | 584 | {50,100},{99,100},{99,149},{50,149}};//bottom |
embeddedartists | 0:c828045bbe69 | 585 | #endif |
embeddedartists | 0:c828045bbe69 | 586 | |
embeddedartists | 0:c828045bbe69 | 587 | if (i&1) |
embeddedartists | 0:c828045bbe69 | 588 | { |
embeddedartists | 0:c828045bbe69 | 589 | SrcCoords[0].x = a[(i/2)*4+2].x; |
embeddedartists | 0:c828045bbe69 | 590 | SrcCoords[0].y = a[(i/2)*4+2].y; |
embeddedartists | 0:c828045bbe69 | 591 | SrcCoords[1].x = a[(i/2)*4+3].x; |
embeddedartists | 0:c828045bbe69 | 592 | SrcCoords[1].y = a[(i/2)*4+3].y; |
embeddedartists | 0:c828045bbe69 | 593 | SrcCoords[2].x = a[(i/2)*4+0].x; |
embeddedartists | 0:c828045bbe69 | 594 | SrcCoords[2].y = a[(i/2)*4+0].y; |
embeddedartists | 0:c828045bbe69 | 595 | DstCoords[0].x = cubeModel[cubePoly[i].p1].scrx; |
embeddedartists | 0:c828045bbe69 | 596 | DstCoords[0].y = cubeModel[cubePoly[i].p1].scry; |
embeddedartists | 0:c828045bbe69 | 597 | DstCoords[1].x = cubeModel[cubePoly[i].p2].scrx; |
embeddedartists | 0:c828045bbe69 | 598 | DstCoords[1].y = cubeModel[cubePoly[i].p2].scry; |
embeddedartists | 0:c828045bbe69 | 599 | DstCoords[2].x = cubeModel[cubePoly[i].p3].scrx; |
embeddedartists | 0:c828045bbe69 | 600 | DstCoords[2].y = cubeModel[cubePoly[i].p3].scry; |
embeddedartists | 0:c828045bbe69 | 601 | TriangleProjectFast(pSourcePicture, &activeFrame, SrcCoords, DstCoords); |
embeddedartists | 0:c828045bbe69 | 602 | } |
embeddedartists | 0:c828045bbe69 | 603 | else |
embeddedartists | 0:c828045bbe69 | 604 | { |
embeddedartists | 0:c828045bbe69 | 605 | SrcCoords[0].x = a[(i/2)*4+0].x; //0 |
embeddedartists | 0:c828045bbe69 | 606 | SrcCoords[0].y = a[(i/2)*4+0].y; //0 |
embeddedartists | 0:c828045bbe69 | 607 | SrcCoords[1].x = a[(i/2)*4+1].x; //31; |
embeddedartists | 0:c828045bbe69 | 608 | SrcCoords[1].y = a[(i/2)*4+1].y; //0; |
embeddedartists | 0:c828045bbe69 | 609 | SrcCoords[2].x = a[(i/2)*4+2].x; //31; |
embeddedartists | 0:c828045bbe69 | 610 | SrcCoords[2].y = a[(i/2)*4+2].y; //31; |
embeddedartists | 0:c828045bbe69 | 611 | DstCoords[0].x = cubeModel[cubePoly[i].p2].scrx; |
embeddedartists | 0:c828045bbe69 | 612 | DstCoords[0].y = cubeModel[cubePoly[i].p2].scry; |
embeddedartists | 0:c828045bbe69 | 613 | DstCoords[1].x = cubeModel[cubePoly[i].p3].scrx; |
embeddedartists | 0:c828045bbe69 | 614 | DstCoords[1].y = cubeModel[cubePoly[i].p3].scry; |
embeddedartists | 0:c828045bbe69 | 615 | DstCoords[2].x = cubeModel[cubePoly[i].p1].scrx; |
embeddedartists | 0:c828045bbe69 | 616 | DstCoords[2].y = cubeModel[cubePoly[i].p1].scry; |
embeddedartists | 0:c828045bbe69 | 617 | TriangleProjectFast(pSourcePicture, &activeFrame, SrcCoords, DstCoords); |
embeddedartists | 0:c828045bbe69 | 618 | } |
embeddedartists | 0:c828045bbe69 | 619 | } |
embeddedartists | 0:c828045bbe69 | 620 | } |
embeddedartists | 0:c828045bbe69 | 621 | } |
embeddedartists | 0:c828045bbe69 | 622 | } |
embeddedartists | 0:c828045bbe69 | 623 | |
embeddedartists | 0:c828045bbe69 | 624 | void CubeDemo::render(uint32_t idx) |
embeddedartists | 0:c828045bbe69 | 625 | { |
embeddedartists | 0:c828045bbe69 | 626 | static uint8_t cnt=0; |
embeddedartists | 0:c828045bbe69 | 627 | |
embeddedartists | 0:c828045bbe69 | 628 | if (cnt == 0) |
embeddedartists | 0:c828045bbe69 | 629 | { |
embeddedartists | 0:c828045bbe69 | 630 | cnt = 1; |
embeddedartists | 0:c828045bbe69 | 631 | pFrmBuf = pFrmBuf1; |
embeddedartists | 0:c828045bbe69 | 632 | } |
embeddedartists | 0:c828045bbe69 | 633 | else if (cnt == 1) |
embeddedartists | 0:c828045bbe69 | 634 | { |
embeddedartists | 0:c828045bbe69 | 635 | cnt = 2; |
embeddedartists | 0:c828045bbe69 | 636 | pFrmBuf = pFrmBuf2; |
embeddedartists | 0:c828045bbe69 | 637 | } |
embeddedartists | 0:c828045bbe69 | 638 | else |
embeddedartists | 0:c828045bbe69 | 639 | { |
embeddedartists | 0:c828045bbe69 | 640 | cnt = 0; |
embeddedartists | 0:c828045bbe69 | 641 | pFrmBuf = pFrmBuf3; |
embeddedartists | 0:c828045bbe69 | 642 | } |
embeddedartists | 0:c828045bbe69 | 643 | |
embeddedartists | 0:c828045bbe69 | 644 | graphics.setFrameBuffer(pFrmBuf); |
embeddedartists | 0:c828045bbe69 | 645 | |
embeddedartists | 0:c828045bbe69 | 646 | // rendering here |
embeddedartists | 0:c828045bbe69 | 647 | memset((void*)(pFrmBuf), BACKGROUND_COLOR, this->windowX * this->windowY * 2); |
embeddedartists | 0:c828045bbe69 | 648 | |
embeddedartists | 0:c828045bbe69 | 649 | rotateAndProject(rx, ry, 0); |
embeddedartists | 0:c828045bbe69 | 650 | |
embeddedartists | 0:c828045bbe69 | 651 | switch(mode) |
embeddedartists | 0:c828045bbe69 | 652 | { |
embeddedartists | 0:c828045bbe69 | 653 | case 0: //draw just points |
embeddedartists | 0:c828045bbe69 | 654 | graphics.put_circle( cubeModel[0].scrx, cubeModel[0].scry, RED, 2, 1); |
embeddedartists | 0:c828045bbe69 | 655 | graphics.put_circle( cubeModel[1].scrx, cubeModel[1].scry, SMALL_CIRCLE_FRONT_COLOR, 2, 1); |
embeddedartists | 0:c828045bbe69 | 656 | graphics.put_circle( cubeModel[2].scrx, cubeModel[2].scry, GREEN, 2, 1); |
embeddedartists | 0:c828045bbe69 | 657 | graphics.put_circle( cubeModel[3].scrx, cubeModel[3].scry, SMALL_CIRCLE_FRONT_COLOR, 2, 1); |
embeddedartists | 0:c828045bbe69 | 658 | graphics.put_circle( cubeModel[4].scrx, cubeModel[4].scry, SMALL_CIRCLE_FRONT_COLOR, 2, 1); |
embeddedartists | 0:c828045bbe69 | 659 | graphics.put_circle( cubeModel[5].scrx, cubeModel[5].scry, SMALL_CIRCLE_FRONT_COLOR, 2, 1); |
embeddedartists | 0:c828045bbe69 | 660 | graphics.put_circle( cubeModel[6].scrx, cubeModel[6].scry, SMALL_CIRCLE_FRONT_COLOR, 2, 1); |
embeddedartists | 0:c828045bbe69 | 661 | graphics.put_circle( cubeModel[7].scrx, cubeModel[7].scry, SMALL_CIRCLE_FRONT_COLOR, 2, 1); |
embeddedartists | 0:c828045bbe69 | 662 | break; |
embeddedartists | 0:c828045bbe69 | 663 | case 1: //draw lines points |
embeddedartists | 0:c828045bbe69 | 664 | graphics.put_line( cubeModel[0].scrx, cubeModel[0].scry, cubeModel[1].scrx, cubeModel[1].scry, RED); |
embeddedartists | 0:c828045bbe69 | 665 | graphics.put_line( cubeModel[1].scrx, cubeModel[1].scry, cubeModel[2].scrx, cubeModel[2].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 666 | graphics.put_line( cubeModel[2].scrx, cubeModel[2].scry, cubeModel[3].scrx, cubeModel[3].scry, GREEN); |
embeddedartists | 0:c828045bbe69 | 667 | graphics.put_line( cubeModel[3].scrx, cubeModel[3].scry, cubeModel[0].scrx, cubeModel[0].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 668 | |
embeddedartists | 0:c828045bbe69 | 669 | graphics.put_line( cubeModel[5].scrx, cubeModel[5].scry, cubeModel[4].scrx, cubeModel[4].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 670 | graphics.put_line( cubeModel[4].scrx, cubeModel[4].scry, cubeModel[7].scrx, cubeModel[7].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 671 | graphics.put_line( cubeModel[7].scrx, cubeModel[7].scry, cubeModel[6].scrx, cubeModel[6].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 672 | graphics.put_line( cubeModel[6].scrx, cubeModel[6].scry, cubeModel[5].scrx, cubeModel[5].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 673 | |
embeddedartists | 0:c828045bbe69 | 674 | graphics.put_line( cubeModel[0].scrx, cubeModel[0].scry, cubeModel[4].scrx, cubeModel[4].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 675 | graphics.put_line( cubeModel[1].scrx, cubeModel[1].scry, cubeModel[5].scrx, cubeModel[5].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 676 | graphics.put_line( cubeModel[2].scrx, cubeModel[2].scry, cubeModel[6].scrx, cubeModel[6].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 677 | graphics.put_line( cubeModel[3].scrx, cubeModel[3].scry, cubeModel[7].scrx, cubeModel[7].scry, SMALL_CIRCLE_FRONT_COLOR); |
embeddedartists | 0:c828045bbe69 | 678 | break; |
embeddedartists | 0:c828045bbe69 | 679 | case 2: //draw cube with texture |
embeddedartists | 0:c828045bbe69 | 680 | if (sourcePicture1.pixels == NULL) |
embeddedartists | 0:c828045bbe69 | 681 | { |
embeddedartists | 0:c828045bbe69 | 682 | drawCubeZ( NULL, false); |
embeddedartists | 0:c828045bbe69 | 683 | } |
embeddedartists | 0:c828045bbe69 | 684 | else |
embeddedartists | 0:c828045bbe69 | 685 | { |
embeddedartists | 0:c828045bbe69 | 686 | drawCubeZ( &sourcePicture1, false); |
embeddedartists | 0:c828045bbe69 | 687 | } |
embeddedartists | 0:c828045bbe69 | 688 | break; |
embeddedartists | 0:c828045bbe69 | 689 | case 3: //draw cube with texture |
embeddedartists | 0:c828045bbe69 | 690 | drawCubeZ( NULL, true); |
embeddedartists | 0:c828045bbe69 | 691 | break; |
embeddedartists | 0:c828045bbe69 | 692 | case 4: //draw cube with texture |
embeddedartists | 0:c828045bbe69 | 693 | if (sourcePicture2.pixels == NULL) |
embeddedartists | 0:c828045bbe69 | 694 | { |
embeddedartists | 0:c828045bbe69 | 695 | drawCubeZ( NULL, false); |
embeddedartists | 0:c828045bbe69 | 696 | } |
embeddedartists | 0:c828045bbe69 | 697 | else |
embeddedartists | 0:c828045bbe69 | 698 | { |
embeddedartists | 0:c828045bbe69 | 699 | drawCubeZ( &sourcePicture2, false); |
embeddedartists | 0:c828045bbe69 | 700 | } |
embeddedartists | 0:c828045bbe69 | 701 | break; |
embeddedartists | 0:c828045bbe69 | 702 | case 5: //draw sorted lines |
embeddedartists | 0:c828045bbe69 | 703 | drawCubeZ( NULL, false); |
embeddedartists | 0:c828045bbe69 | 704 | break; |
embeddedartists | 0:c828045bbe69 | 705 | default: |
embeddedartists | 0:c828045bbe69 | 706 | mode = 0; |
embeddedartists | 0:c828045bbe69 | 707 | break; |
embeddedartists | 0:c828045bbe69 | 708 | } |
embeddedartists | 0:c828045bbe69 | 709 | ry += ry_; |
embeddedartists | 0:c828045bbe69 | 710 | rx += rx_; |
embeddedartists | 0:c828045bbe69 | 711 | } |
embeddedartists | 0:c828045bbe69 | 712 | |
embeddedartists | 0:c828045bbe69 | 713 | /*********************************************************************** |
embeddedartists | 0:c828045bbe69 | 714 | * Private functions |
embeddedartists | 0:c828045bbe69 | 715 | **********************************************************************/ |
embeddedartists | 0:c828045bbe69 | 716 | |
embeddedartists | 0:c828045bbe69 | 717 | void CubeDemo::initialize() |
embeddedartists | 0:c828045bbe69 | 718 | { |
embeddedartists | 0:c828045bbe69 | 719 | mode = 3; |
embeddedartists | 0:c828045bbe69 | 720 | rx = 0x9000; |
embeddedartists | 0:c828045bbe69 | 721 | ry = 0; |
embeddedartists | 0:c828045bbe69 | 722 | ry_ = 350; |
embeddedartists | 0:c828045bbe69 | 723 | rx_ = -25; //-3 |
embeddedartists | 0:c828045bbe69 | 724 | camX = 0; |
embeddedartists | 0:c828045bbe69 | 725 | camY = 0; |
embeddedartists | 0:c828045bbe69 | 726 | camZ = 128; |
embeddedartists | 0:c828045bbe69 | 727 | |
embeddedartists | 0:c828045bbe69 | 728 | createCubeModel(70); |
embeddedartists | 0:c828045bbe69 | 729 | |
embeddedartists | 0:c828045bbe69 | 730 | Image::ImageData_t d; |
embeddedartists | 0:c828045bbe69 | 731 | if (Image::decode(cube_image1, cube_image1_sz, &d) == 0) { |
embeddedartists | 0:c828045bbe69 | 732 | sourcePicture1.w = d.width; |
embeddedartists | 0:c828045bbe69 | 733 | sourcePicture1.h = d.height; |
embeddedartists | 0:c828045bbe69 | 734 | sourcePicture1.pixels = d.pixels; |
embeddedartists | 0:c828045bbe69 | 735 | } |
embeddedartists | 0:c828045bbe69 | 736 | if (Image::decode(cube_image2, cube_image2_sz, &d) == 0) { |
embeddedartists | 0:c828045bbe69 | 737 | sourcePicture2.w = d.width; |
embeddedartists | 0:c828045bbe69 | 738 | sourcePicture2.h = d.height; |
embeddedartists | 0:c828045bbe69 | 739 | sourcePicture2.pixels = d.pixels; |
embeddedartists | 0:c828045bbe69 | 740 | } |
embeddedartists | 0:c828045bbe69 | 741 | } |
embeddedartists | 0:c828045bbe69 | 742 | |
embeddedartists | 0:c828045bbe69 | 743 | |
embeddedartists | 0:c828045bbe69 | 744 | /****************************************************************************** |
embeddedartists | 0:c828045bbe69 | 745 | * Public functions |
embeddedartists | 0:c828045bbe69 | 746 | *****************************************************************************/ |
embeddedartists | 0:c828045bbe69 | 747 | CubeDemo::CubeDemo(uint8_t *pFrameBuf, uint16_t dispWidth, uint16_t dispHeight) |
embeddedartists | 0:c828045bbe69 | 748 | : graphics((uint16_t *)pFrameBuf, dispWidth, dispHeight) { |
embeddedartists | 0:c828045bbe69 | 749 | |
embeddedartists | 0:c828045bbe69 | 750 | this->windowX = dispWidth; |
embeddedartists | 0:c828045bbe69 | 751 | this->windowY = dispHeight; |
embeddedartists | 0:c828045bbe69 | 752 | this->pFrmBuf = (uint16_t *)pFrameBuf; |
embeddedartists | 0:c828045bbe69 | 753 | this->pFrmBuf1 = (uint16_t *)pFrameBuf; |
embeddedartists | 0:c828045bbe69 | 754 | this->pFrmBuf2 = (uint16_t *)((uint32_t)pFrameBuf + dispWidth*dispHeight*2); |
embeddedartists | 0:c828045bbe69 | 755 | this->pFrmBuf3 = (uint16_t *)((uint32_t)pFrameBuf + dispWidth*dispHeight*4); |
embeddedartists | 0:c828045bbe69 | 756 | |
embeddedartists | 0:c828045bbe69 | 757 | sourcePicture1.w = 0; |
embeddedartists | 0:c828045bbe69 | 758 | sourcePicture1.h = 0; |
embeddedartists | 0:c828045bbe69 | 759 | sourcePicture1.pixels = NULL; |
embeddedartists | 0:c828045bbe69 | 760 | sourcePicture2.w = 0; |
embeddedartists | 0:c828045bbe69 | 761 | sourcePicture2.h = 0; |
embeddedartists | 0:c828045bbe69 | 762 | sourcePicture2.pixels = NULL; |
embeddedartists | 0:c828045bbe69 | 763 | |
embeddedartists | 0:c828045bbe69 | 764 | initialize(); |
embeddedartists | 0:c828045bbe69 | 765 | } |
embeddedartists | 0:c828045bbe69 | 766 | |
embeddedartists | 0:c828045bbe69 | 767 | CubeDemo::~CubeDemo() |
embeddedartists | 0:c828045bbe69 | 768 | { |
embeddedartists | 0:c828045bbe69 | 769 | if (sourcePicture1.pixels != NULL) { |
embeddedartists | 0:c828045bbe69 | 770 | free(sourcePicture1.pixels); |
embeddedartists | 0:c828045bbe69 | 771 | } |
embeddedartists | 0:c828045bbe69 | 772 | if (sourcePicture2.pixels != NULL) { |
embeddedartists | 0:c828045bbe69 | 773 | free(sourcePicture2.pixels); |
embeddedartists | 0:c828045bbe69 | 774 | } |
embeddedartists | 0:c828045bbe69 | 775 | } |
embeddedartists | 0:c828045bbe69 | 776 | void CubeDemo::run(EaLcdBoardGPIO& lcdBoard, uint32_t loops, uint32_t delayMs) { |
embeddedartists | 0:c828045bbe69 | 777 | |
embeddedartists | 0:c828045bbe69 | 778 | printf("CubeDemo, %d loops, %dms delay\n", loops, delayMs); |
embeddedartists | 0:c828045bbe69 | 779 | |
embeddedartists | 0:c828045bbe69 | 780 | for(int32_t n=0;n<loops;n++) { |
embeddedartists | 0:c828045bbe69 | 781 | |
embeddedartists | 0:c828045bbe69 | 782 | mode = ((n/128) % 5); |
embeddedartists | 0:c828045bbe69 | 783 | //mode = 4; |
embeddedartists | 0:c828045bbe69 | 784 | |
embeddedartists | 0:c828045bbe69 | 785 | //render globe |
embeddedartists | 0:c828045bbe69 | 786 | render(n); |
embeddedartists | 0:c828045bbe69 | 787 | |
embeddedartists | 0:c828045bbe69 | 788 | //update framebuffer |
embeddedartists | 0:c828045bbe69 | 789 | lcdBoard.setFrameBuffer((uint32_t)this->pFrmBuf); |
embeddedartists | 0:c828045bbe69 | 790 | |
embeddedartists | 0:c828045bbe69 | 791 | if (mode == 3) |
embeddedartists | 0:c828045bbe69 | 792 | wait_ms(delayMs-7); |
embeddedartists | 0:c828045bbe69 | 793 | else |
embeddedartists | 0:c828045bbe69 | 794 | wait_ms(delayMs); |
embeddedartists | 0:c828045bbe69 | 795 | } |
embeddedartists | 0:c828045bbe69 | 796 | } |
embeddedartists | 0:c828045bbe69 | 797 |