deepstone

diff src/scantmpl.h @ 3:0e781cc43178

adding textures
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 10:16:09 +0200
parents f04884489bad
children bce78aaafc68
line diff
     1.1 --- a/src/scantmpl.h	Mon Nov 21 10:15:37 2011 +0200
     1.2 +++ b/src/scantmpl.h	Mon Nov 21 10:16:09 2011 +0200
     1.3 @@ -25,7 +25,10 @@
     1.4  #ifdef INTERP_ENERGY
     1.5  	float e, de, dfde;
     1.6  #endif
     1.7 -	float x, y;
     1.8 +#ifdef INTERP_TEX
     1.9 +	float u, v, du, dv, dfdu, dfdv;
    1.10 +#endif
    1.11 +	float x;
    1.12  	struct vertex *edge;
    1.13  
    1.14  	dy = v1->pos.y - v0->pos.y;
    1.15 @@ -37,7 +40,6 @@
    1.16  	dfdx = dx / dy;
    1.17  
    1.18  #ifdef INTERP_DEPTH
    1.19 -	assert(fb->zbuf);
    1.20  	dz = v1->pos.z - v0->pos.z;
    1.21  	dfdz = dz / dy;
    1.22  #endif
    1.23 @@ -45,6 +47,12 @@
    1.24  	de = v1->energy - v0->energy;
    1.25  	dfde = de / dy;
    1.26  #endif
    1.27 +#ifdef INTERP_TEX
    1.28 +	du = v1->tc.x - v0->tc.x;
    1.29 +	dv = v1->tc.y - v0->tc.y;
    1.30 +	dfdu = du / dy;
    1.31 +	dfdv = dv / dy;
    1.32 +#endif
    1.33  
    1.34  	if(dy < 0.0) {
    1.35  		struct vertex *tmp = v0;
    1.36 @@ -65,6 +73,10 @@
    1.37  #ifdef INTERP_ENERGY
    1.38  	e = v0->energy;
    1.39  #endif
    1.40 +#ifdef INTERP_TEX
    1.41 +	u = v0->tc.x;
    1.42 +	v = v0->tc.y;
    1.43 +#endif
    1.44  	for(i=start; i<end; i++) {
    1.45  		edge[i].pos.x = x;
    1.46  		x += dfdx;
    1.47 @@ -81,13 +93,21 @@
    1.48  #else
    1.49  		edge[i].energy = v0->energy;
    1.50  #endif
    1.51 +
    1.52 +#ifdef INTERP_TEX
    1.53 +		edge[i].tc.x = u;
    1.54 +		edge[i].tc.y = v;
    1.55 +		u += dfdu;
    1.56 +		v += dfdv;
    1.57 +#endif
    1.58  	}
    1.59  }
    1.60  
    1.61  static void SCAN_LINE(int y, unsigned char *sline)
    1.62  {
    1.63 -	int i, x0, x1, len, tmp, cidx;
    1.64 -#if defined(INTERP_DEPTH) || defined(INTERP_ENERGY)
    1.65 +	int x0, x1, len, tmp, cidx;
    1.66 +#if defined(INTERP_DEPTH) || defined(INTERP_ENERGY) || defined(INTERP_TEX)
    1.67 +	int i;
    1.68  	float x, dx;
    1.69  #endif
    1.70  #ifdef INTERP_DEPTH
    1.71 @@ -96,6 +116,10 @@
    1.72  #ifdef INTERP_ENERGY
    1.73  	float e, de, dfde;
    1.74  #endif
    1.75 +#ifdef INTERP_TEX
    1.76 +	int tx, ty;
    1.77 +	float u, v, du, dv, dfdu, dfdv;
    1.78 +#endif
    1.79  	struct vertex *left, *right;
    1.80  
    1.81  	x0 = (int)ROUND(vleft[y].pos.x);
    1.82 @@ -124,7 +148,7 @@
    1.83  	assert(len >= 0);
    1.84  
    1.85  	cidx = left[y].cidx;
    1.86 -#if !defined(INTERP_DEPTH) && !defined(INTERP_ENERGY)
    1.87 +#if !defined(INTERP_DEPTH) && !defined(INTERP_ENERGY) && !defined(INTERP_TEX)
    1.88  	/* no interpolation at all, just memset the whole scanline */
    1.89  	memset(sline + x0, cidx + left[y].energy * st->col_range, len);
    1.90  #else
    1.91 @@ -146,13 +170,36 @@
    1.92  	de = right[y].energy - e;
    1.93  	dfde = de / dx;
    1.94  #endif
    1.95 +#ifdef INTERP_TEX
    1.96 +	u = left[y].tc.x;
    1.97 +	v = left[y].tc.y;
    1.98 +	du = right[y].tc.x - u;
    1.99 +	dv = right[y].tc.y - v;
   1.100 +	dfdu = du / dx;
   1.101 +	dfdv = dv / dx;
   1.102 +#endif
   1.103  
   1.104  	for(i=0; i<len; i++) {
   1.105 +		int c = cidx;
   1.106 +
   1.107 +#ifdef INTERP_DEPTH
   1.108 +		z += dfdz;
   1.109 +#endif
   1.110 +#ifdef INTERP_TEX
   1.111 +		tx = (int)(u * st->tex.width) & st->tex.xmask;
   1.112 +		ty = (int)(v * st->tex.height) & st->tex.ymask;
   1.113 +		c = st->tex.pixels[(ty << st->tex.xshift) + tx];
   1.114 +
   1.115 +		u += dfdu;
   1.116 +		v += dfdv;
   1.117 +#endif
   1.118  #ifdef INTERP_ENERGY
   1.119 -		cidx = left[y].cidx + e * st->col_range;
   1.120 +		c += e * st->col_range;
   1.121  		e += dfde;
   1.122 +#else
   1.123 +		c += left[y].energy * st->col_range;
   1.124  #endif
   1.125 -		sline[x0 + i] = cidx;
   1.126 +		sline[x0 + i] = c;
   1.127  	}
   1.128  #endif	/* flat */
   1.129  }