gba-x3dtest

annotate src/x3d.c @ 17:0a7f402892b3

texture mapping
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Jun 2014 06:57:51 +0300
parents b755fb002f17
children 62390f9cc93e
rev   line source
nuclear@9 1 #include "config.h"
nuclear@14 2 #include <stdio.h>
nuclear@7 3 #include <string.h>
nuclear@12 4 #include <math.h>
nuclear@7 5 #include "x3d.h"
nuclear@7 6 #include "fixed.h"
nuclear@7 7 #include "sincos.h"
nuclear@8 8 #include "logger.h"
nuclear@8 9 #include "polyfill.h"
nuclear@8 10 #include "gbasys.h"
nuclear@17 11 #include "x3d_impl.h"
nuclear@7 12
nuclear@14 13 int dbg_fill_dump;
nuclear@14 14
nuclear@7 15 #define MAT_STACK_SIZE 4
nuclear@7 16
nuclear@7 17 struct matrix {
nuclear@8 18 int32_t m[12];
nuclear@7 19 };
nuclear@7 20
nuclear@15 21 static void proc_vertex(const int32_t *vin, const int32_t *cin, const int32_t *tin,
nuclear@15 22 pvec3 *vout, pvec3 *cout, pvec2 *tout);
nuclear@14 23 static int dump_frame(struct pixel_buffer *frame);
nuclear@8 24
nuclear@8 25
nuclear@7 26 static int32_t proj_fov = M_PI_X16;
nuclear@7 27 static int32_t proj_aspect = 65536;
nuclear@13 28 static int32_t inv_proj_aspect = 65536;
nuclear@7 29 static int32_t proj_near = ftox16(0.5);
nuclear@7 30 static int32_t proj_far = 500 << 16;
nuclear@13 31 static int32_t inv_tan_half_xfov, inv_tan_half_yfov;
nuclear@7 32
nuclear@8 33 #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0}
nuclear@8 34
nuclear@8 35 static struct matrix identity = { ID_INIT };
nuclear@7 36
nuclear@7 37 static short mtop;
nuclear@8 38 static struct matrix mstack[MAT_STACK_SIZE] = { {ID_INIT}, {ID_INIT} };
nuclear@8 39
nuclear@8 40 static const int32_t *vertex_array;
nuclear@8 41 static unsigned short vertex_count;
nuclear@8 42 static const int32_t *color_array;
nuclear@8 43 static unsigned short color_count;
nuclear@15 44 static const int32_t *texcoord_array;
nuclear@15 45 static unsigned short texcoord_count;
nuclear@8 46
nuclear@8 47 static int32_t im_color[3];
nuclear@15 48 static int32_t im_texcoord[2];
nuclear@9 49 static uint8_t im_color_index;
nuclear@7 50
nuclear@17 51 #define MAX_TEXTURES 64
nuclear@17 52 static struct texture textures[MAX_TEXTURES];
nuclear@17 53 static short cur_tex = -1;
nuclear@17 54
nuclear@17 55
nuclear@12 56 void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz)
nuclear@7 57 {
nuclear@12 58 proj_fov = (M_PI_X16 * fov) / 180;
nuclear@7 59 proj_aspect = aspect;
nuclear@13 60 inv_proj_aspect = x16div(65536, proj_aspect);
nuclear@7 61 proj_near = nearz;
nuclear@7 62 proj_far = farz;
nuclear@12 63
nuclear@13 64 inv_tan_half_yfov = (int32_t)(65536.0 / tan(0.5 * proj_fov / 65536.0));
nuclear@13 65 inv_tan_half_xfov = x16mul(inv_tan_half_yfov, aspect);
nuclear@7 66 }
nuclear@7 67
nuclear@7 68 int x3d_push_matrix(void)
nuclear@7 69 {
nuclear@7 70 short newtop = mtop + 1;
nuclear@7 71 if(newtop >= MAT_STACK_SIZE) {
nuclear@7 72 return -1;
nuclear@7 73 }
nuclear@7 74 memcpy(mstack + newtop, mstack + mtop, sizeof *mstack);
nuclear@7 75 mtop = newtop;
nuclear@7 76 return 0;
nuclear@7 77 }
nuclear@7 78
nuclear@7 79 int x3d_pop_matrix(void)
nuclear@7 80 {
nuclear@7 81 if(mtop <= 0) {
nuclear@7 82 return -1;
nuclear@7 83 }
nuclear@7 84 --mtop;
nuclear@7 85 return 0;
nuclear@7 86 }
nuclear@7 87
nuclear@7 88 void x3d_load_matrix(int32_t *m)
nuclear@7 89 {
nuclear@8 90 memcpy(mstack[mtop].m, m, sizeof *mstack);
nuclear@7 91 }
nuclear@7 92
nuclear@7 93
nuclear@7 94 #define M(i,j) (((i) << 2) + (j))
nuclear@7 95 void x3d_mult_matrix(int32_t *m)
nuclear@7 96 {
nuclear@7 97 int i, j;
nuclear@7 98 struct matrix tmp;
nuclear@7 99
nuclear@8 100 memcpy(tmp.m, mstack[mtop].m, sizeof tmp);
nuclear@7 101
nuclear@7 102 for(i=0; i<3; i++) {
nuclear@7 103 for(j=0; j<4; j++) {
nuclear@8 104 mstack[mtop].m[M(i, j)] =
nuclear@14 105 x16mul(m[M(0, j)], tmp.m[M(i, 0)]) +
nuclear@14 106 x16mul(m[M(1, j)], tmp.m[M(i, 1)]) +
nuclear@14 107 x16mul(m[M(2, j)], tmp.m[M(i, 2)]);
nuclear@7 108 }
nuclear@14 109 mstack[mtop].m[M(i, 3)] += tmp.m[M(i, 3)];
nuclear@7 110 }
nuclear@7 111 }
nuclear@7 112
nuclear@7 113 void x3d_load_identity(void)
nuclear@7 114 {
nuclear@8 115 memcpy(mstack[mtop].m, identity.m, sizeof identity);
nuclear@7 116 }
nuclear@7 117
nuclear@8 118 void x3d_translate(int32_t x, int32_t y, int32_t z)
nuclear@8 119 {
nuclear@8 120 int32_t m[] = ID_INIT;
nuclear@8 121 m[3] = x;
nuclear@8 122 m[7] = y;
nuclear@8 123 m[11] = z;
nuclear@8 124
nuclear@8 125 x3d_mult_matrix(m);
nuclear@8 126 }
nuclear@8 127
nuclear@8 128 void x3d_rotate(int32_t deg, int32_t x, int32_t y, int32_t z)
nuclear@8 129 {
nuclear@8 130 int32_t xform[] = ID_INIT;
nuclear@8 131
nuclear@8 132 int32_t angle = x16mul(M_PI_X16, deg) / 180;
nuclear@8 133 int32_t sina = sin_x16(angle);
nuclear@8 134 int32_t cosa = cos_x16(angle);
nuclear@8 135 int32_t one_minus_cosa = 65536 - cosa;
nuclear@8 136 int32_t nxsq = x16sq(x);
nuclear@8 137 int32_t nysq = x16sq(y);
nuclear@8 138 int32_t nzsq = x16sq(z);
nuclear@8 139
nuclear@8 140 xform[0] = nxsq + x16mul(65536 - nxsq, cosa);
nuclear@8 141 xform[4] = x16mul(x16mul(x, y), one_minus_cosa) - x16mul(z, sina);
nuclear@8 142 xform[8] = x16mul(x16mul(x, z), one_minus_cosa) + x16mul(y, sina);
nuclear@8 143 xform[1] = x16mul(x16mul(x, y), one_minus_cosa) + x16mul(z, sina);
nuclear@8 144 xform[5] = nysq + x16mul(65536 - nysq, cosa);
nuclear@8 145 xform[9] = x16mul(x16mul(y, z), one_minus_cosa) - x16mul(x, sina);
nuclear@8 146 xform[2] = x16mul(x16mul(x, z), one_minus_cosa) - x16mul(y, sina);
nuclear@8 147 xform[6] = x16mul(x16mul(y, z), one_minus_cosa) + x16mul(x, sina);
nuclear@8 148 xform[10] = nzsq + x16mul(65536 - nzsq, cosa);
nuclear@8 149
nuclear@8 150 x3d_mult_matrix(xform);
nuclear@8 151 }
nuclear@8 152
nuclear@8 153 void x3d_scale(int32_t x, int32_t y, int32_t z)
nuclear@8 154 {
nuclear@8 155 int32_t m[] = ID_INIT;
nuclear@8 156
nuclear@8 157 m[0] = x;
nuclear@8 158 m[5] = y;
nuclear@8 159 m[10] = z;
nuclear@8 160
nuclear@8 161 x3d_mult_matrix(m);
nuclear@8 162 }
nuclear@8 163
nuclear@8 164 void x3d_vertex_array(int count, const int32_t *ptr)
nuclear@8 165 {
nuclear@8 166 vertex_array = ptr;
nuclear@8 167 vertex_count = count;
nuclear@8 168 }
nuclear@8 169
nuclear@8 170 void x3d_color_array(int count, const int32_t *ptr)
nuclear@8 171 {
nuclear@8 172 color_array = ptr;
nuclear@8 173 color_count = count;
nuclear@8 174 }
nuclear@8 175
nuclear@15 176 void x3d_texcoord_array(int count, const int32_t *ptr)
nuclear@15 177 {
nuclear@15 178 texcoord_array = ptr;
nuclear@15 179 texcoord_count = count;
nuclear@15 180 }
nuclear@15 181
nuclear@12 182 int x3d_draw(int prim, int vnum)
nuclear@8 183 {
nuclear@8 184 int i, j, pverts = prim;
nuclear@8 185 const int32_t *vptr = vertex_array;
nuclear@8 186 const int32_t *cptr = color_array;
nuclear@15 187 const int32_t *tptr = texcoord_array;
nuclear@9 188 #ifndef PALMODE
nuclear@8 189 short cr, cg, cb;
nuclear@9 190 #endif
nuclear@9 191 uint16_t color;
nuclear@8 192
nuclear@8 193 if(!vertex_array) return -1;
nuclear@8 194
nuclear@8 195 if(vnum > vertex_count) {
nuclear@8 196 logmsg(LOG_DBG, "%s called with vnum=%d, but current vertex array has %d vertices\n",
nuclear@8 197 __FUNCTION__, vnum, vertex_count);
nuclear@8 198 vnum = vertex_count;
nuclear@8 199 }
nuclear@8 200 if(color_array && vnum > color_count) {
nuclear@8 201 logmsg(LOG_DBG, "%s called with vnum=%d, but current color array has %d elements\n",
nuclear@8 202 __FUNCTION__, vnum, color_count);
nuclear@8 203 vnum = color_count;
nuclear@8 204 }
nuclear@15 205 if(texcoord_array && vnum > texcoord_count) {
nuclear@15 206 logmsg(LOG_DBG, "%s called with vnum=%d, but current texcoord array has %d elements\n",
nuclear@15 207 __FUNCTION__, vnum, texcoord_count);
nuclear@15 208 vnum = texcoord_count;
nuclear@15 209 }
nuclear@8 210
nuclear@8 211 for(i=0; i<vnum; i+=pverts) {
nuclear@8 212 /* process vertices */
nuclear@8 213 pvec3 vpos[4];
nuclear@8 214 pvec3 col[4];
nuclear@15 215 pvec2 tex[4];
nuclear@8 216
nuclear@8 217 for(j=0; j<pverts; j++) {
nuclear@15 218 proc_vertex(vptr, cptr, tptr, vpos + j, col + j, tex + j);
nuclear@12 219
nuclear@12 220 if(vpos[j].z <= proj_near) {
nuclear@12 221 goto skip_prim;
nuclear@12 222 }
nuclear@12 223
nuclear@8 224 vptr += 3;
nuclear@8 225 if(cptr) cptr += 3;
nuclear@15 226 if(tptr) tptr += 2;
nuclear@8 227 }
nuclear@8 228
nuclear@9 229 #ifdef PALMODE
nuclear@9 230 color = im_color_index;
nuclear@9 231 #else
nuclear@8 232 cr = col[0].x >> 8;
nuclear@8 233 cg = col[0].y >> 8;
nuclear@8 234 cb = col[0].z >> 8;
nuclear@8 235
nuclear@8 236 if(cr > 255) cr = 255;
nuclear@8 237 if(cg > 255) cg = 255;
nuclear@8 238 if(cb > 255) cb = 255;
nuclear@8 239
nuclear@9 240 color = RGB(cr, cg, cb);
nuclear@9 241 #endif
nuclear@9 242
nuclear@12 243 /* project & viewport */
nuclear@12 244 for(j=0; j<pverts; j++) {
nuclear@12 245 int32_t x, y;
nuclear@12 246
nuclear@13 247 x = x16mul(vpos[j].x, inv_tan_half_xfov);
nuclear@12 248 x = x16div(x, vpos[j].z);
nuclear@13 249 vpos[j].x = (x16mul(x, inv_proj_aspect) + 65536) * (WIDTH / 2);
nuclear@12 250
nuclear@13 251 y = x16mul(vpos[j].y, inv_tan_half_yfov);
nuclear@12 252 y = x16div(y, vpos[j].z);
nuclear@12 253 vpos[j].y = (65536 - y) * (HEIGHT / 2);
nuclear@12 254 }
nuclear@12 255
nuclear@8 256 switch(pverts) {
nuclear@8 257 case X3D_POINTS:
nuclear@9 258 draw_point(vpos, color);
nuclear@8 259 break;
nuclear@8 260
nuclear@8 261 case X3D_LINES:
nuclear@8 262 break;
nuclear@8 263
nuclear@8 264 case X3D_TRIANGLES:
nuclear@8 265 case X3D_QUADS:
nuclear@17 266 draw_poly(pverts, vpos, tex, color, cur_tex >= 0 ? textures + cur_tex : 0);
nuclear@14 267 if(dbg_fill_dump) {
nuclear@14 268 dump_frame(back_buffer);
nuclear@14 269 }
nuclear@8 270 break;
nuclear@8 271 }
nuclear@12 272 skip_prim: ;
nuclear@8 273 }
nuclear@14 274
nuclear@14 275 dbg_fill_dump = 0;
nuclear@8 276 return 0;
nuclear@8 277 }
nuclear@8 278
nuclear@15 279 static void proc_vertex(const int32_t *vin, const int32_t *cin, const int32_t *tin,
nuclear@15 280 pvec3 *vout, pvec3 *cout, pvec2 *tout)
nuclear@8 281 {
nuclear@8 282 int i;
nuclear@8 283 int32_t tvert[3];
nuclear@8 284 int32_t *mvmat = mstack[mtop].m;
nuclear@8 285
nuclear@8 286 /* transform vertex with current matrix */
nuclear@8 287 for(i=0; i<3; i++) {
nuclear@8 288 tvert[i] = x16mul(mvmat[0], vin[0]) +
nuclear@8 289 x16mul(mvmat[1], vin[1]) +
nuclear@8 290 x16mul(mvmat[2], vin[2]) +
nuclear@8 291 mvmat[3];
nuclear@8 292 mvmat += 4;
nuclear@8 293 }
nuclear@8 294
nuclear@8 295 vout->x = tvert[0];
nuclear@8 296 vout->y = tvert[1];
nuclear@8 297 vout->z = tvert[2];
nuclear@8 298 /*logmsg(LOG_DBG, "%s: (%g %g %g) -> (%g %g %g)\n", __FUNCTION__,
nuclear@8 299 x16tof(vin[0]), x16tof(vin[1]), x16tof(vin[2]),
nuclear@8 300 x16tof(vout->x), x16tof(vout->y), x16tof(vout->z));*/
nuclear@8 301
nuclear@8 302 if(color_array) {
nuclear@8 303 cout->x = cin[0];
nuclear@8 304 cout->y = cin[1];
nuclear@8 305 cout->z = cin[2];
nuclear@8 306 } else {
nuclear@8 307 cout->x = im_color[0];
nuclear@8 308 cout->y = im_color[1];
nuclear@8 309 cout->z = im_color[2];
nuclear@8 310 }
nuclear@15 311
nuclear@15 312 if(texcoord_array) {
nuclear@15 313 tout->x = tin[0];
nuclear@15 314 tout->y = tin[1];
nuclear@15 315 } else {
nuclear@15 316 tout->x = im_texcoord[0];
nuclear@15 317 tout->y = im_texcoord[1];
nuclear@15 318 }
nuclear@8 319 }
nuclear@8 320
nuclear@9 321 void x3d_color_index(int cidx)
nuclear@9 322 {
nuclear@9 323 im_color_index = cidx;
nuclear@9 324 }
nuclear@9 325
nuclear@8 326 void x3d_color(int32_t r, int32_t g, int32_t b)
nuclear@8 327 {
nuclear@8 328 im_color[0] = r;
nuclear@8 329 im_color[1] = g;
nuclear@8 330 im_color[2] = b;
nuclear@8 331 }
nuclear@14 332
nuclear@17 333 static int count_bits(int x)
nuclear@17 334 {
nuclear@17 335 int i, count = 0;
nuclear@17 336 for(i=0; i<32; i++) {
nuclear@17 337 if(x & 1) count++;
nuclear@17 338 x >>= 1;
nuclear@17 339 }
nuclear@17 340 return count;
nuclear@17 341 }
nuclear@17 342
nuclear@17 343 int x3d_create_texture_rgb(int xsz, int ysz, const uint16_t *pixels)
nuclear@17 344 {
nuclear@17 345 int i, j;
nuclear@17 346
nuclear@17 347 if(xsz == 0 || count_bits(xsz) > 1 || ysz == 0 || count_bits(ysz) > 1) {
nuclear@17 348 logmsg(LOG_DBG, "%s: texture size (%dx%d) not power of two!\n", __func__, xsz, ysz);
nuclear@17 349 return -1;
nuclear@17 350 }
nuclear@17 351
nuclear@17 352 for(i=0; i<MAX_TEXTURES; i++) {
nuclear@17 353 if(!textures[i].pixels) {
nuclear@17 354 textures[i].pixels = pixels;
nuclear@17 355 textures[i].xsz = xsz;
nuclear@17 356 textures[i].ysz = ysz;
nuclear@17 357 textures[i].umask = xsz - 1;
nuclear@17 358 textures[i].vmask = ysz - 1;
nuclear@17 359
nuclear@17 360 for(j=0; j<32; j++) {
nuclear@17 361 if((1 << j) == xsz) {
nuclear@17 362 textures[i].ushift = j;
nuclear@17 363 }
nuclear@17 364 if((1 << j) == ysz) {
nuclear@17 365 textures[i].vshift = j;
nuclear@17 366 }
nuclear@17 367 }
nuclear@17 368
nuclear@17 369 return i;
nuclear@17 370 }
nuclear@17 371 }
nuclear@17 372 return -1;
nuclear@17 373 }
nuclear@17 374
nuclear@17 375 void x3d_enable_texture(int texid)
nuclear@17 376 {
nuclear@17 377 cur_tex = texid;
nuclear@17 378 }
nuclear@17 379
nuclear@17 380 void x3d_disable_texture(void)
nuclear@17 381 {
nuclear@17 382 cur_tex = 0;
nuclear@17 383 }
nuclear@17 384
nuclear@17 385 int x3d_get_active_texture(void)
nuclear@17 386 {
nuclear@17 387 return cur_tex;
nuclear@17 388 }
nuclear@17 389
nuclear@14 390 static int dump_frame(struct pixel_buffer *frame)
nuclear@14 391 {
nuclear@14 392 static int frameno;
nuclear@14 393 char buf[128];
nuclear@14 394 FILE *fp;
nuclear@14 395 int i, npix;
nuclear@14 396 uint16_t *ptr = frame->pixels;
nuclear@14 397
nuclear@14 398 sprintf(buf, "dump%03d.ppm", ++frameno);
nuclear@14 399
nuclear@14 400 if(!(fp = fopen(buf, "wb"))) {
nuclear@14 401 fprintf(stderr, "failed to dump file: %s\n", buf);
nuclear@14 402 return -1;
nuclear@14 403 }
nuclear@14 404
nuclear@14 405 fprintf(fp, "P6\n%d %d\n255\n", frame->x, frame->y);
nuclear@14 406
nuclear@14 407 npix = frame->x * frame->y;
nuclear@14 408 for(i=0; i<npix; i++) {
nuclear@14 409 uint16_t pixel = *ptr++;
nuclear@14 410 fputc(GET_R(pixel), fp);
nuclear@14 411 fputc(GET_G(pixel), fp);
nuclear@14 412 fputc(GET_B(pixel), fp);
nuclear@14 413 }
nuclear@14 414 fclose(fp);
nuclear@14 415 return 0;
nuclear@14 416 }