gba-x3dtest

view src/x3d.c @ 12:ecc022a21279

more tuff
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jun 2014 06:44:04 +0300
parents b0ed38f13261
children 2070a81127f2
line source
1 #include "config.h"
2 #include <string.h>
3 #include <math.h>
4 #include "x3d.h"
5 #include "fixed.h"
6 #include "sincos.h"
7 #include "logger.h"
8 #include "polyfill.h"
9 #include "gbasys.h"
11 #define MAT_STACK_SIZE 4
13 struct matrix {
14 int32_t m[12];
15 };
17 static void proc_vertex(const int32_t *vin, const int32_t *cin, pvec3 *vout, pvec3 *cout);
20 static int32_t proj_fov = M_PI_X16;
21 static int32_t proj_aspect = 65536;
22 static int32_t proj_near = ftox16(0.5);
23 static int32_t proj_far = 500 << 16;
24 static int32_t tan_half_xfov, tan_half_yfov;
26 #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0}
28 static struct matrix identity = { ID_INIT };
30 static short mtop;
31 static struct matrix mstack[MAT_STACK_SIZE] = { {ID_INIT}, {ID_INIT} };
33 static const int32_t *vertex_array;
34 static unsigned short vertex_count;
35 static const int32_t *color_array;
36 static unsigned short color_count;
38 static int32_t im_color[3];
39 static uint8_t im_color_index;
41 void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz)
42 {
43 proj_fov = (M_PI_X16 * fov) / 180;
44 proj_aspect = aspect;
45 proj_near = nearz;
46 proj_far = farz;
48 tan_half_yfov = (int32_t)(tan(0.5 * proj_fov / 65536.0) * 65536.0);
49 tan_half_xfov = x16mul(tan_half_yfov, aspect);
50 }
52 int x3d_push_matrix(void)
53 {
54 short newtop = mtop + 1;
55 if(newtop >= MAT_STACK_SIZE) {
56 return -1;
57 }
58 memcpy(mstack + newtop, mstack + mtop, sizeof *mstack);
59 mtop = newtop;
60 return 0;
61 }
63 int x3d_pop_matrix(void)
64 {
65 if(mtop <= 0) {
66 return -1;
67 }
68 --mtop;
69 return 0;
70 }
72 void x3d_load_matrix(int32_t *m)
73 {
74 memcpy(mstack[mtop].m, m, sizeof *mstack);
75 }
78 #define M(i,j) (((i) << 2) + (j))
79 void x3d_mult_matrix(int32_t *m)
80 {
81 int i, j;
82 struct matrix tmp;
84 memcpy(tmp.m, mstack[mtop].m, sizeof tmp);
86 for(i=0; i<3; i++) {
87 for(j=0; j<4; j++) {
88 mstack[mtop].m[M(i, j)] =
89 x16mul(tmp.m[M(0, j)], m[M(i, 0)]) +
90 x16mul(tmp.m[M(1, j)], m[M(i, 1)]) +
91 x16mul(tmp.m[M(2, j)], m[M(i, 2)]);
92 }
93 mstack[mtop].m[M(i, 3)] += m[M(i, 3)];
94 }
95 }
97 void x3d_load_identity(void)
98 {
99 memcpy(mstack[mtop].m, identity.m, sizeof identity);
100 }
102 void x3d_translate(int32_t x, int32_t y, int32_t z)
103 {
104 int32_t m[] = ID_INIT;
105 m[3] = x;
106 m[7] = y;
107 m[11] = z;
109 x3d_mult_matrix(m);
110 }
112 void x3d_rotate(int32_t deg, int32_t x, int32_t y, int32_t z)
113 {
114 int32_t xform[] = ID_INIT;
116 int32_t angle = x16mul(M_PI_X16, deg) / 180;
117 int32_t sina = sin_x16(angle);
118 int32_t cosa = cos_x16(angle);
119 int32_t one_minus_cosa = 65536 - cosa;
120 int32_t nxsq = x16sq(x);
121 int32_t nysq = x16sq(y);
122 int32_t nzsq = x16sq(z);
124 xform[0] = nxsq + x16mul(65536 - nxsq, cosa);
125 xform[4] = x16mul(x16mul(x, y), one_minus_cosa) - x16mul(z, sina);
126 xform[8] = x16mul(x16mul(x, z), one_minus_cosa) + x16mul(y, sina);
127 xform[1] = x16mul(x16mul(x, y), one_minus_cosa) + x16mul(z, sina);
128 xform[5] = nysq + x16mul(65536 - nysq, cosa);
129 xform[9] = x16mul(x16mul(y, z), one_minus_cosa) - x16mul(x, sina);
130 xform[2] = x16mul(x16mul(x, z), one_minus_cosa) - x16mul(y, sina);
131 xform[6] = x16mul(x16mul(y, z), one_minus_cosa) + x16mul(x, sina);
132 xform[10] = nzsq + x16mul(65536 - nzsq, cosa);
134 x3d_mult_matrix(xform);
135 }
137 void x3d_scale(int32_t x, int32_t y, int32_t z)
138 {
139 int32_t m[] = ID_INIT;
141 m[0] = x;
142 m[5] = y;
143 m[10] = z;
145 x3d_mult_matrix(m);
146 }
148 void x3d_vertex_array(int count, const int32_t *ptr)
149 {
150 vertex_array = ptr;
151 vertex_count = count;
152 }
154 void x3d_color_array(int count, const int32_t *ptr)
155 {
156 color_array = ptr;
157 color_count = count;
158 }
160 int x3d_draw(int prim, int vnum)
161 {
162 int i, j, pverts = prim;
163 const int32_t *vptr = vertex_array;
164 const int32_t *cptr = color_array;
165 #ifndef PALMODE
166 short cr, cg, cb;
167 #endif
168 uint16_t color;
170 if(!vertex_array) return -1;
172 if(vnum > vertex_count) {
173 logmsg(LOG_DBG, "%s called with vnum=%d, but current vertex array has %d vertices\n",
174 __FUNCTION__, vnum, vertex_count);
175 vnum = vertex_count;
176 }
177 if(color_array && vnum > color_count) {
178 logmsg(LOG_DBG, "%s called with vnum=%d, but current color array has %d elements\n",
179 __FUNCTION__, vnum, color_count);
180 vnum = color_count;
181 }
183 for(i=0; i<vnum; i+=pverts) {
184 /* process vertices */
185 pvec3 vpos[4];
186 pvec3 col[4];
188 for(j=0; j<pverts; j++) {
189 proc_vertex(vptr, cptr, vpos + j, col + j);
191 if(vpos[j].z <= proj_near) {
192 goto skip_prim;
193 }
195 vptr += 3;
196 if(cptr) cptr += 3;
197 }
199 #ifdef PALMODE
200 color = im_color_index;
201 #else
202 cr = col[0].x >> 8;
203 cg = col[0].y >> 8;
204 cb = col[0].z >> 8;
206 if(cr > 255) cr = 255;
207 if(cg > 255) cg = 255;
208 if(cb > 255) cb = 255;
210 color = RGB(cr, cg, cb);
211 #endif
213 /* project & viewport */
214 for(j=0; j<pverts; j++) {
215 int32_t x, y;
217 x = x16mul(vpos[j].x, tan_half_xfov);
218 x = x16div(x, vpos[j].z);
219 vpos[j].x = (x + 65536) * (WIDTH / 2);
221 y = x16mul(vpos[j].y, tan_half_yfov);
222 y = x16div(y, vpos[j].z);
223 vpos[j].y = (65536 - y) * (HEIGHT / 2);
224 }
226 switch(pverts) {
227 case X3D_POINTS:
228 draw_point(vpos, color);
229 break;
231 case X3D_LINES:
232 break;
234 case X3D_TRIANGLES:
235 case X3D_QUADS:
236 draw_poly(pverts, vpos, color);
237 break;
238 }
239 skip_prim: ;
240 }
241 return 0;
242 }
244 static void proc_vertex(const int32_t *vin, const int32_t *cin, pvec3 *vout, pvec3 *cout)
245 {
246 int i;
247 int32_t tvert[3];
248 int32_t *mvmat = mstack[mtop].m;
250 /* transform vertex with current matrix */
251 for(i=0; i<3; i++) {
252 tvert[i] = x16mul(mvmat[0], vin[0]) +
253 x16mul(mvmat[1], vin[1]) +
254 x16mul(mvmat[2], vin[2]) +
255 mvmat[3];
256 mvmat += 4;
257 }
259 vout->x = tvert[0];
260 vout->y = tvert[1];
261 vout->z = tvert[2];
262 /*logmsg(LOG_DBG, "%s: (%g %g %g) -> (%g %g %g)\n", __FUNCTION__,
263 x16tof(vin[0]), x16tof(vin[1]), x16tof(vin[2]),
264 x16tof(vout->x), x16tof(vout->y), x16tof(vout->z));*/
266 if(color_array) {
267 cout->x = cin[0];
268 cout->y = cin[1];
269 cout->z = cin[2];
270 } else {
271 cout->x = im_color[0];
272 cout->y = im_color[1];
273 cout->z = im_color[2];
274 }
275 }
277 void x3d_color_index(int cidx)
278 {
279 im_color_index = cidx;
280 }
282 void x3d_color(int32_t r, int32_t g, int32_t b)
283 {
284 im_color[0] = r;
285 im_color[1] = g;
286 im_color[2] = b;
287 }