|
| 1 | +#include <stdint.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <math.h> |
| 4 | +#include <ti/getcsc.h> |
| 5 | +#include <graphx.h> |
| 6 | +#include <lcddrvce.h> |
| 7 | + |
| 8 | +#define ORG (-50) |
| 9 | +#define DEG (6.0) |
| 10 | +#define ANG (DEG * M_PI / 180.0) |
| 11 | + |
| 12 | +typedef struct { |
| 13 | + float sin_ang; |
| 14 | + float cos_ang; |
| 15 | + float midx1; |
| 16 | + float midy1; |
| 17 | + float midx2; |
| 18 | + float midy2; |
| 19 | +} cube_t; |
| 20 | + |
| 21 | +static float face1[4][2] = { |
| 22 | + { 140, 45 }, |
| 23 | + { 240, 45 }, |
| 24 | + { 240, 145 }, |
| 25 | + { 140, 145 }, |
| 26 | +}; |
| 27 | + |
| 28 | +static float face2[4][2] = { |
| 29 | + { 140 + ORG, 45 - ORG }, |
| 30 | + { 240 + ORG, 45 - ORG }, |
| 31 | + { 240 + ORG, 145 - ORG }, |
| 32 | + { 140 + ORG, 145 - ORG }, |
| 33 | +}; |
| 34 | + |
| 35 | +uint8_t line_color; |
| 36 | + |
| 37 | +void draw_line(uint24_t x0, uint8_t y0, uint24_t x1, uint8_t y1); |
| 38 | + |
| 39 | +void rotate(cube_t *cube); |
| 40 | + |
| 41 | +int main(void) { |
| 42 | + static cube_t cube; |
| 43 | + |
| 44 | + /* Set up the intial cube positions */ |
| 45 | + cube.sin_ang = sin(ANG); |
| 46 | + cube.cos_ang = cos(ANG); |
| 47 | + cube.midx1 = (face1[0][0] + face1[1][0]) / 2; |
| 48 | + cube.midy1 = (face1[1][1] + face1[2][1]) / 2; |
| 49 | + cube.midx2 = (face2[0][0] + face2[1][0]) / 2; |
| 50 | + cube.midy2 = (face2[1][1] + face2[2][1]) / 2; |
| 51 | + |
| 52 | + /* Start the graphics routines */ |
| 53 | + gfx_Begin(); |
| 54 | + gfx_SetDrawBuffer(); |
| 55 | + |
| 56 | + /* Setup Column-Major mode (240x320) */ |
| 57 | + lcd_Init(); |
| 58 | + lcd_SetRamAccessOrder(LCD_MADCTL_DEFAULT ^ LCD_MV); |
| 59 | + lcd_SetColumnAddress(0, GFX_LCD_HEIGHT - 1); |
| 60 | + lcd_SetRowAddress(0, GFX_LCD_WIDTH - 1); |
| 61 | + |
| 62 | + /* Loop until a key is pressed */ |
| 63 | + do { |
| 64 | + /* Rotate the cube */ |
| 65 | + rotate(&cube); |
| 66 | + |
| 67 | + /* Swap the buffer with the screen */ |
| 68 | + gfx_SwapDraw(); |
| 69 | + |
| 70 | + } while (!os_GetCSC()); |
| 71 | + |
| 72 | + /* Restore Row-Major mode (320x240) */ |
| 73 | + lcd_SetRamAccessOrder(LCD_MADCTL_DEFAULT); |
| 74 | + lcd_SetColumnAddress(0, GFX_LCD_WIDTH - 1); |
| 75 | + lcd_SetRowAddress(0, GFX_LCD_HEIGHT - 1); |
| 76 | + lcd_Cleanup(); |
| 77 | + |
| 78 | + /* End graphics drawing */ |
| 79 | + gfx_End(); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +/* Cube rotation */ |
| 85 | +void rotate(cube_t *cube) { |
| 86 | + static uint8_t drawnTimes = 0; |
| 87 | + |
| 88 | + /* Compute the new vertex coordinates */ |
| 89 | + for (uint8_t i = 0; i < 4; ++i) { |
| 90 | + float tmp1, tmp2; |
| 91 | + |
| 92 | + tmp1 = face1[i][0] - cube->midx1; |
| 93 | + tmp2 = face1[i][1] - cube->midy1; |
| 94 | + |
| 95 | + face1[i][0] = cube->midx1 + (tmp1 * cube->cos_ang) - (tmp2 * cube->sin_ang); |
| 96 | + face1[i][1] = cube->midy1 + (tmp1 * cube->sin_ang) + (tmp2 * cube->cos_ang); |
| 97 | + |
| 98 | + tmp1 = face2[i][0] - cube->midx2; |
| 99 | + tmp2 = face2[i][1] - cube->midy2; |
| 100 | + |
| 101 | + face2[i][0] = cube->midx2 + (tmp1 * cube->cos_ang) - (tmp2 * cube->sin_ang); |
| 102 | + face2[i][1] = cube->midy2 + (tmp1 * cube->sin_ang) + (tmp2 * cube->cos_ang); |
| 103 | + } |
| 104 | + |
| 105 | + /* Clear the old cube */ |
| 106 | + gfx_FillScreen(255); |
| 107 | + |
| 108 | + /* Every 200 draws, change the color */ |
| 109 | + if (drawnTimes % 200 == 0) { |
| 110 | + line_color = rand(); |
| 111 | + } |
| 112 | + drawnTimes++; |
| 113 | + |
| 114 | + /* Draw the new cube */ |
| 115 | + for (uint8_t i = 0; i < 4; ++i) { |
| 116 | + uint8_t j = (i + 1) & 3; |
| 117 | + draw_line(face1[i][0], face1[i][1], face1[j][0], face1[j][1]); |
| 118 | + draw_line(face2[i][0], face2[i][1], face2[j][0], face2[j][1]); |
| 119 | + draw_line(face1[i][0], face1[i][1], face2[i][0], face2[i][1]); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/* Bresenham line drawing code */ |
| 124 | + |
| 125 | +static void draw_line0(int x0, int y0, int x1, int y1) { |
| 126 | + int dX = x1 - x0; |
| 127 | + int dY = y1 - y0; |
| 128 | + int y_incr = 1; |
| 129 | + if (dY < 0) { |
| 130 | + y_incr = -1; |
| 131 | + dY = -dY; |
| 132 | + } |
| 133 | + int dD = 2 * dY - dX; |
| 134 | + const int dD_jump = 2 * (dY - dX); |
| 135 | + dY *= 2; |
| 136 | + |
| 137 | + uint8_t* buffer = (uint8_t*)gfx_vbuffer + (x0 * GFX_LCD_HEIGHT) + y0; |
| 138 | + const uint8_t color = line_color; |
| 139 | + |
| 140 | + for (uint24_t x = (uint24_t)dX; x > 0; x--) { |
| 141 | + *buffer = color; |
| 142 | + /* Moves right one pixel */ |
| 143 | + buffer += GFX_LCD_HEIGHT; |
| 144 | + if (dD > 0) { |
| 145 | + buffer += y_incr; |
| 146 | + dD += dD_jump; |
| 147 | + } else { |
| 148 | + dD += dY; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +static void draw_line1(int x0, int y0, int x1, int y1) { |
| 154 | + int dX = x1 - x0; |
| 155 | + int dY = y1 - y0; |
| 156 | + int x_incr = GFX_LCD_HEIGHT; |
| 157 | + if (dX < 0) { |
| 158 | + x_incr = -GFX_LCD_HEIGHT; |
| 159 | + dX = -dX; |
| 160 | + } |
| 161 | + int dD = (2 * dX) - dY; |
| 162 | + const int dD_jump = 2 * (dX - dY); |
| 163 | + dX *= 2; |
| 164 | + |
| 165 | + uint8_t* buffer = (uint8_t*)gfx_vbuffer + (x0 * GFX_LCD_HEIGHT) + y0; |
| 166 | + const uint8_t color = line_color; |
| 167 | + |
| 168 | + for (uint8_t y = (uint8_t)dY; y > 0; y--) { |
| 169 | + *buffer = color; |
| 170 | + /* Moves down one pixel */ |
| 171 | + ++buffer; |
| 172 | + if (dD > 0) { |
| 173 | + buffer += x_incr; |
| 174 | + dD += dD_jump; |
| 175 | + } else { |
| 176 | + dD += dX; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +void draw_line(uint24_t x0, uint8_t y0, uint24_t x1, uint8_t y1) { |
| 182 | + if (abs((int)y1 - (int)y0) < abs((int)x1 - (int)x0)) { |
| 183 | + if (x0 > x1) { |
| 184 | + draw_line0(x1, y1, x0, y0); |
| 185 | + } else { |
| 186 | + draw_line0(x0, y0, x1, y1); |
| 187 | + } |
| 188 | + } else { |
| 189 | + if (y0 > y1) { |
| 190 | + draw_line1(x1, y1, x0, y0); |
| 191 | + } else { |
| 192 | + draw_line1(x0, y0, x1, y1); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments