From 3085df1432327b2189c92e72d88904e0824e8aa4 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 18 Feb 2024 20:43:40 +0100 Subject: [PATCH] fix up colour cube/saturation/gamma cleans up things, and fixes it not being applied to 3D models and colourmapped sectors when not in palette rendering mode also adds a new way of handling gamma which can be used if wanted --- src/doomdef.h | 12 ++--- src/hardware/hw_main.c | 6 +++ src/hardware/hw_md2.c | 16 +++++++ src/v_video.c | 104 ++++++++++++++++++++++++----------------- src/v_video.h | 2 + 5 files changed, 88 insertions(+), 52 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index 60035995e..8e77e87ae 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -558,7 +558,10 @@ extern const char *compdate, *comptime, *comprevision, *compbranch; // None of these that are disabled in the normal build are guaranteed to work perfectly // Compile them at your own risk! -/// Kalaron/Eternity Engine slope code (SRB2CB ported) +/// Undefine to use the new method of Gamma correction see colour cube in v_video.c +#define BACKWARDSCOMPATCORRECTION + +/// Kalaron/Eternity Engine slope code (SRB2CB ported) #define ESLOPE /// Backwards compatibility with SRB2CB's slope linedef types. @@ -622,13 +625,6 @@ extern const char *compdate, *comptime, *comprevision, *compbranch; /// SRB2Kart: MIDI support is shitty and busted and we don't want it, lets throw it behind a define #define NO_MIDI -/// FINALLY some real clipping that doesn't make walls dissappear AND speeds the game up -/// (that was the original comment from SRB2CB, sadly it is a lie and actually slows game down) -/// on the bright side it fixes some weird issues with translucent walls -/// \note SRB2CB port. -/// SRB2CB itself ported this from PrBoom+ -//#define NEWCLIP - /// Sprite rotation #define ROTSPRITE #define ROTANGLES 72 // Needs to be a divisor of 360 (45, 60, 90, 120...) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index c8e23e79f..487a6976b 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -501,6 +501,12 @@ void HWR_Lighting(FSurfaceInfo *Surface, INT32 light_level, extracolormap_t *col // Clamp the light level, since it can sometimes go out of the 0-255 range from animations light_level = min(max(light_level, cv_secbright.value), 255); + // in palette rendering mode, this is not needed since it properly takes the changes to the palette itself + if (!HWR_ShouldUsePaletteRendering()) + { + V_CubeApply(&tint_color.s.red, &tint_color.s.green, &tint_color.s.blue); + V_CubeApply(&fade_color.s.red, &fade_color.s.green, &fade_color.s.blue); + } Surface->PolyColor.rgba = poly_color.rgba; Surface->TintColor.rgba = tint_color.rgba; Surface->FadeColor.rgba = fade_color.rgba; diff --git a/src/hardware/hw_md2.c b/src/hardware/hw_md2.c index c017fdbd2..96a9d12da 100644 --- a/src/hardware/hw_md2.c +++ b/src/hardware/hw_md2.c @@ -383,6 +383,7 @@ static void md2_loadTexture(md2_t *model) if (!grpatch->mipmap->downloaded && !grpatch->mipmap->data) { int w = 0, h = 0; + #ifdef HAVE_PNG grpatch->mipmap->format = PNG_Load(filename, &w, &h, grpatch); if (grpatch->mipmap->format == 0) @@ -401,6 +402,21 @@ static void md2_loadTexture(md2_t *model) grpatch->height = (INT16)h; grpatch->mipmap->width = (UINT16)w; grpatch->mipmap->height = (UINT16)h; + + // for palette rendering, color cube is applied in post-processing instead of here + if (!HWR_ShouldUsePaletteRendering()) + { + UINT32 size; + RGBA_t *image; + // Lactozilla: Apply colour cube + image = grpatch->mipmap->data; + size = w*h; + while (size--) + { + V_CubeApply(&image->s.red, &image->s.green, &image->s.blue); + image++; + } + } } HWD.pfnSetTexture(grpatch->mipmap); HWR_UnlockCachedPatch(grpatch); diff --git a/src/v_video.c b/src/v_video.c index 025bc3248..0d0405208 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -95,6 +95,7 @@ toast 18/04/17 */ float Cubepal[2][2][2][3]; +boolean Cubeapply = false; // returns whether to apply cube, selectively avoiding expensive operations static boolean InitCube(void) @@ -279,12 +280,14 @@ static boolean InitCube(void) return true; } +#ifdef BACKWARDSCOMPATCORRECTION /* So it turns out that the way gamma was implemented previously, the default colour profile of the game was messed up. Since this bad decision has been around for a long time, and the intent is to keep the base game looking the same, I'm not gonna be the one to remove this base modification. toast 20/04/17 +... welp yes i am (27/07/19, see the ifdef around it) */ const UINT8 correctiontable[256] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, @@ -303,11 +306,12 @@ const UINT8 correctiontable[256] = 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255}; +#endif // keep a copy of the palette so that we can get the RGB value for a color index at any time. static void LoadPalette(const char *lumpname) { - boolean cube = InitCube(); + Cubeapply = InitCube(); lumpnum_t lumpnum = W_GetNumForName(lumpname); size_t i, palsize = W_LumpLength(lumpnum)/3; UINT8 *pal; @@ -319,51 +323,63 @@ static void LoadPalette(const char *lumpname) pal = W_CacheLumpNum(lumpnum, PU_CACHE); for (i = 0; i < palsize; i++) { +#ifdef BACKWARDSCOMPATCORRECTION pLocalPalette[i].s.red = correctiontable[*pal++]; pLocalPalette[i].s.green = correctiontable[*pal++]; pLocalPalette[i].s.blue = correctiontable[*pal++]; +#else + pLocalPalette[i].s.red = *pal++; + pLocalPalette[i].s.green = *pal++; + pLocalPalette[i].s.blue = *pal++; +#endif pLocalPalette[i].s.alpha = 0xFF; - // lerp of colour cubing! - if (cube) - { - float working[4][3]; - float linear; - UINT8 q; - - linear = (pLocalPalette[i].s.red/255.0); -#define dolerp(e1, e2) ((1 - linear)*e1 + linear*e2) - for (q = 0; q < 3; q++) - { - working[0][q] = dolerp(Cubepal[0][0][0][q], Cubepal[1][0][0][q]); - working[1][q] = dolerp(Cubepal[0][1][0][q], Cubepal[1][1][0][q]); - working[2][q] = dolerp(Cubepal[0][0][1][q], Cubepal[1][0][1][q]); - working[3][q] = dolerp(Cubepal[0][1][1][q], Cubepal[1][1][1][q]); - } - linear = (pLocalPalette[i].s.green/255.0); - for (q = 0; q < 3; q++) - { - working[0][q] = dolerp(working[0][q], working[1][q]); - working[1][q] = dolerp(working[2][q], working[3][q]); - } - linear = (pLocalPalette[i].s.blue/255.0); - for (q = 0; q < 3; q++) - { - working[0][q] = 255*dolerp(working[0][q], working[1][q]); - if (working[0][q] > 255.0) - working[0][q] = 255.0; - else if (working[0][q] < 0.0) - working[0][q] = 0.0; - } -#undef dowork - - pLocalPalette[i].s.red = (UINT8)(working[0][0]); - pLocalPalette[i].s.green = (UINT8)(working[0][1]); - pLocalPalette[i].s.blue = (UINT8)(working[0][2]); - } + // lerp of colour cubing! if you want, make it smoother yourself + if (Cubeapply) + V_CubeApply(&pLocalPalette[i].s.red, &pLocalPalette[i].s.green, &pLocalPalette[i].s.blue); } } +void V_CubeApply(UINT8 *red, UINT8 *green, UINT8 *blue) +{ + float working[4][3]; + float linear; + UINT8 q; + + if (!Cubeapply) + return; + + linear = (*red/255.0); +#define dolerp(e1, e2) ((1 - linear)*e1 + linear*e2) + for (q = 0; q < 3; q++) + { + working[0][q] = dolerp(Cubepal[0][0][0][q], Cubepal[1][0][0][q]); + working[1][q] = dolerp(Cubepal[0][1][0][q], Cubepal[1][1][0][q]); + working[2][q] = dolerp(Cubepal[0][0][1][q], Cubepal[1][0][1][q]); + working[3][q] = dolerp(Cubepal[0][1][1][q], Cubepal[1][1][1][q]); + } + linear = (*green/255.0); + for (q = 0; q < 3; q++) + { + working[0][q] = dolerp(working[0][q], working[1][q]); + working[1][q] = dolerp(working[2][q], working[3][q]); + } + linear = (*blue/255.0); + for (q = 0; q < 3; q++) + { + working[0][q] = 255*dolerp(working[0][q], working[1][q]); + if (working[0][q] > 255.0) + working[0][q] = 255.0; + else if (working[0][q] < 0.0) + working[0][q] = 0.0; + } +#undef dolerp + + *red = (UINT8)(working[0][0]); + *green = (UINT8)(working[0][1]); + *blue = (UINT8)(working[0][2]); +} + const char *R_GetPalname(UINT16 num) { static char palname[9]; @@ -985,11 +1001,11 @@ void V_DrawFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 c) if (alphalevel) { if (alphalevel == 13) - alphalevel = hudminusalpha[cv_translucenthud.value]; + alphalevel = hudminusalpha[hudtrans]; else if (alphalevel == 14) - alphalevel = 10 - cv_translucenthud.value; + alphalevel = 10 - hudtrans; else if (alphalevel == 15) - alphalevel = hudplusalpha[cv_translucenthud.value]; + alphalevel = hudplusalpha[hudtrans]; if (alphalevel >= 10) return; // invis @@ -1124,11 +1140,11 @@ void V_DrawFillConsoleMap(INT32 x, INT32 y, INT32 w, INT32 h, INT32 c) if ((alphalevel = ((c & V_ALPHAMASK) >> V_ALPHASHIFT))) { if (alphalevel == 13) - alphalevel = hudminusalpha[cv_translucenthud.value]; + alphalevel = hudminusalpha[hudtrans]; else if (alphalevel == 14) - alphalevel = 10 - cv_translucenthud.value; + alphalevel = 10 - hudtrans; else if (alphalevel == 15) - alphalevel = hudplusalpha[cv_translucenthud.value]; + alphalevel = hudplusalpha[hudtrans]; if (alphalevel >= 10) return; // invis diff --git a/src/v_video.h b/src/v_video.h index f8a005456..d65966731 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -61,6 +61,8 @@ extern RGBA_t *pLocalPalette; extern UINT8 hudtrans; +void V_CubeApply(UINT8 *red, UINT8 *green, UINT8 *blue); + // Retrieve the ARGB value from a palette color index #define V_GetColor(color) (pLocalPalette[color&0xFF])