From e3f61f3c78430ba02ba3a54e9e8e883502d5eb1c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 22 Feb 2025 09:33:08 +1100 Subject: [PATCH] Initial work on texture and colour matrix support --- GL/matrix.c | 16 +++++++++-- GL/private.h | 2 ++ GL/tnl_effects.c | 73 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/GL/matrix.c b/GL/matrix.c index 422e3f8..3e2b10f 100644 --- a/GL/matrix.c +++ b/GL/matrix.c @@ -130,8 +130,20 @@ static void UpdateNormalMatrix() { } static void OnMatrixChanged() { - if(MATRIX_MODE == GL_MODELVIEW) NORMAL_DIRTY = true; - if(MATRIX_MODE == GL_PROJECTION) PROJECTION_DIRTY = true; + switch (MATRIX_MODE) { + case GL_MODELVIEW: + NORMAL_DIRTY = true; + return; + case GL_PROJECTION: + PROJECTION_DIRTY = true; + return; + case GL_TEXTURE: + _glTnlUpdateTextureMatrix(); + return; + case GL_COLOR: + _glTnlUpdateColorMatrix(); + return; + } } diff --git a/GL/private.h b/GL/private.h index a491932..846f421 100644 --- a/GL/private.h +++ b/GL/private.h @@ -492,6 +492,8 @@ void _glTnlLoadMatrix(void); void _glTnlApplyEffects(SubmissionTarget* target); void _glTnlUpdateLighting(void); +void _glTnlUpdateTextureMatrix(void); +void _glTnlUpdateColorMatrix(void); /* This is from KOS pvr_buffers.c */ #define PVR_MIN_Z 0.0001f diff --git a/GL/tnl_effects.c b/GL/tnl_effects.c index 97755e1..440c869 100644 --- a/GL/tnl_effects.c +++ b/GL/tnl_effects.c @@ -8,10 +8,7 @@ #include "private.h" #include "platform.h" -#define TNL_FX_LIGHTING 0x01 -#define TNL_FX_TEXTURE 0x02 -#define TNL_FX_COLOR 0x04 -static int TNL_EFFECTS; +static int TNL_EFFECTS, TNL_LIGHTING, TNL_TEXTURE, TNL_COLOR; #define ITERATE(count) \ GLuint i = count; \ @@ -25,13 +22,17 @@ void _glTnlLoadMatrix(void) { * If we're not doing lighting though we can optimise by taking * vertices straight to clip-space */ - if(TNL_EFFECTS & TNL_FX_LIGHTING) { + if(TNL_LIGHTING) { _glMatrixLoadModelView(); } else { _glMatrixLoadModelViewProjection(); } } +static void updateEffects(void) { + TNL_EFFECTS = TNL_LIGHTING | TNL_TEXTURE | TNL_COLOR; +} + static void transformVertices(SubmissionTarget* target) { TRACE(); @@ -66,20 +67,70 @@ static void lightingEffect(SubmissionTarget* target) { } void _glTnlUpdateLighting(void) { - if (_glIsLightingEnabled()) { - TNL_EFFECTS |= TNL_FX_LIGHTING; - } else { - TNL_EFFECTS &= ~TNL_FX_LIGHTING; + TNL_LIGHTING = _glIsLightingEnabled(); + updateEffects(); +} + + +static void textureEffect(SubmissionTarget* target) { + Matrix4x4* m = _glGetTextureMatrix(); + UploadMatrix4x4(m); + float coords[4]; + + Vertex* it = _glSubmissionTargetStart(target); + uint32_t count = target->count; + + ITERATE(count) { + TransformVertex(it->uv[0], it->uv[1], 0.0f, 1.0f, coords, &coords[3]); + it->uv[0] = coords[0]; + it->uv[1] = coords[1]; + it++; } } +void _glTnlUpdateTextureMatrix(void) { + Matrix4x4* m = _glGetTextureMatrix(); + TNL_TEXTURE = _glIsIdentity(m); + updateEffects(); +} + + +static void colorEffect(SubmissionTarget* target) { + Matrix4x4* m = _glGetColorMatrix(); + UploadMatrix4x4(m); + float coords[4]; + + Vertex* it = _glSubmissionTargetStart(target); + uint32_t count = target->count; + + ITERATE(count) { + TransformVertex(it->bgra[2], it->bgra[1], it->bgra[0], it->bgra[3], coords, &coords[3]); + it->bgra[2] = clamp(coords[0], 0, 255); + it->bgra[1] = clamp(coords[1], 0, 255); + it->bgra[0] = clamp(coords[2], 0, 255); + it->bgra[3] = clamp(coords[3], 0, 255); + it++; + } +} + +void _glTnlUpdateColorMatrix(void) { + Matrix4x4* m = _glGetColorMatrix(); + TNL_COLOR = _glIsIdentity(m); + updateEffects(); +} + + void _glTnlApplyEffects(SubmissionTarget* target) { if (!TNL_EFFECTS) return; - if (TNL_EFFECTS & TNL_FX_LIGHTING) + if (TNL_LIGHTING) lightingEffect(target); + if (TNL_TEXTURE) + textureEffect(target); + if (TNL_COLOR) + colorEffect(target); - if (TNL_EFFECTS & TNL_FX_LIGHTING) { + if (TNL_LIGHTING) { /* OK eye-space work done, now move into clip space */ _glMatrixLoadProjection(); transformVertices(target);