Skip to content

Commit

Permalink
tr2/render/swr: fix missing underwater tint
Browse files Browse the repository at this point in the history
Resolves #2066.
  • Loading branch information
rr- committed Dec 17, 2024
1 parent bcc9ac7 commit db3026c
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 15 deletions.
6 changes: 6 additions & 0 deletions data/tr1/ship/shaders/2d.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ uniform sampler1D texPalette;
uniform sampler2D texAlpha;
uniform bool paletteEnabled;
uniform bool alphaEnabled;
uniform bool tintEnabled;
uniform vec3 tintColor;
uniform int effect;

#ifdef OGL33C
Expand Down Expand Up @@ -65,6 +67,10 @@ void main(void) {
OUTCOLOR = TEXTURE2D(texMain, uv);
}

if (tintEnabled) {
OUTCOLOR.rgb *= tintColor.rgb;
}

if (effect == EFFECT_VIGNETTE) {
float x_dist = vertCoords.x - 0.5;
float y_dist = vertCoords.y - 0.5;
Expand Down
6 changes: 6 additions & 0 deletions data/tr2/ship/shaders/2d.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ uniform sampler1D texPalette;
uniform sampler2D texAlpha;
uniform bool paletteEnabled;
uniform bool alphaEnabled;
uniform bool tintEnabled;
uniform vec3 tintColor;
uniform int effect;

#ifdef OGL33C
Expand Down Expand Up @@ -65,6 +67,10 @@ void main(void) {
OUTCOLOR = TEXTURE2D(texMain, uv);
}

if (tintEnabled) {
OUTCOLOR.rgb *= tintColor.rgb;
}

if (effect == EFFECT_VIGNETTE) {
float x_dist = vertCoords.x - 0.5;
float y_dist = vertCoords.y - 0.5;
Expand Down
1 change: 1 addition & 0 deletions docs/tr2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- added an option to use PS1 contrast levels, available under F8 (#1646)
- added an option to allow disabling the developer console (#2063)
- fixed Lara prioritising throwing a spent flare while mid-air, so to avoid missing ledge grabs (#1989)
- fixed software renderer not applying underwater tint (#2066, regression from 0.7)

## [0.7.1](https://github.com/LostArtefacts/TRX/compare/tr2-0.7...tr2-0.7.1) - 2024-12-17
- fixed a crash when selecting the sound option (#2057, regression from 0.6)
Expand Down
2 changes: 1 addition & 1 deletion docs/tr2/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4657,7 +4657,7 @@ typedef enum {
0x004D93F0 - uint8_t g_LabTextureUVFlag[2048]; // MAX_TEXTURES
0x005251B0 - FRAME_INFO *g_AnimFrames;
0x0051BC38 - int32_t g_IsWet;
0x0051B308 - RGB_888 g_WaterPalette[256];
0x0051B308 + RGB_888 g_WaterPalette[256];
0x004BF2D8 - uint8_t g_DepthQIndex[256];
0x004D7C74 - int32_t g_NumCameras;
0x0051B92C - int16_t *g_AnimTextureRanges;
Expand Down
28 changes: 27 additions & 1 deletion src/libtrx/gfx/2d/2d_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ typedef enum {
M_UNIFORM_TEXTURE_ALPHA,
M_UNIFORM_PALETTE_ENABLED,
M_UNIFORM_ALPHA_ENABLED,
M_UNIFORM_TINT_ENABLED,
M_UNIFORM_TINT_COLOR,
M_UNIFORM_EFFECT,
M_UNIFORM_NUMBER_OF,
} M_UNIFORM;
Expand Down Expand Up @@ -45,6 +47,7 @@ struct GFX_2D_RENDERER {
int32_t y;
} repeat;

GFX_COLOR tint_color;
GFX_2D_EFFECT effect;
bool use_palette;
bool use_alpha;
Expand Down Expand Up @@ -102,6 +105,7 @@ GFX_2D_RENDERER *GFX_2D_Renderer_Create(void)
const GFX_CONFIG *const config = GFX_Context_GetConfig();

r->effect = GFX_2D_EFFECT_NONE;
r->tint_color = (GFX_COLOR) { .r = 255, .g = 255, .b = 255 };
r->use_palette = false;
r->use_alpha = false;
r->repeat.x = 1;
Expand Down Expand Up @@ -145,6 +149,8 @@ GFX_2D_RENDERER *GFX_2D_Renderer_Create(void)
{ M_UNIFORM_TEXTURE_ALPHA, "texAlpha" },
{ M_UNIFORM_PALETTE_ENABLED, "paletteEnabled" },
{ M_UNIFORM_ALPHA_ENABLED, "alphaEnabled" },
{ M_UNIFORM_TINT_ENABLED, "tintEnabled" },
{ M_UNIFORM_TINT_COLOR, "tintColor" },
{ M_UNIFORM_EFFECT, "effect" },
{ -1, NULL },
};
Expand All @@ -162,6 +168,10 @@ GFX_2D_RENDERER *GFX_2D_Renderer_Create(void)
&r->program, r->loc[M_UNIFORM_PALETTE_ENABLED], r->use_palette);
GFX_GL_Program_Uniform1i(
&r->program, r->loc[M_UNIFORM_ALPHA_ENABLED], r->use_alpha);
GFX_GL_Program_Uniform1i(
&r->program, r->loc[M_UNIFORM_TINT_ENABLED],
r->tint_color.r != 255 || r->tint_color.g != 255
|| r->tint_color.b != 255);
GFX_GL_Program_Uniform1i(&r->program, r->loc[M_UNIFORM_EFFECT], r->effect);
GFX_GL_CheckError();

Expand Down Expand Up @@ -291,7 +301,7 @@ void GFX_2D_Renderer_Upload(
}

void GFX_2D_Renderer_SetPalette(
GFX_2D_RENDERER *const r, const GFX_PALETTE_ENTRY *const palette)
GFX_2D_RENDERER *const r, const GFX_COLOR *const palette)
{
ASSERT(r != NULL);

Expand Down Expand Up @@ -355,6 +365,22 @@ void GFX_2D_Renderer_SetEffect(
}
}

void GFX_2D_Renderer_SetTint(GFX_2D_RENDERER *const r, const GFX_COLOR color)
{
ASSERT(r != NULL);
if (r->tint_color.r != color.r || r->tint_color.g != color.g
|| r->tint_color.b != color.b) {
GFX_GL_Program_Bind(&r->program);
GFX_GL_Program_Uniform1i(
&r->program, r->loc[M_UNIFORM_TINT_ENABLED],
color.r != 255 || color.g != 255 || color.b != 255);
GFX_GL_Program_Uniform3f(
&r->program, r->loc[M_UNIFORM_TINT_COLOR], color.r / 255.0,
color.g / 255.0, color.b / 255.0);
r->tint_color = color;
}
}

void GFX_2D_Renderer_Render(GFX_2D_RENDERER *const r)
{
ASSERT(r != NULL);
Expand Down
5 changes: 3 additions & 2 deletions src/libtrx/include/libtrx/gfx/2d/2d_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

typedef struct {
uint8_t r, g, b;
} GFX_PALETTE_ENTRY;
} GFX_COLOR;

typedef enum {
GFX_2D_EFFECT_NONE = 0,
Expand All @@ -32,8 +32,9 @@ void GFX_2D_Renderer_Upload(
GFX_2D_RENDERER *renderer, GFX_2D_SURFACE_DESC *desc, const uint8_t *data);

void GFX_2D_Renderer_SetPalette(
GFX_2D_RENDERER *renderer, const GFX_PALETTE_ENTRY *palette);
GFX_2D_RENDERER *renderer, const GFX_COLOR *palette);
void GFX_2D_Renderer_SetRepeat(GFX_2D_RENDERER *renderer, int32_t x, int32_t y);
void GFX_2D_Renderer_SetEffect(GFX_2D_RENDERER *renderer, GFX_2D_EFFECT filter);
void GFX_2D_Renderer_SetTint(GFX_2D_RENDERER *renderer, GFX_COLOR color);

void GFX_2D_Renderer_Render(GFX_2D_RENDERER *renderer);
4 changes: 2 additions & 2 deletions src/tr2/decomp/fmv.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ static void M_Play(const char *const file_name)

// Populate the palette with a palette corresponding to
// AV_PIX_FMT_RGB8
GFX_PALETTE_ENTRY palette[256];
GFX_COLOR palette[256];
for (int32_t i = 0; i < 256; i++) {
GFX_PALETTE_ENTRY *const col = &palette[i];
GFX_COLOR *const col = &palette[i];
col->r = 0x24 * (i >> 5);
col->g = 0x24 * ((i >> 2) & 7);
col->b = 0x55 * (i & 3);
Expand Down
7 changes: 0 additions & 7 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,6 @@ static void M_LoadDepthQ(VFILE *const file)
}
}

g_IsWet = 0;
for (int32_t i = 0; i < 256; i++) {
g_WaterPalette[i].red = g_GamePalette8[i].red * 2 / 3;
g_WaterPalette[i].green = g_GamePalette8[i].green * 2 / 3;
g_WaterPalette[i].blue = g_GamePalette8[i].blue;
}

Benchmark_End(benchmark, NULL);
}

Expand Down
1 change: 1 addition & 0 deletions src/tr2/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ void __cdecl Output_LightRoom(ROOM *const room)
void __cdecl Output_SetupBelowWater(const bool is_underwater)
{
g_IsWet = is_underwater;
Render_SetWet(is_underwater);
g_IsWaterEffect = true;
g_IsWibbleEffect = !is_underwater;
g_IsShadeEffect = true;
Expand Down
8 changes: 8 additions & 0 deletions src/tr2/game/render/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,11 @@ void Render_InsertSprite(
r->InsertSprite(r, z, x0, y0, x1, y1, sprite_idx, shade);
}
}

void Render_SetWet(const bool is_wet)
{
RENDERER *const r = M_GetRenderer();
if (r->SetWet != NULL) {
r->SetWet(r, is_wet);
}
}
1 change: 1 addition & 0 deletions src/tr2/game/render/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void Render_DrawBlackRectangle(int32_t opacity);

void Render_ClearZBuffer(void);
void Render_EnableZBuffer(bool z_write_enable, bool z_test_enable);
void Render_SetWet(bool is_wet);

// TODO: there's too much repetition for these
const int16_t *Render_InsertObjectG3(
Expand Down
2 changes: 1 addition & 1 deletion src/tr2/game/render/hwr.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ static void M_InsertSprite_Sorted(
}

const bool old_shade = g_IsShadeEffect;
g_IsShadeEffect = 0;
g_IsShadeEffect = false;
M_InsertPolyTextured(num_points, z, POLY_HWR_WGTMAP, sprite->tex_page);
g_IsShadeEffect = old_shade;
}
Expand Down
1 change: 1 addition & 0 deletions src/tr2/game/render/priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct RENDERER {
void (*ClearZBuffer)(struct RENDERER *);
void (*EnableZBuffer)(struct RENDERER *, bool, bool);
void (*DrawPolyList)(struct RENDERER *);
void (*SetWet)(struct RENDERER *, bool);

const int16_t *(*InsertGT4)(
struct RENDERER *renderer, const int16_t *obj_ptr, int32_t num,
Expand Down
15 changes: 14 additions & 1 deletion src/tr2/game/render/swr.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct {
GFX_2D_RENDERER *renderer_2d;
GFX_2D_SURFACE *surface;
GFX_2D_SURFACE *surface_alpha;
GFX_PALETTE_ENTRY palette[256];
GFX_COLOR palette[256];
} M_PRIV;

static VERTEX_INFO m_VBuffer[32] = { 0 };
Expand Down Expand Up @@ -1401,6 +1401,18 @@ static void M_DrawPolyList(RENDERER *const renderer)
GFX_2D_Renderer_Render(priv->renderer_2d);
}

static void M_SetWet(RENDERER *const renderer, const bool is_wet)
{
M_PRIV *const priv = renderer->priv;
if (is_wet) {
GFX_2D_Renderer_SetTint(
priv->renderer_2d, (GFX_COLOR) { .r = 170, .g = 170, .b = 255 });
} else {
GFX_2D_Renderer_SetTint(
priv->renderer_2d, (GFX_COLOR) { .r = 255, .g = 255, .b = 255 });
}
}

static const int16_t *M_InsertObjectG3(
RENDERER *const renderer, const int16_t *obj_ptr, const int32_t num,
const SORT_TYPE sort_type)
Expand Down Expand Up @@ -2257,6 +2269,7 @@ void Renderer_SW_Prepare(RENDERER *const renderer)
renderer->ResetPolyList = NULL;
renderer->EnableZBuffer = NULL;
renderer->ClearZBuffer = NULL;
renderer->SetWet = M_SetWet;

renderer->InsertObjectG3 = M_InsertObjectG3;
renderer->InsertObjectG4 = M_InsertObjectG4;
Expand Down

0 comments on commit db3026c

Please sign in to comment.