gba-x3dtest

diff 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
line diff
     1.1 --- a/src/x3d.c	Wed Jun 25 18:18:05 2014 +0300
     1.2 +++ b/src/x3d.c	Thu Jun 26 06:57:51 2014 +0300
     1.3 @@ -8,6 +8,7 @@
     1.4  #include "logger.h"
     1.5  #include "polyfill.h"
     1.6  #include "gbasys.h"
     1.7 +#include "x3d_impl.h"
     1.8  
     1.9  int dbg_fill_dump;
    1.10  
    1.11 @@ -47,6 +48,11 @@
    1.12  static int32_t im_texcoord[2];
    1.13  static uint8_t im_color_index;
    1.14  
    1.15 +#define MAX_TEXTURES	64
    1.16 +static struct texture textures[MAX_TEXTURES];
    1.17 +static short cur_tex = -1;
    1.18 +
    1.19 +
    1.20  void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz)
    1.21  {
    1.22  	proj_fov = (M_PI_X16 * fov) / 180;
    1.23 @@ -257,7 +263,7 @@
    1.24  
    1.25  		case X3D_TRIANGLES:
    1.26  		case X3D_QUADS:
    1.27 -			draw_poly(pverts, vpos, tex, color);
    1.28 +			draw_poly(pverts, vpos, tex, color, cur_tex >= 0 ? textures + cur_tex : 0);
    1.29  			if(dbg_fill_dump) {
    1.30  				dump_frame(back_buffer);
    1.31  			}
    1.32 @@ -324,6 +330,63 @@
    1.33  	im_color[2] = b;
    1.34  }
    1.35  
    1.36 +static int count_bits(int x)
    1.37 +{
    1.38 +	int i, count = 0;
    1.39 +	for(i=0; i<32; i++) {
    1.40 +		if(x & 1) count++;
    1.41 +		x >>= 1;
    1.42 +	}
    1.43 +	return count;
    1.44 +}
    1.45 +
    1.46 +int x3d_create_texture_rgb(int xsz, int ysz, const uint16_t *pixels)
    1.47 +{
    1.48 +	int i, j;
    1.49 +
    1.50 +	if(xsz == 0 || count_bits(xsz) > 1 || ysz == 0 || count_bits(ysz) > 1) {
    1.51 +		logmsg(LOG_DBG, "%s: texture size (%dx%d) not power of two!\n", __func__, xsz, ysz);
    1.52 +		return -1;
    1.53 +	}
    1.54 +
    1.55 +	for(i=0; i<MAX_TEXTURES; i++) {
    1.56 +		if(!textures[i].pixels) {
    1.57 +			textures[i].pixels = pixels;
    1.58 +			textures[i].xsz = xsz;
    1.59 +			textures[i].ysz = ysz;
    1.60 +			textures[i].umask = xsz - 1;
    1.61 +			textures[i].vmask = ysz - 1;
    1.62 +
    1.63 +			for(j=0; j<32; j++) {
    1.64 +				if((1 << j) == xsz) {
    1.65 +					textures[i].ushift = j;
    1.66 +				}
    1.67 +				if((1 << j) == ysz) {
    1.68 +					textures[i].vshift = j;
    1.69 +				}
    1.70 +			}
    1.71 +
    1.72 +			return i;
    1.73 +		}
    1.74 +	}
    1.75 +	return -1;
    1.76 +}
    1.77 +
    1.78 +void x3d_enable_texture(int texid)
    1.79 +{
    1.80 +	cur_tex = texid;
    1.81 +}
    1.82 +
    1.83 +void x3d_disable_texture(void)
    1.84 +{
    1.85 +	cur_tex = 0;
    1.86 +}
    1.87 +
    1.88 +int x3d_get_active_texture(void)
    1.89 +{
    1.90 +	return cur_tex;
    1.91 +}
    1.92 +
    1.93  static int dump_frame(struct pixel_buffer *frame)
    1.94  {
    1.95  	static int frameno;