nds_test2
changeset 1:d625ba001a62
more stuff in ds3
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 29 Jan 2018 03:48:05 +0200 |
parents | abcaf667f2bd |
children | dd8c9847bae9 |
files | src/ds3.c src/ds3.h src/main.c |
diffstat | 3 files changed, 208 insertions(+), 14 deletions(-) [+] |
line diff
1.1 --- a/src/ds3.c Sun Jan 28 20:05:26 2018 +0200 1.2 +++ b/src/ds3.c Mon Jan 29 03:48:05 2018 +0200 1.3 @@ -1,4 +1,5 @@ 1.4 #include <stdint.h> 1.5 +#include <math.h> 1.6 #include "dsregs.h" 1.7 #include "ds3.h" 1.8 1.9 @@ -52,6 +53,42 @@ 1.10 } 1.11 } 1.12 1.13 +void ds3_load_matrix4x3(int32_t *m) 1.14 +{ 1.15 + int i; 1.16 + for(i=0; i<12; i++) { 1.17 + int16_t val = (int16_t)(*m++ >> 4); 1.18 + REG_MTX_LOAD_4X3 = val; 1.19 + } 1.20 +} 1.21 + 1.22 +void ds3_mult_matrix(int32_t *m) 1.23 +{ 1.24 + int i; 1.25 + for(i=0; i<16; i++) { 1.26 + int16_t val = (int16_t)(*m++ >> 4); 1.27 + REG_MTX_MULT_4X4 = val; 1.28 + } 1.29 +} 1.30 + 1.31 +void ds3_mult_matrix4x3(int32_t *m) 1.32 +{ 1.33 + int i; 1.34 + for(i=0; i<12; i++) { 1.35 + int16_t val = (int16_t)(*m++ >> 4); 1.36 + REG_MTX_MULT_4X3 = val; 1.37 + } 1.38 +} 1.39 + 1.40 +void ds3_mult_matrix3x3(int32_t *m) 1.41 +{ 1.42 + int i; 1.43 + for(i=0; i<9; i++) { 1.44 + int16_t val = (int16_t)(*m++ >> 4); 1.45 + REG_MTX_MULT_3X3 = val; 1.46 + } 1.47 +} 1.48 + 1.49 void ds3_push_matrix(void) 1.50 { 1.51 REG_MTX_PUSH = 0; 1.52 @@ -97,6 +134,21 @@ 1.53 REG_VTX_16 = (z >> 4) & 0xffff; 1.54 } 1.55 1.56 +void ds3_vertex3f(float x, float y, float z) 1.57 +{ 1.58 + ds3_vertex3((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f), (int32_t)(z * 65536.0f)); 1.59 +} 1.60 + 1.61 +void ds3_vertex2(int32_t x, int32_t y) 1.62 +{ 1.63 + REG_VTX_XY = ((x >> 4) & 0xffff) | ((y << 12) & 0xffff0000); 1.64 +} 1.65 + 1.66 +void ds3_vertex2f(float x, float y) 1.67 +{ 1.68 + ds3_vertex2((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f)); 1.69 +} 1.70 + 1.71 void ds3_color(uint16_t color) 1.72 { 1.73 REG_COLOR = color; 1.74 @@ -106,3 +158,130 @@ 1.75 { 1.76 REG_COLOR = RGB15(r >> 3, g >> 3, b >> 3); 1.77 } 1.78 + 1.79 +void ds3_color3f(float r, float g, float b) 1.80 +{ 1.81 + uint16_t ir = r * 31.0f; 1.82 + uint16_t ig = g * 31.0f; 1.83 + uint16_t ib = b * 31.0f; 1.84 + REG_COLOR = RGB15(ir, ig, ib); 1.85 +} 1.86 + 1.87 +void ds3_normal(int32_t x, int32_t y, int32_t z) 1.88 +{ 1.89 + REG_NORMAL = ((x >> 7) & 0x3ff) | ((y << 17) & 0xffc00) | ((z << 27) & 0x3ff00000); 1.90 +} 1.91 + 1.92 +void ds3_normal3f(float x, float y, float z) 1.93 +{ 1.94 + ds3_normal((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f), (int32_t)(z * 65536.0f)); 1.95 +} 1.96 + 1.97 +void ds3_texcoord2(int32_t s, int32_t t) 1.98 +{ 1.99 + REG_TEXCOORD = (((s) >> 12) & 0xffff) | (((t) << 4) & 0xffff0000); 1.100 +} 1.101 + 1.102 +void ds3_texcoord2f(float s, float t) 1.103 +{ 1.104 + ds3_texcoord2((int32_t)(s * 65536.0f), (int32_t)(t * 65536.0f)); 1.105 +} 1.106 + 1.107 +void ds3_ortho(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar) 1.108 +{ 1.109 + int32_t m[16] = {0}; 1.110 + int32_t dx = right - left; 1.111 + int32_t dy = top - bottom; 1.112 + int32_t dz = zfar - znear; 1.113 + 1.114 + m[0] = 0x2000000 / (dx << 8); 1.115 + m[5] = 0x2000000 / (dy << 8); 1.116 + m[10] = -0x2000000 / (dz << 8); 1.117 + m[12] = -((right + left) << 8) / (dx << 8); 1.118 + m[13] = -((top + bottom) << 8) / (dy << 8); 1.119 + m[14] = -((zfar + znear) << 8) / (dz << 8); 1.120 + 1.121 + ds3_mult_matrix(m); 1.122 +} 1.123 + 1.124 +void ds3_orthof(float left, float right, float top, float bottom, float znear, float zfar) 1.125 +{ 1.126 + int32_t m[16] = {0}; 1.127 + float dx = right - left; 1.128 + float dy = top - bottom; 1.129 + float dz = zfar - znear; 1.130 + 1.131 + m[0] = (int32_t)(2.0f / dx * 65536.0f); 1.132 + m[5] = (int32_t)(2.0f / dy * 65536.0f); 1.133 + m[10] = (int32_t)(-2.0f / dz * 65536.0f); 1.134 + m[12] = (int32_t)(-(right + left) / dx); 1.135 + m[13] = (int32_t)(-(top + bottom) / dy); 1.136 + m[14] = (int32_t)(-(zfar + znear) / dz); 1.137 + 1.138 + ds3_mult_matrix(m); 1.139 +} 1.140 + 1.141 +void ds3_frustum(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar) 1.142 +{ 1.143 + int32_t m[16] = {0}; 1.144 + 1.145 + int32_t dx = right - left; 1.146 + int32_t dy = top - bottom; 1.147 + int32_t dz = zfar - znear; 1.148 + 1.149 + int32_t a = ((right + left) << 8) / (dx << 8); 1.150 + int32_t b = ((top + bottom) << 8) / (dy << 8); 1.151 + int32_t c = (-(zfar + znear) << 8) / (dz << 8); 1.152 + int32_t d = -(((zfar >> 8) * znear) << 1) / (dz << 8); 1.153 + 1.154 + m[0] = (znear << 9) / (dx << 8); 1.155 + m[5] = (znear << 9) / (dy << 8); 1.156 + m[8] = a; 1.157 + m[9] = b; 1.158 + m[10] = c; 1.159 + m[11] = -65536; 1.160 + m[14] = d; 1.161 + 1.162 + ds3_mult_matrix(m); 1.163 +} 1.164 + 1.165 +void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr) 1.166 +{ 1.167 + int32_t m[16] = {0}; 1.168 + 1.169 + float dx = right - left; 1.170 + float dy = top - bottom; 1.171 + float dz = fr - nr; 1.172 + 1.173 + float a = (right + left) / dx; 1.174 + float b = (top + bottom) / dy; 1.175 + float c = -(fr + nr) / dz; 1.176 + float d = -2.0 * fr * nr / dz; 1.177 + 1.178 + m[0] = (int32_t)(2.0 * nr / dx * 65536.0f); 1.179 + m[5] = (int32_t)(2.0 * nr / dy * 65536.0f); 1.180 + m[8] = (int32_t)(a * 65536.0f); 1.181 + m[9] = (int32_t)(b * 65536.0f); 1.182 + m[10] = (int32_t)(c * 65536.0f); 1.183 + m[11] = -65536; 1.184 + m[14] = (int32_t)(d * 65536.0f); 1.185 + 1.186 + ds3_mult_matrix(m); 1.187 +} 1.188 + 1.189 +void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar) 1.190 +{ 1.191 + int32_t m[16] = {0}; 1.192 + 1.193 + float vfov = M_PI * vfov_deg / 180.0f; 1.194 + float s = 1.0f / tan(vfov * 0.5f); 1.195 + float range = znear - zfar; 1.196 + 1.197 + m[0] = (int32_t)(s / aspect * 65536.0f); 1.198 + m[5] = (int32_t)(s * 65536.0f); 1.199 + m[10] = (int32_t)((znear + zfar) / range * 65536.0f); 1.200 + m[11] = -65536; 1.201 + m[14] = (int32_t)(2.0f * znear * zfar / range * 65536.0f); 1.202 + 1.203 + ds3_mult_matrix(m); 1.204 +}
2.1 --- a/src/ds3.h Sun Jan 28 20:05:26 2018 +0200 2.2 +++ b/src/ds3.h Mon Jan 29 03:48:05 2018 +0200 2.3 @@ -5,15 +5,15 @@ 2.4 2.5 #define RGB15(r, g, b) (((r) & 0x1f) | (((g) & 0x1f) << 5) | (((b) & 0x1f) << 10)) 2.6 2.7 -#define DS3_TEXTURE_2D 0x0001 2.8 -#define DS3_SPECULAR 0x0002 2.9 -#define DS3_ALPHA_TEST 0x0004 2.10 -#define DS3_BLEND 0x0008 2.11 -#define DS3_ANTIALIAS 0x0010 2.12 -#define DS3_EDGE 0x0020 2.13 -#define DS3_FOG_ALPHA 0x0040 2.14 -#define DS3_FOG 0x0080 2.15 -#define DS3_CLEAR_BM 0x4000 2.16 +#define DS3_TEXTURE_2D 0x0001 2.17 +#define DS3_SPECULAR 0x0002 2.18 +#define DS3_ALPHA_TEST 0x0004 2.19 +#define DS3_BLEND 0x0008 2.20 +#define DS3_POLYGON_SMOOTH 0x0010 2.21 +#define DS3_EDGE 0x0020 2.22 +#define DS3_FOG_ALPHA 0x0040 2.23 +#define DS3_FOG 0x0080 2.24 +#define DS3_CLEAR_BM 0x4000 2.25 2.26 #define DS3_TRIANGLES 0 2.27 #define DS3_QUADS 1 2.28 @@ -35,6 +35,10 @@ 2.29 void ds3_matrix_mode(int mmode); 2.30 void ds3_load_identity(void); 2.31 void ds3_load_matrix(int32_t *m); 2.32 +void ds3_load_matrix4x3(int32_t *m); 2.33 +void ds3_mult_matrix(int32_t *m); 2.34 +void ds3_mult_matrix4x3(int32_t *m); 2.35 +void ds3_mult_matrix3x3(int32_t *m); 2.36 void ds3_push_matrix(void); 2.37 void ds3_pop_matrix(void); 2.38 void ds3_translate(int32_t x, int32_t y, int32_t z); 2.39 @@ -46,7 +50,22 @@ 2.40 void ds3_end(void); 2.41 2.42 void ds3_vertex3(int32_t x, int32_t y, int32_t z); 2.43 +void ds3_vertex3f(float x, float y, float z); 2.44 +void ds3_vertex2(int32_t x, int32_t y); 2.45 +void ds3_vertex2f(float x, float y); 2.46 void ds3_color(uint16_t color); 2.47 void ds3_color3b(unsigned char r, unsigned char g, unsigned char b); 2.48 +void ds3_color3f(float r, float g, float b); 2.49 +void ds3_normal(int32_t x, int32_t y, int32_t z); 2.50 +void ds3_normal3f(float x, float y, float z); 2.51 +void ds3_texcoord2(int32_t s, int32_t t); 2.52 +void ds3_texcoord2f(float s, float t); 2.53 + 2.54 +void ds3_ortho(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar); 2.55 +void ds3_orthof(float left, float right, float top, float bottom, float znear, float zfar); 2.56 + 2.57 +void ds3_frustum(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar); 2.58 + 2.59 +void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar); 2.60 2.61 #endif /* DS3_H_ */
3.1 --- a/src/main.c Sun Jan 28 20:05:26 2018 +0200 3.2 +++ b/src/main.c Mon Jan 29 03:48:05 2018 +0200 3.3 @@ -51,7 +51,7 @@ 3.4 ds3_clear_depth(0x7fff); 3.5 ds3_viewport(0, 0, 256, 192); 3.6 3.7 - ds3_enable(DS3_ANTIALIAS); 3.8 + ds3_enable(DS3_POLYGON_SMOOTH); 3.9 3.10 REG_POLYGON_ATTR = 0x001f00c0; /* alpha = 31, cull none */ 3.11 3.12 @@ -64,10 +64,6 @@ 3.13 int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204; 3.14 int32_t sa = ((sintab[idx] >> 8) * scale) >> 8; 3.15 int32_t ca = ((costab[idx] >> 8) * scale) >> 8; 3.16 - /*float scale = 0.5 * sin(t * 0.8) + 0.8; 3.17 - int32_t sa = (int16_t)(sin(t) * 256 * scale); 3.18 - int32_t ca = (int16_t)(cos(t) * 256 * scale); 3.19 - */ 3.20 3.21 int32_t x = ca * -128 + sa * -96 + (128 << 8); 3.22 int32_t y = -sa * -128 + ca * -96 + (96 << 8);