nds_test2
view src/main.c @ 2:dd8c9847bae9
cube
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 29 Jan 2018 14:40:45 +0200 |
parents | d625ba001a62 |
children |
line source
1 #include <stdint.h>
2 #include <math.h>
3 #include "dsregs.h"
4 #include "ds3.h"
6 static void draw_cube(void);
7 static void xorpat(void *addr, int xsz, int ysz);
9 static void *vram = VRAM_LCDC_PTR;
10 static uint16_t *bgmem = VRAM_BGB_PTR;
12 #define SIN_TAB_SZ 256
13 static int32_t sintab[SIN_TAB_SZ];
14 static int32_t costab[SIN_TAB_SZ];
16 int main(void)
17 {
18 int i;
19 uint32_t frame;
20 int32_t m[16] = {
21 0x10000, 0, 0, 0,
22 0, 0x10000, 0, 0,
23 0, 0, 0x10000, 0,
24 0, 0, 0, 0x10000
25 };
27 REG_POWCNT1 = POWCNT1_LCD | POWCNT1_3DREND | POWCNT1_3DGEOM | POWCNT1_2DA | POWCNT1_2DB | POWCNT1_DSWAP;
29 REG_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG0 | DISPCNT_BG0_3D;
30 REG_B_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG2 | 5;
32 REG_B_BG2CNT = BGXCNT_BM_256X256 | BGXCNT_BM16 | BGXCNT_OVF_WRAP;
33 REG_B_BG2PA = 0x100;
34 REG_B_BG2PB = 0;
35 REG_B_BG2PC = 0;
36 REG_B_BG2PD = 0x100;
38 REG_VRAMCNT_A = VRAM_ENABLE;
39 REG_VRAMCNT_C = VRAM_ENABLE | 4;
41 xorpat(bgmem, 256, 256);
43 for(i=0; i<SIN_TAB_SZ; i++) {
44 float theta = (float)i * M_PI * 2.0 / (float)SIN_TAB_SZ;
45 float s = sin(theta);
46 float c = cos(theta);
47 sintab[i] = (int32_t)(s * 65536.0);
48 costab[i] = (int32_t)(c * 65536.0);
49 }
51 ds3_clear_color(RGB15(4, 4, 4), 31);
52 ds3_clear_depth(0x7fff);
53 ds3_viewport(0, 0, 256, 192);
55 ds3_enable(DS3_POLYGON_SMOOTH);
57 REG_POLYGON_ATTR = 0x001f0080; /* alpha = 31, cull back */
59 ds3_matrix_mode(DS3_PROJECTION);
60 ds3_load_identity();
61 /*ds3_ortho(-87381, 87381, -65536, 65536, -65536, 65536);*/
62 /*ds3_orthof(-1.3333, 1.33333, -1, 1, -1, 1);*/
63 ds3_perspectivef(45, 1.33333, 1.0, 100.0);
65 for(;;) {
66 int idx = frame & 0xff;
67 int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204;
68 int32_t sa = ((sintab[idx] >> 8) * scale) >> 8;
69 int32_t ca = ((costab[idx] >> 8) * scale) >> 8;
70 int32_t x = ca * -128 + sa * -96 + (128 << 8);
71 int32_t y = -sa * -128 + ca * -96 + (96 << 8);
73 ds3_matrix_mode(DS3_MODELVIEW);
74 ds3_load_identity();
75 ds3_translate(0, 0, -0x30000);
77 m[0] = 0x10000;
78 m[2] = m[8] = 0;
79 m[5] = costab[idx]; m[6] = sintab[idx];
80 m[9] = -sintab[idx]; m[10] = costab[idx];
81 ds3_mult_matrix(m);
83 m[5] = 0x10000;
84 m[6] = m[9] = 0;
85 m[0] = costab[idx]; m[2] = -sintab[idx];
86 m[8] = sintab[idx];
87 ds3_mult_matrix(m);
89 draw_cube();
91 ds3_swap_buffers();
92 while(REG_VCOUNT < 192);
94 REG_B_BG2PA = ca;
95 REG_B_BG2PB = sa;
96 REG_B_BG2PC = -sa;
97 REG_B_BG2PD = ca;
98 REG_B_BG2X = x;
99 REG_B_BG2Y = y;
101 ++frame;
102 }
103 return 0;
104 }
106 #define VOFFS 0x8000
107 static void draw_cube(void)
108 {
109 ds3_begin(DS3_QUADS);
110 ds3_color(RGB15(31, 0, 0));
111 ds3_vertex3(-VOFFS, -VOFFS, VOFFS);
112 ds3_vertex3(VOFFS, -VOFFS, VOFFS);
113 ds3_vertex3(VOFFS, VOFFS, VOFFS);
114 ds3_vertex3(-VOFFS, VOFFS, VOFFS);
115 ds3_color(RGB15(0, 31, 0));
116 ds3_vertex3(VOFFS, -VOFFS, VOFFS);
117 ds3_vertex3(VOFFS, -VOFFS, -VOFFS);
118 ds3_vertex3(VOFFS, VOFFS, -VOFFS);
119 ds3_vertex3(VOFFS, VOFFS, VOFFS);
120 ds3_color(RGB15(0, 0, 31));
121 ds3_vertex3(VOFFS, -VOFFS, -VOFFS);
122 ds3_vertex3(-VOFFS, -VOFFS, -VOFFS);
123 ds3_vertex3(-VOFFS, VOFFS, -VOFFS);
124 ds3_vertex3(VOFFS, VOFFS, -VOFFS);
125 ds3_color(RGB15(31, 31, 0));
126 ds3_vertex3(-VOFFS, -VOFFS, -VOFFS);
127 ds3_vertex3(-VOFFS, -VOFFS, VOFFS);
128 ds3_vertex3(-VOFFS, VOFFS, VOFFS);
129 ds3_vertex3(-VOFFS, VOFFS, -VOFFS);
130 ds3_color(RGB15(31, 0, 31));
131 ds3_vertex3(-VOFFS, VOFFS, VOFFS);
132 ds3_vertex3(VOFFS, VOFFS, VOFFS);
133 ds3_vertex3(VOFFS, VOFFS, -VOFFS);
134 ds3_vertex3(-VOFFS, VOFFS, -VOFFS);
135 ds3_color(RGB15(0, 31, 31));
136 ds3_vertex3(VOFFS, -VOFFS, -VOFFS);
137 ds3_vertex3(VOFFS, -VOFFS, VOFFS);
138 ds3_vertex3(-VOFFS, -VOFFS, VOFFS);
139 ds3_vertex3(-VOFFS, -VOFFS, -VOFFS);
140 ds3_end();
141 }
143 static void xorpat(void *addr, int xsz, int ysz)
144 {
145 int i, j;
146 uint16_t *p = addr;
148 for(i=0; i<ysz; i++) {
149 for(j=0; j<xsz; j++) {
150 int xor = i^j;
151 uint16_t red = xor >> 2;
152 uint16_t green = xor >> 1;
153 uint16_t blue = xor;
154 *p++ = 0x8000 | red | ((green & 0x1f) << 5) | ((blue & 0x1f) << 10);
155 }
156 }
157 }