summaryrefslogtreecommitdiff
path: root/src/swgfx.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2026-02-13 10:27:58 -0800
committer3gg <3gg@shellblade.net>2026-02-13 10:27:58 -0800
commit77f9dbee1721518e09f0beed10b3dbb78d893b08 (patch)
treeb9b26485ef1cff497867e120ae27428c6f82c029 /src/swgfx.c
parent5f7855b6aa0e3338625eb7d30e8ccc5b74050bbf (diff)
Ambient lighting. Defer texture filtering
Diffstat (limited to 'src/swgfx.c')
-rw-r--r--src/swgfx.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/swgfx.c b/src/swgfx.c
index d41b69d..ddd64f7 100644
--- a/src/swgfx.c
+++ b/src/swgfx.c
@@ -45,6 +45,7 @@ typedef struct swgfx {
45 R* depth; // Depth buffer. 45 R* depth; // Depth buffer.
46 sgTextureId* texture; // Texture ID buffer. 46 sgTextureId* texture; // Texture ID buffer.
47 sgVec2* texcoords; // Texture coords buffer. 47 sgVec2* texcoords; // Texture coords buffer.
48 sgPixel* albedo; // Albedo buffer.
48 sgViewport_t viewport; 49 sgViewport_t viewport;
49 sgMat4 model; // Model matrix. 50 sgMat4 model; // Model matrix.
50 sgMat4 view; // View matrix. 51 sgMat4 view; // View matrix.
@@ -89,6 +90,7 @@ static inline sgVec2 mod2(sgVec2 v, R m) { return (sgVec2){mod1(v.x, m), mod1(v
89static inline sgVec3 add3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x + b.x, a.y + b.y, a.z + b.z}; } 90static inline sgVec3 add3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x + b.x, a.y + b.y, a.z + b.z}; }
90static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; } 91static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; }
91static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; } 92static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; }
93static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; }
92static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } 94static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; }
93static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; } 95static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; }
94static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};} 96static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};}
@@ -739,6 +741,7 @@ size_t sgMem(int width, int height) {
739 Align(N * sizeof(R)) + // Depth buffer. 741 Align(N * sizeof(R)) + // Depth buffer.
740 Align(N * sizeof(sgTextureId)) + // Texture ID buffer. 742 Align(N * sizeof(sgTextureId)) + // Texture ID buffer.
741 Align(N * sizeof(sgVec2)) + // Texture coords buffer. 743 Align(N * sizeof(sgVec2)) + // Texture coords buffer.
744 Align(N * sizeof(sgPixel)) + // Albedo buffer.
742 Align(SWGFX_TEXTURE_REGISTER_SIZE * sizeof(sgTexture)) + // Texture register. 745 Align(SWGFX_TEXTURE_REGISTER_SIZE * sizeof(sgTexture)) + // Texture register.
743 (SG_ALIGN - 1); // To make room to align allocations within the buffer. 746 (SG_ALIGN - 1); // To make room to align allocations within the buffer.
744} 747}
@@ -752,6 +755,7 @@ swgfx* sgNew(int width, int height, void* mem) {
752 gfx->depth = SG_ALLOC(&aligned, N, R); 755 gfx->depth = SG_ALLOC(&aligned, N, R);
753 gfx->texture = SG_ALLOC(&aligned, N, sgTextureId); 756 gfx->texture = SG_ALLOC(&aligned, N, sgTextureId);
754 gfx->texcoords = SG_ALLOC(&aligned, N, sgVec2); 757 gfx->texcoords = SG_ALLOC(&aligned, N, sgVec2);
758 gfx->albedo = SG_ALLOC(&aligned, N, sgPixel);
755 gfx->textureRegister = SG_ALLOC(&aligned, SWGFX_TEXTURE_REGISTER_SIZE, sgTexture); 759 gfx->textureRegister = SG_ALLOC(&aligned, SWGFX_TEXTURE_REGISTER_SIZE, sgTexture);
756 gfx->activeTexture = DefaultTextureId; 760 gfx->activeTexture = DefaultTextureId;
757 gfx->defaultPixel = (sgPixel){255, 255, 255, 255}; 761 gfx->defaultPixel = (sgPixel){255, 255, 255, 255};
@@ -973,10 +977,21 @@ void sgLighting(swgfx* gfx) {
973 const sgTextureId texid = gfx->texture[i]; 977 const sgTextureId texid = gfx->texture[i];
974 const sgTexture* texture = &gfx->textureRegister[texid]; 978 const sgTexture* texture = &gfx->textureRegister[texid];
975 const sgVec2 uv = gfx->texcoords[i]; 979 const sgVec2 uv = gfx->texcoords[i];
976 sgPixel* colour = &gfx->colour[i]; 980 sgPixel* albedo = &gfx->albedo[i];
977 // TODO: Actual lighting. 981 *albedo = Sample(texture->image, texture->filter, uv);
978 const sgPixel albedo = Sample(texture->image, texture->filter, uv); 982 }
979 *colour = albedo; 983 }
984}
985
986void sgAmbient(swgfx* gfx, sgVec3 ambient) {
987 assert(gfx);
988 const int N = gfx->dims.x * gfx->dims.y;
989 for (int i = 0; i < N; ++i) {
990 const R depth = gfx->depth[i];
991 if (depth != DepthClearValue) {
992 const sgPixel* albedo = &gfx->albedo[i];
993 sgPixel* colour = &gfx->colour[i];
994 *colour = Vec3ToPixel(mul3(PixelToVec3(*albedo), ambient), (R)albedo->a/255.f);
980 } 995 }
981 } 996 }
982} 997}