nds_test2

diff src/ds3.c @ 2:dd8c9847bae9

cube
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jan 2018 14:40:45 +0200
parents d625ba001a62
children
line diff
     1.1 --- a/src/ds3.c	Mon Jan 29 03:48:05 2018 +0200
     1.2 +++ b/src/ds3.c	Mon Jan 29 14:40:45 2018 +0200
     1.3 @@ -194,12 +194,13 @@
     1.4  	int32_t dy = top - bottom;
     1.5  	int32_t dz = zfar - znear;
     1.6  
     1.7 -	m[0] = 0x2000000 / (dx << 8);
     1.8 -	m[5] = 0x2000000 / (dy << 8);
     1.9 -	m[10] = -0x2000000 / (dz << 8);
    1.10 -	m[12] = -((right + left) << 8) / (dx << 8);
    1.11 -	m[13] = -((top + bottom) << 8) / (dy << 8);
    1.12 -	m[14] = -((zfar + znear) << 8) / (dz << 8);
    1.13 +	m[0] = x16div(0x20000, dx);
    1.14 +	m[5] = x16div(0x20000, dy);
    1.15 +	m[10] = x16div(-0x20000, dz);
    1.16 +	m[12] = x16div(-(right + left), dx);
    1.17 +	m[13] = x16div(-(top + bottom), dy);
    1.18 +	m[14] = x16div(-(zfar + znear), dz);
    1.19 +	m[15] = 65536;
    1.20  
    1.21  	ds3_mult_matrix(m);
    1.22  }
    1.23 @@ -214,9 +215,10 @@
    1.24  	m[0] = (int32_t)(2.0f / dx * 65536.0f);
    1.25  	m[5] = (int32_t)(2.0f / dy * 65536.0f);
    1.26  	m[10] = (int32_t)(-2.0f / dz * 65536.0f);
    1.27 -	m[12] = (int32_t)(-(right + left) / dx);
    1.28 -	m[13] = (int32_t)(-(top + bottom) / dy);
    1.29 -	m[14] = (int32_t)(-(zfar + znear) / dz);
    1.30 +	m[12] = (int32_t)(-(right + left) / dx * 65536.0f);
    1.31 +	m[13] = (int32_t)(-(top + bottom) / dy * 65536.0f);
    1.32 +	m[14] = (int32_t)(-(zfar + znear) / dz * 65536.0f);
    1.33 +	m[15] = 65536;
    1.34  
    1.35  	ds3_mult_matrix(m);
    1.36  }
    1.37 @@ -229,13 +231,13 @@
    1.38  	int32_t dy = top - bottom;
    1.39  	int32_t dz = zfar - znear;
    1.40  
    1.41 -	int32_t a = ((right + left) << 8) / (dx << 8);
    1.42 -	int32_t b = ((top + bottom) << 8) / (dy << 8);
    1.43 -	int32_t c = (-(zfar + znear) << 8) / (dz << 8);
    1.44 -	int32_t d = -(((zfar >> 8) * znear) << 1) / (dz << 8);
    1.45 +	int32_t a = x16div(right + left, dx);
    1.46 +	int32_t b = x16div(top + bottom, dy);
    1.47 +	int32_t c = x16div(-(zfar + znear), dz);
    1.48 +	int32_t d = x16div(-((zfar >> 8) * znear) << 1, dz);
    1.49  
    1.50 -	m[0] = (znear << 9) / (dx << 8);
    1.51 -	m[5] = (znear << 9) / (dy << 8);
    1.52 +	m[0] = x16div(znear << 1, dx);
    1.53 +	m[5] = x16div(znear << 1, dy);
    1.54  	m[8] = a;
    1.55  	m[9] = b;
    1.56  	m[10] = c;
    1.57 @@ -245,7 +247,7 @@
    1.58  	ds3_mult_matrix(m);
    1.59  }
    1.60  
    1.61 -void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
    1.62 +void g3d_frustumf(float left, float right, float bottom, float top, float nr, float fr)
    1.63  {
    1.64  	int32_t m[16] = {0};
    1.65  
    1.66 @@ -269,7 +271,7 @@
    1.67  	ds3_mult_matrix(m);
    1.68  }
    1.69  
    1.70 -void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar)
    1.71 +void ds3_perspectivef(float vfov_deg, float aspect, float znear, float zfar)
    1.72  {
    1.73  	int32_t m[16] = {0};
    1.74  
    1.75 @@ -285,3 +287,13 @@
    1.76  
    1.77  	ds3_mult_matrix(m);
    1.78  }
    1.79 +
    1.80 +int32_t x16div(int32_t a, int32_t b)
    1.81 +{
    1.82 +	REG_DIVCNT = DIVCNT_64_32;
    1.83 +	REG_DIV_NUMER = (int64_t)a << 16;
    1.84 +	REG_DIV_DENOM = (int64_t)b;
    1.85 +
    1.86 +	while(REG_DIVCNT & DIVCNT_BUSY);
    1.87 +	return (int32_t)REG_DIV_RESULT;
    1.88 +}