Skip to content

Commit

Permalink
TrueColor: implement smoothest diminished lighting (#1220)
Browse files Browse the repository at this point in the history
* Initial implementation

* Done requested changes

* Small comment corrections

* Requested corrections

* Enable smooth lighting by default for TrueColor

* formulate smooth lighting values relative to Vanilla values

* it's all about bit-shifting, right?

* Let planes light be smooth

The beauty of the formula!

Most importantly, MAXLIGHTZ now have 8192 values, not 10240 as was planned initially, yet still producing nearly perfect smoothing.

* Smoother lighting for Strife

* Smooth/smoothest diminishing lighting for Heretic

* Use I_Realloc for colormaps (re-)allocation

Fixes problem with gamma-correction toggling and avoid dancing with malloc/free.

Co-Authored-By: Fabian Greffrath <fabian@greffrath.com>

* Smooth/smoothest diminishing lighting for Hexen

* Fix torch glowing effect for smoothest lighting

"1 << 3" is similar to logics of shifting amount of colormaps via "32 << 3".
Not sure if it's 100% identical to vanilla approach, but seems to correct.

* Make INVERSECOLORMAP macro again

I.e. handle all possible player-fixedcolormap changes via rendering only.

Co-Authored-By: Fabian Greffrath <fabian@greffrath.com>

* Heretic: use INVERSECOLORMAP for invul colormap generating

Co-Authored-By: Fabian Greffrath <fabian@greffrath.com>

* Done requested changes

---------

Co-authored-by: Fabian Greffrath <fabian@greffrath.com>
  • Loading branch information
JNechaevsky and fabiangreffrath authored Sep 11, 2024
1 parent 3ddd6fd commit e9c7b3d
Show file tree
Hide file tree
Showing 29 changed files with 426 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/crispy.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static crispy_t crispy_s = {
.smoothscaling = 1,
.soundfix = 1,
#ifdef CRISPY_TRUECOLOR
.smoothlight = 1,
.truecolor = 1,
#endif
.vsync = 1,
Expand Down
4 changes: 4 additions & 0 deletions src/doom/m_crispy.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ static void M_CrispyToggleSmoothLightingHook (void)
{
crispy->smoothlight = !crispy->smoothlight;

#ifdef CRISPY_TRUECOLOR
// [crispy] re-calculate amount of colormaps and light tables
R_InitColormaps();
#endif
// [crispy] re-calculate the zlight[][] array
R_InitLightTables();
// [crispy] re-calculate the scalelight[][] array
Expand Down
1 change: 0 additions & 1 deletion src/doom/m_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2789,7 +2789,6 @@ boolean M_Responder (event_t* ev)
I_SetPalette (W_CacheLumpName (DEH_String("PLAYPAL"),PU_CACHE));
#else
{
extern void R_InitColormaps (void);
I_SetPalette (0);
R_InitColormaps();
inhelpscreens = true;
Expand Down
4 changes: 2 additions & 2 deletions src/doom/p_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ void P_SegLengths (boolean contrast_only)
li->fakecontrast = -LIGHTBRIGHT;
else
if (abs(finesine[li->r_angle >> ANGLETOFINESHIFT]) < rightangle)
li->fakecontrast = -(LIGHTBRIGHT >> 1);
li->fakecontrast = -LIGHTBRIGHT / 2;
else
if (!dx)
li->fakecontrast = LIGHTBRIGHT;
else
if (abs(finecosine[li->r_angle >> ANGLETOFINESHIFT]) < rightangle)
li->fakecontrast = LIGHTBRIGHT >> 1;
li->fakecontrast = LIGHTBRIGHT / 2;
else
li->fakecontrast = 0;
}
Expand Down
14 changes: 12 additions & 2 deletions src/doom/r_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,17 +1189,27 @@ void R_InitColormaps (void)
// 256 byte align tables.
lump = W_GetNumForName(DEH_String("COLORMAP"));
colormaps = W_CacheLumpNum(lump, PU_STATIC);
NUMCOLORMAPS = 32; // [crispy] smooth diminishing lighting
#else
int c, i, j = 0;
byte r, g, b;

byte *const playpal = W_CacheLumpName("PLAYPAL", PU_STATIC);
byte *const colormap = W_CacheLumpName("COLORMAP", PU_STATIC);

if (!colormaps)
// [crispy] Smoothest diminishing lighting.
// Compiled in but not enabled TrueColor mode
// can't use more than original 32 colormaps.
if (crispy->truecolor && crispy->smoothlight)
{
colormaps = (lighttable_t*) Z_Malloc((NUMCOLORMAPS + 1) * 256 * sizeof(lighttable_t), PU_STATIC, 0);
NUMCOLORMAPS = 256;
}
else
{
NUMCOLORMAPS = 32;
}

colormaps = I_Realloc(colormaps, (NUMCOLORMAPS + 1) * 256 * sizeof(lighttable_t));

if (crispy->truecolor)
{
Expand Down
48 changes: 33 additions & 15 deletions src/doom/r_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ lighttable_t*** zlight = NULL;
int extralight;

// [crispy] parameterized for smooth diminishing lighting
int NUMCOLORMAPS;
int LIGHTLEVELS;
int LIGHTSEGSHIFT;
int LIGHTBRIGHT;
Expand Down Expand Up @@ -699,23 +700,40 @@ void R_InitLightTables (void)
// [crispy] smooth diminishing lighting
if (crispy->smoothlight)
{
LIGHTLEVELS = 32;
LIGHTSEGSHIFT = 3;
LIGHTBRIGHT = 2;
MAXLIGHTSCALE = 48;
LIGHTSCALESHIFT = 12;
MAXLIGHTZ = 1024;
LIGHTZSHIFT = 17;
#ifdef CRISPY_TRUECOLOR
if (crispy->truecolor)
{
// [crispy] if in TrueColor mode, use smoothest diminished lighting
LIGHTLEVELS = 16 << 4;
LIGHTSEGSHIFT = 4 - 4;
LIGHTBRIGHT = 1 << 4;
MAXLIGHTSCALE = 48 << 3;
LIGHTSCALESHIFT = 12 - 3;
MAXLIGHTZ = 128 << 6;
LIGHTZSHIFT = 20 - 6;
}
else
#endif
{
// [crispy] else, use paletted approach
LIGHTLEVELS = 16 << 1;
LIGHTSEGSHIFT = 4 - 1;
LIGHTBRIGHT = 1 << 1;
MAXLIGHTSCALE = 48 << 0;
LIGHTSCALESHIFT = 12 - 0;
MAXLIGHTZ = 128 << 3;
LIGHTZSHIFT = 20 - 3;
}
}
else
{
LIGHTLEVELS = 16;
LIGHTSEGSHIFT = 4;
LIGHTBRIGHT = 1;
MAXLIGHTSCALE = 48;
LIGHTSCALESHIFT = 12;
MAXLIGHTZ = 128;
LIGHTZSHIFT = 20;
LIGHTLEVELS = 16;
LIGHTSEGSHIFT = 4;
LIGHTBRIGHT = 1;
MAXLIGHTSCALE = 48;
LIGHTSCALESHIFT = 12;
MAXLIGHTZ = 128;
LIGHTZSHIFT = 20;
}

scalelight = malloc(LIGHTLEVELS * sizeof(*scalelight));
Expand Down Expand Up @@ -1092,7 +1110,7 @@ void R_SetupFrame (player_t* player)
{
fixedcolormap =
colormaps
+ player->fixedcolormap*256;
+ player->fixedcolormap*(NUMCOLORMAPS / 32)*256; // [crispy] smooth diminishing lighting

walllights = scalelightfixed;

Expand Down
4 changes: 3 additions & 1 deletion src/doom/r_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ extern lighttable_t* fixedcolormap;

// Number of diminishing brightness levels.
// There a 0-31, i.e. 32 LUT in the COLORMAP lump.
#define NUMCOLORMAPS 32
// [crispy] parameterized for smooth diminishing lighting
extern int NUMCOLORMAPS;

// Blocky/low detail mode.
//B remove this?
Expand Down Expand Up @@ -200,6 +201,7 @@ void R_Init (void);
// Called by M_Responder.
void R_SetViewSize (int blocks, int detail);

void R_InitColormaps(void);
void R_ExecuteSetViewSize(void);


Expand Down
1 change: 1 addition & 0 deletions src/heretic/d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ void D_BindVariables(void)
M_BindIntVariable("crispy_mouselook", &crispy->mouselook);
M_BindIntVariable("crispy_playercoords", &crispy->playercoords);
M_BindIntVariable("crispy_secretmessage", &crispy->secretmessage);
M_BindIntVariable("crispy_smoothlight", &crispy->smoothlight);
M_BindIntVariable("crispy_soundmono", &crispy->soundmono);
#ifdef CRISPY_TRUECOLOR
M_BindIntVariable("crispy_truecolor", &crispy->truecolor);
Expand Down
36 changes: 32 additions & 4 deletions src/heretic/mn_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ static boolean CrispyHires(int option);
static boolean CrispyToggleWidescreen(int option);
static boolean CrispySmoothing(int option);
static boolean CrispyBrightmaps(int option);
static boolean CrispySmoothLighting(int option);
static boolean CrispySoundMono(int option);
static boolean CrispySndChannels(int option);
static boolean CrispyAutomapStats(int option);
Expand Down Expand Up @@ -169,6 +170,8 @@ void MN_LoadSlotText(void);
extern void I_ReInitGraphics(int reinit);
extern void AM_LevelInit(boolean reinit);
extern void AM_initVariables(void);
extern void P_SegLengths (boolean contrast_only);
extern void R_InitLightTables (void);

// Public Data

Expand Down Expand Up @@ -368,6 +371,7 @@ static MenuItem_t Crispness1Items[] = {
{ITT_EMPTY, NULL, NULL, 0, MENU_NONE},
{ITT_EMPTY, NULL, NULL, 0, MENU_NONE},
{ITT_LRFUNC2, "APPLY BRIGHTMAPS TO:", CrispyBrightmaps, 0, MENU_NONE},
{ITT_LRFUNC2, "SMOOTH DIMINISHING LIGHTING:", CrispySmoothLighting, 0, MENU_NONE},
{ITT_EMPTY, NULL, NULL, 0, MENU_NONE},
{ITT_EMPTY, NULL, NULL, 0, MENU_NONE},
{ITT_LRFUNC2, "MONO SFX:", CrispySoundMono, 0, MENU_NONE},
Expand All @@ -379,7 +383,7 @@ static MenuItem_t Crispness1Items[] = {
static Menu_t Crispness1Menu = {
68, 35,
DrawCrispness,
15, Crispness1Items,
16, Crispness1Items,
0,
MENU_OPTIONS
};
Expand Down Expand Up @@ -1704,6 +1708,27 @@ static boolean CrispyBrightmaps(int option)
return true;
}

static void CrispySmoothLightingHook (void)
{
crispy->smoothlight = !crispy->smoothlight;
#ifdef CRISPY_TRUECOLOR
// [crispy] re-calculate amount of colormaps and light tables
R_InitColormaps();
#endif
// [crispy] re-calculate the zlight[][] array
R_InitLightTables();
// [crispy] re-calculate the scalelight[][] array
R_ExecuteSetViewSize();
// [crispy] re-calculate fake contrast
P_SegLengths(true);
}

static boolean CrispySmoothLighting(int option)
{
crispy->post_rendering_hook = CrispySmoothLightingHook;
return true;
}

static boolean CrispySoundMono(int option)
{
crispy->soundmono = !crispy->soundmono;
Expand Down Expand Up @@ -2982,13 +3007,16 @@ static void DrawCrispness1(void)
// Brightmaps
DrawCrispnessMultiItem(crispy->brightmaps, 213, 115, multiitem_brightmaps, false);

DrawCrispnessSubheader("AUDIBLE", 135);
// Smooth Diminishing Lighting
DrawCrispnessItem(crispy->smoothlight, 257, 125);

DrawCrispnessSubheader("AUDIBLE", 145);

// Mono SFX
DrawCrispnessItem(crispy->soundmono, 137, 145);
DrawCrispnessItem(crispy->soundmono, 137, 155);

// Sound Channels
DrawCrispnessMultiItem(snd_Channels >> 4, 181, 155, multiitem_sndchannels, false);
DrawCrispnessMultiItem(snd_Channels >> 4, 181, 165, multiitem_sndchannels, false);
}

static void DrawCrispness2(void)
Expand Down
23 changes: 21 additions & 2 deletions src/heretic/p_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ static angle_t anglediff(angle_t a, angle_t b)
return b - a;
}

static void P_SegLengths(void)
void P_SegLengths(boolean contrast_only)
{
int i;
const int rightangle = abs(finesine[(ANG60/2) >> ANGLETOFINESHIFT]);

for (i = 0; i < numsegs; i++)
{
Expand All @@ -186,6 +187,8 @@ static void P_SegLengths(void)
dx = li->v2->r_x - li->v1->r_x;
dy = li->v2->r_y - li->v1->r_y;

if (!contrast_only)
{
li->length = (uint32_t)(sqrt((double)dx * dx + (double)dy * dy) / 2);

// [crispy] re-calculate angle used for rendering
Expand All @@ -198,6 +201,22 @@ static void P_SegLengths(void)
{
li->r_angle = li->angle;
}
}

// [crispy] smoother fake contrast
if (!dy)
li->fakecontrast = -LIGHTBRIGHT;
else
if (abs(finesine[li->r_angle >> ANGLETOFINESHIFT]) < rightangle)
li->fakecontrast = -LIGHTBRIGHT / 2;
else
if (!dx)
li->fakecontrast = LIGHTBRIGHT;
else
if (abs(finecosine[li->r_angle >> ANGLETOFINESHIFT]) < rightangle)
li->fakecontrast = LIGHTBRIGHT / 2;
else
li->fakecontrast = 0;
}
}

Expand Down Expand Up @@ -839,7 +858,7 @@ void P_SetupLevel(int episode, int map, int playermask, skill_t skill)
P_RemoveSlimeTrails();

// [crispy] fix long wall wobble
P_SegLengths();
P_SegLengths(false);

bodyqueslot = 0;
deathmatch_p = deathmatchstarts;
Expand Down
20 changes: 15 additions & 5 deletions src/heretic/r_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,17 +708,27 @@ void R_InitColormaps(void)
length = W_LumpLength(lump);
colormaps = Z_Malloc(length, PU_STATIC, 0);
W_ReadLump(lump, colormaps);
NUMCOLORMAPS = 32; // [crispy] smooth diminishing lighting
#else
int c, i, j = 0;
byte r, g, b;

byte *const playpal = W_CacheLumpName("PLAYPAL", PU_STATIC);
byte *const colormap = W_CacheLumpName("COLORMAP", PU_STATIC);

if (!colormaps)
// [crispy] Smoothest diminishing lighting.
// Compiled in but not enabled TrueColor mode
// can't use more than original 32 colormaps.
if (crispy->truecolor && crispy->smoothlight)
{
colormaps = (lighttable_t*) Z_Malloc((NUMCOLORMAPS + 1) * 256 * sizeof(lighttable_t), PU_STATIC, 0);
NUMCOLORMAPS = 256;
}
else
{
NUMCOLORMAPS = 32;
}

colormaps = I_Realloc(colormaps, (NUMCOLORMAPS + 1) * 256 * sizeof(lighttable_t));

if (crispy->truecolor)
{
Expand Down Expand Up @@ -756,9 +766,9 @@ void R_InitColormaps(void)
// [crispy] Invulnerability (c == COLORMAPS), generated from COLORMAP lump
for (i = 0; i < 256; i++)
{
r = gamma2table[crispy->gamma][playpal[3 * colormap[c * 256 + i] + 0]] & ~3;
g = gamma2table[crispy->gamma][playpal[3 * colormap[c * 256 + i] + 1]] & ~3;
b = gamma2table[crispy->gamma][playpal[3 * colormap[c * 256 + i] + 2]] & ~3;
r = gamma2table[crispy->gamma][playpal[3 * colormap[INVERSECOLORMAP * 256 + i] + 0]] & ~3;
g = gamma2table[crispy->gamma][playpal[3 * colormap[INVERSECOLORMAP * 256 + i] + 1]] & ~3;
b = gamma2table[crispy->gamma][playpal[3 * colormap[INVERSECOLORMAP * 256 + i] + 2]] & ~3;

colormaps[j++] = 0xff000000 | (r << 16) | (g << 8) | b;
}
Expand Down
24 changes: 14 additions & 10 deletions src/heretic/r_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@
//
// lighting constants
//
#define LIGHTLEVELS 16
#define LIGHTSEGSHIFT 4
#define MAXLIGHTSCALE 48
#define LIGHTSCALESHIFT 12
#define MAXLIGHTZ 128
#define LIGHTZSHIFT 20
#define NUMCOLORMAPS 32 // number of diminishing
// [crispy] parameterized for smooth diminishing lighting
extern int LIGHTLEVELS;
extern int LIGHTSEGSHIFT;
extern int LIGHTBRIGHT;
extern int MAXLIGHTSCALE;
extern int LIGHTSCALESHIFT;
extern int MAXLIGHTZ;
extern int LIGHTZSHIFT;
// [crispy] parameterized for smooth diminishing lighting
extern int NUMCOLORMAPS; // number of diminishing
#define INVERSECOLORMAP 32

#define LOOKDIRMIN 110 // [crispy] -110, actually
Expand Down Expand Up @@ -171,6 +174,7 @@ typedef struct

uint32_t length; // [crispy] fix long wall wobble
angle_t r_angle; // [crispy] recalculated angle used for rendering
int fakecontrast;
} seg_t;

typedef struct
Expand Down Expand Up @@ -342,9 +346,9 @@ extern fixed_t projection;
extern int validcount;

extern int sscount, linecount, loopcount;
extern lighttable_t *scalelight[LIGHTLEVELS][MAXLIGHTSCALE];
extern lighttable_t *scalelightfixed[MAXLIGHTSCALE];
extern lighttable_t *zlight[LIGHTLEVELS][MAXLIGHTZ];
extern lighttable_t ***scalelight;
extern lighttable_t **scalelightfixed;
extern lighttable_t ***zlight;

extern int extralight;
extern lighttable_t *fixedcolormap;
Expand Down
Loading

0 comments on commit e9c7b3d

Please sign in to comment.