# HG changeset patch # User John Tsiombikas # Date 1517190485 -7200 # Node ID d625ba001a62154cc99c0158ed9929787948d4ca # Parent abcaf667f2bd50651a53286820e7d44e31c2de53 more stuff in ds3 diff -r abcaf667f2bd -r d625ba001a62 src/ds3.c --- a/src/ds3.c Sun Jan 28 20:05:26 2018 +0200 +++ b/src/ds3.c Mon Jan 29 03:48:05 2018 +0200 @@ -1,4 +1,5 @@ #include +#include #include "dsregs.h" #include "ds3.h" @@ -52,6 +53,42 @@ } } +void ds3_load_matrix4x3(int32_t *m) +{ + int i; + for(i=0; i<12; i++) { + int16_t val = (int16_t)(*m++ >> 4); + REG_MTX_LOAD_4X3 = val; + } +} + +void ds3_mult_matrix(int32_t *m) +{ + int i; + for(i=0; i<16; i++) { + int16_t val = (int16_t)(*m++ >> 4); + REG_MTX_MULT_4X4 = val; + } +} + +void ds3_mult_matrix4x3(int32_t *m) +{ + int i; + for(i=0; i<12; i++) { + int16_t val = (int16_t)(*m++ >> 4); + REG_MTX_MULT_4X3 = val; + } +} + +void ds3_mult_matrix3x3(int32_t *m) +{ + int i; + for(i=0; i<9; i++) { + int16_t val = (int16_t)(*m++ >> 4); + REG_MTX_MULT_3X3 = val; + } +} + void ds3_push_matrix(void) { REG_MTX_PUSH = 0; @@ -97,6 +134,21 @@ REG_VTX_16 = (z >> 4) & 0xffff; } +void ds3_vertex3f(float x, float y, float z) +{ + ds3_vertex3((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f), (int32_t)(z * 65536.0f)); +} + +void ds3_vertex2(int32_t x, int32_t y) +{ + REG_VTX_XY = ((x >> 4) & 0xffff) | ((y << 12) & 0xffff0000); +} + +void ds3_vertex2f(float x, float y) +{ + ds3_vertex2((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f)); +} + void ds3_color(uint16_t color) { REG_COLOR = color; @@ -106,3 +158,130 @@ { REG_COLOR = RGB15(r >> 3, g >> 3, b >> 3); } + +void ds3_color3f(float r, float g, float b) +{ + uint16_t ir = r * 31.0f; + uint16_t ig = g * 31.0f; + uint16_t ib = b * 31.0f; + REG_COLOR = RGB15(ir, ig, ib); +} + +void ds3_normal(int32_t x, int32_t y, int32_t z) +{ + REG_NORMAL = ((x >> 7) & 0x3ff) | ((y << 17) & 0xffc00) | ((z << 27) & 0x3ff00000); +} + +void ds3_normal3f(float x, float y, float z) +{ + ds3_normal((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f), (int32_t)(z * 65536.0f)); +} + +void ds3_texcoord2(int32_t s, int32_t t) +{ + REG_TEXCOORD = (((s) >> 12) & 0xffff) | (((t) << 4) & 0xffff0000); +} + +void ds3_texcoord2f(float s, float t) +{ + ds3_texcoord2((int32_t)(s * 65536.0f), (int32_t)(t * 65536.0f)); +} + +void ds3_ortho(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar) +{ + int32_t m[16] = {0}; + int32_t dx = right - left; + int32_t dy = top - bottom; + int32_t dz = zfar - znear; + + m[0] = 0x2000000 / (dx << 8); + m[5] = 0x2000000 / (dy << 8); + m[10] = -0x2000000 / (dz << 8); + m[12] = -((right + left) << 8) / (dx << 8); + m[13] = -((top + bottom) << 8) / (dy << 8); + m[14] = -((zfar + znear) << 8) / (dz << 8); + + ds3_mult_matrix(m); +} + +void ds3_orthof(float left, float right, float top, float bottom, float znear, float zfar) +{ + int32_t m[16] = {0}; + float dx = right - left; + float dy = top - bottom; + float dz = zfar - znear; + + m[0] = (int32_t)(2.0f / dx * 65536.0f); + m[5] = (int32_t)(2.0f / dy * 65536.0f); + m[10] = (int32_t)(-2.0f / dz * 65536.0f); + m[12] = (int32_t)(-(right + left) / dx); + m[13] = (int32_t)(-(top + bottom) / dy); + m[14] = (int32_t)(-(zfar + znear) / dz); + + ds3_mult_matrix(m); +} + +void ds3_frustum(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar) +{ + int32_t m[16] = {0}; + + int32_t dx = right - left; + int32_t dy = top - bottom; + int32_t dz = zfar - znear; + + int32_t a = ((right + left) << 8) / (dx << 8); + int32_t b = ((top + bottom) << 8) / (dy << 8); + int32_t c = (-(zfar + znear) << 8) / (dz << 8); + int32_t d = -(((zfar >> 8) * znear) << 1) / (dz << 8); + + m[0] = (znear << 9) / (dx << 8); + m[5] = (znear << 9) / (dy << 8); + m[8] = a; + m[9] = b; + m[10] = c; + m[11] = -65536; + m[14] = d; + + ds3_mult_matrix(m); +} + +void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr) +{ + int32_t m[16] = {0}; + + float dx = right - left; + float dy = top - bottom; + float dz = fr - nr; + + float a = (right + left) / dx; + float b = (top + bottom) / dy; + float c = -(fr + nr) / dz; + float d = -2.0 * fr * nr / dz; + + m[0] = (int32_t)(2.0 * nr / dx * 65536.0f); + m[5] = (int32_t)(2.0 * nr / dy * 65536.0f); + m[8] = (int32_t)(a * 65536.0f); + m[9] = (int32_t)(b * 65536.0f); + m[10] = (int32_t)(c * 65536.0f); + m[11] = -65536; + m[14] = (int32_t)(d * 65536.0f); + + ds3_mult_matrix(m); +} + +void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar) +{ + int32_t m[16] = {0}; + + float vfov = M_PI * vfov_deg / 180.0f; + float s = 1.0f / tan(vfov * 0.5f); + float range = znear - zfar; + + m[0] = (int32_t)(s / aspect * 65536.0f); + m[5] = (int32_t)(s * 65536.0f); + m[10] = (int32_t)((znear + zfar) / range * 65536.0f); + m[11] = -65536; + m[14] = (int32_t)(2.0f * znear * zfar / range * 65536.0f); + + ds3_mult_matrix(m); +} diff -r abcaf667f2bd -r d625ba001a62 src/ds3.h --- a/src/ds3.h Sun Jan 28 20:05:26 2018 +0200 +++ b/src/ds3.h Mon Jan 29 03:48:05 2018 +0200 @@ -5,15 +5,15 @@ #define RGB15(r, g, b) (((r) & 0x1f) | (((g) & 0x1f) << 5) | (((b) & 0x1f) << 10)) -#define DS3_TEXTURE_2D 0x0001 -#define DS3_SPECULAR 0x0002 -#define DS3_ALPHA_TEST 0x0004 -#define DS3_BLEND 0x0008 -#define DS3_ANTIALIAS 0x0010 -#define DS3_EDGE 0x0020 -#define DS3_FOG_ALPHA 0x0040 -#define DS3_FOG 0x0080 -#define DS3_CLEAR_BM 0x4000 +#define DS3_TEXTURE_2D 0x0001 +#define DS3_SPECULAR 0x0002 +#define DS3_ALPHA_TEST 0x0004 +#define DS3_BLEND 0x0008 +#define DS3_POLYGON_SMOOTH 0x0010 +#define DS3_EDGE 0x0020 +#define DS3_FOG_ALPHA 0x0040 +#define DS3_FOG 0x0080 +#define DS3_CLEAR_BM 0x4000 #define DS3_TRIANGLES 0 #define DS3_QUADS 1 @@ -35,6 +35,10 @@ void ds3_matrix_mode(int mmode); void ds3_load_identity(void); void ds3_load_matrix(int32_t *m); +void ds3_load_matrix4x3(int32_t *m); +void ds3_mult_matrix(int32_t *m); +void ds3_mult_matrix4x3(int32_t *m); +void ds3_mult_matrix3x3(int32_t *m); void ds3_push_matrix(void); void ds3_pop_matrix(void); void ds3_translate(int32_t x, int32_t y, int32_t z); @@ -46,7 +50,22 @@ void ds3_end(void); void ds3_vertex3(int32_t x, int32_t y, int32_t z); +void ds3_vertex3f(float x, float y, float z); +void ds3_vertex2(int32_t x, int32_t y); +void ds3_vertex2f(float x, float y); void ds3_color(uint16_t color); void ds3_color3b(unsigned char r, unsigned char g, unsigned char b); +void ds3_color3f(float r, float g, float b); +void ds3_normal(int32_t x, int32_t y, int32_t z); +void ds3_normal3f(float x, float y, float z); +void ds3_texcoord2(int32_t s, int32_t t); +void ds3_texcoord2f(float s, float t); + +void ds3_ortho(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar); +void ds3_orthof(float left, float right, float top, float bottom, float znear, float zfar); + +void ds3_frustum(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar); + +void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar); #endif /* DS3_H_ */ diff -r abcaf667f2bd -r d625ba001a62 src/main.c --- a/src/main.c Sun Jan 28 20:05:26 2018 +0200 +++ b/src/main.c Mon Jan 29 03:48:05 2018 +0200 @@ -51,7 +51,7 @@ ds3_clear_depth(0x7fff); ds3_viewport(0, 0, 256, 192); - ds3_enable(DS3_ANTIALIAS); + ds3_enable(DS3_POLYGON_SMOOTH); REG_POLYGON_ATTR = 0x001f00c0; /* alpha = 31, cull none */ @@ -64,10 +64,6 @@ int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204; int32_t sa = ((sintab[idx] >> 8) * scale) >> 8; int32_t ca = ((costab[idx] >> 8) * scale) >> 8; - /*float scale = 0.5 * sin(t * 0.8) + 0.8; - int32_t sa = (int16_t)(sin(t) * 256 * scale); - int32_t ca = (int16_t)(cos(t) * 256 * scale); - */ int32_t x = ca * -128 + sa * -96 + (128 << 8); int32_t y = -sa * -128 + ca * -96 + (96 << 8);