From 77f9dbee1721518e09f0beed10b3dbb78d893b08 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 13 Feb 2026 10:27:58 -0800 Subject: Ambient lighting. Defer texture filtering --- src/swgfx.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src') 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 { R* depth; // Depth buffer. sgTextureId* texture; // Texture ID buffer. sgVec2* texcoords; // Texture coords buffer. + sgPixel* albedo; // Albedo buffer. sgViewport_t viewport; sgMat4 model; // Model matrix. sgMat4 view; // View matrix. @@ -89,6 +90,7 @@ static inline sgVec2 mod2(sgVec2 v, R m) { return (sgVec2){mod1(v.x, m), mod1(v static inline sgVec3 add3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x + b.x, a.y + b.y, a.z + b.z}; } static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; } static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; } +static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; } static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; } static 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) { Align(N * sizeof(R)) + // Depth buffer. Align(N * sizeof(sgTextureId)) + // Texture ID buffer. Align(N * sizeof(sgVec2)) + // Texture coords buffer. + Align(N * sizeof(sgPixel)) + // Albedo buffer. Align(SWGFX_TEXTURE_REGISTER_SIZE * sizeof(sgTexture)) + // Texture register. (SG_ALIGN - 1); // To make room to align allocations within the buffer. } @@ -752,6 +755,7 @@ swgfx* sgNew(int width, int height, void* mem) { gfx->depth = SG_ALLOC(&aligned, N, R); gfx->texture = SG_ALLOC(&aligned, N, sgTextureId); gfx->texcoords = SG_ALLOC(&aligned, N, sgVec2); + gfx->albedo = SG_ALLOC(&aligned, N, sgPixel); gfx->textureRegister = SG_ALLOC(&aligned, SWGFX_TEXTURE_REGISTER_SIZE, sgTexture); gfx->activeTexture = DefaultTextureId; gfx->defaultPixel = (sgPixel){255, 255, 255, 255}; @@ -973,10 +977,21 @@ void sgLighting(swgfx* gfx) { const sgTextureId texid = gfx->texture[i]; const sgTexture* texture = &gfx->textureRegister[texid]; const sgVec2 uv = gfx->texcoords[i]; - sgPixel* colour = &gfx->colour[i]; - // TODO: Actual lighting. - const sgPixel albedo = Sample(texture->image, texture->filter, uv); - *colour = albedo; + sgPixel* albedo = &gfx->albedo[i]; + *albedo = Sample(texture->image, texture->filter, uv); + } + } +} + +void sgAmbient(swgfx* gfx, sgVec3 ambient) { + assert(gfx); + const int N = gfx->dims.x * gfx->dims.y; + for (int i = 0; i < N; ++i) { + const R depth = gfx->depth[i]; + if (depth != DepthClearValue) { + const sgPixel* albedo = &gfx->albedo[i]; + sgPixel* colour = &gfx->colour[i]; + *colour = Vec3ToPixel(mul3(PixelToVec3(*albedo), ambient), (R)albedo->a/255.f); } } } -- cgit v1.2.3