Skip to content

Commit

Permalink
gfx: fix wireframe mode discarding transparent pixels
Browse files Browse the repository at this point in the history
Resolves #2314.
  • Loading branch information
rr- committed Jan 16, 2025
1 parent 972d786 commit 4cc05f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
1 change: 1 addition & 0 deletions docs/tr1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- fixed textures animating during demo fade-outs (#2217, regression from 4.0)
- fixed waterfall mist not animating during demo (#2218, regression from 3.0)
- fixed sound option arrows disappearing with specific volumes chosen (#2295, regression from 2.7)
- fixed wireframe mode discarding transparent pixels (#2315, regression from 4.2)
- improved pause screen compatibility with PS1 (#2248)

## [4.7.1](https://github.com/LostArtefacts/TRX/compare/tr1-4.7...tr1-4.7.1) - 2024-12-21
Expand Down
1 change: 1 addition & 0 deletions docs/tr2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- fixed the passport object not being selected when exiting to title (#2192, regression from 0.8)
- fixed the upside-down camera fix to no longer limit Lara's vision (#2276, regression from 0.8)
- fixed /kill command freezing the game under rare circumstances (#2297, regression from 0.3)
- fixed wireframe mode discarding transparent pixels (#2315, regression from 0.7)

## [0.8](https://github.com/LostArtefacts/TRX/compare/tr2-0.8...tr2-0.8) - 2025-01-01
- completed decompilation efforts – TR2X.dll is gone, Tomb2.exe no longer needed (#1694)
Expand Down
76 changes: 48 additions & 28 deletions src/libtrx/gfx/3d/3d_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct GFX_3D_RENDERER {
GFX_GL_TEXTURE *env_map_texture;
int selected_texture_num;
GFX_BLEND_MODE selected_blend_mode;
bool alpha_point_discard;
float alpha_threshold;

// shader variable locations
GLint loc_mat_projection;
Expand All @@ -30,19 +32,51 @@ struct GFX_3D_RENDERER {
GLint loc_brightness_multiplier;
};

static void M_ApplyUniforms(GFX_3D_RENDERER *renderer);
static void M_Flush(GFX_3D_RENDERER *renderer);
static void M_SelectTextureImpl(GFX_3D_RENDERER *renderer, int texture_num);
static void M_RestoreTexture(GFX_3D_RENDERER *const renderer);

static void M_ApplyUniforms(GFX_3D_RENDERER *const renderer)
{
GFX_GL_Program_Bind(&renderer->program);
GFX_GL_Program_Uniform1f(
&renderer->program, renderer->loc_alpha_threshold,
renderer->config->enable_wireframe ? -1.0f : renderer->alpha_threshold);
GFX_GL_Program_Uniform1i(
&renderer->program, renderer->loc_alpha_point_discard,
!renderer->config->enable_wireframe && renderer->alpha_point_discard);
}

static void M_Flush(GFX_3D_RENDERER *const renderer)
{
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
glLineWidth(renderer->config->line_width);
GFX_GL_CheckError();
glPolygonMode(
GL_FRONT_AND_BACK,
renderer->config->enable_wireframe ? GL_LINE : GL_FILL);
GFX_GL_CheckError();

GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
if (renderer->config->enable_wireframe) {
glBlendFunc(GL_ONE, GL_ZERO);
} else {
switch (renderer->selected_blend_mode) {
case GFX_BLEND_MODE_OFF:
glBlendFunc(GL_ONE, GL_ZERO);
GFX_GL_CheckError();
break;
case GFX_BLEND_MODE_NORMAL:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GFX_GL_CheckError();
break;
case GFX_BLEND_MODE_MULTIPLY:
glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
GFX_GL_CheckError();
break;
}
}
M_ApplyUniforms(renderer);
}

static void M_SelectTextureImpl(
Expand Down Expand Up @@ -84,6 +118,8 @@ GFX_3D_RENDERER *GFX_3D_Renderer_Create(void)
for (int i = 0; i < GFX_MAX_TEXTURES; i++) {
renderer->textures[i] = NULL;
}
renderer->alpha_point_discard = false;
renderer->alpha_threshold = -1.0;

GFX_GL_Sampler_Init(&renderer->sampler);
GFX_GL_Sampler_Bind(&renderer->sampler, 0);
Expand Down Expand Up @@ -130,12 +166,9 @@ GFX_3D_RENDERER *GFX_3D_Renderer_Create(void)
GFX_GL_Program_UniformMatrix4fv(
&renderer->program, renderer->loc_mat_model_view, 1, GL_FALSE,
&model_view[0][0]);
GFX_GL_Program_Uniform1i(
&renderer->program, renderer->loc_alpha_point_discard, false);
GFX_GL_Program_Uniform1f(
&renderer->program, renderer->loc_alpha_threshold, -1.0);
GFX_GL_Program_Uniform1f(
&renderer->program, renderer->loc_brightness_multiplier, 1.0);
M_ApplyUniforms(renderer);

GFX_3D_VertexStream_Init(&renderer->vertex_stream);
return renderer;
Expand Down Expand Up @@ -163,6 +196,7 @@ void GFX_3D_Renderer_RenderBegin(GFX_3D_RENDERER *const renderer)
GFX_GL_Sampler_Bind(&renderer->sampler, 0);

M_RestoreTexture(renderer);
M_ApplyUniforms(renderer);

const float left = 0.0f;
const float top = 0.0f;
Expand Down Expand Up @@ -405,44 +439,30 @@ void GFX_3D_Renderer_SetBlendingMode(
if (renderer->selected_blend_mode == blend_mode) {
return;
}
M_Flush(renderer);

GFX_GL_Program_Bind(&renderer->program);
switch (blend_mode) {
case GFX_BLEND_MODE_OFF:
glBlendFunc(GL_ONE, GL_ZERO);
GFX_GL_CheckError();
break;
case GFX_BLEND_MODE_NORMAL:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GFX_GL_CheckError();
break;
case GFX_BLEND_MODE_MULTIPLY:
glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
GFX_GL_CheckError();
break;
}
renderer->selected_blend_mode = blend_mode;
M_Flush(renderer);
}

void GFX_3D_Renderer_SetAlphaPointDiscard(
GFX_3D_RENDERER *const renderer, const bool is_enabled)
{
ASSERT(renderer != NULL);
if (renderer->alpha_point_discard == is_enabled) {
return;
}
renderer->alpha_point_discard = is_enabled;
M_Flush(renderer);
GFX_GL_Program_Bind(&renderer->program);
GFX_GL_Program_Uniform1f(
&renderer->program, renderer->loc_alpha_point_discard, is_enabled);
}

void GFX_3D_Renderer_SetAlphaThreshold(
GFX_3D_RENDERER *const renderer, const float value)
{
ASSERT(renderer != NULL);
if (renderer->alpha_threshold == value) {
return;
}
renderer->alpha_threshold = value;
M_Flush(renderer);
GFX_GL_Program_Bind(&renderer->program);
GFX_GL_Program_Uniform1f(
&renderer->program, renderer->loc_alpha_threshold, value);
}

void GFX_3D_Renderer_SetBrightnessMultiplier(
Expand Down

0 comments on commit 4cc05f4

Please sign in to comment.