Skip to content

Commit

Permalink
output: fix water wibble effect
Browse files Browse the repository at this point in the history
Resolves #1493
  • Loading branch information
rr- committed Oct 4, 2024
1 parent bf4bfbf commit 336ef90
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/tr1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- fixed `/endlevel` displaying a success message in the title screen
- fixed Story So Far feature looping cutscenes forever (#1551, regression from 4.4)
- improved object name matching in console commands to work like TR2X
- improved vertex movement when looking through water portals even more (#1493)

## [4.4](https://github.com/LostArtefacts/TRX/compare/tr1-4.3...tr1-4.4) - 2024-09-20
- added `/exit` command (#1462)
Expand Down
12 changes: 12 additions & 0 deletions src/libtrx/gfx/3d/3d_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ void GFX_3D_Renderer_SetTextureFilter(
filter == GFX_TF_BILINEAR);
}

void GFX_3D_Renderer_SetDepthWritesEnabled(
GFX_3D_RENDERER *const renderer, const bool is_enabled)
{
assert(renderer);
GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream);
if (is_enabled) {
glDepthMask(GL_TRUE);
} else {
glDepthMask(GL_FALSE);
}
}

void GFX_3D_Renderer_SetDepthTestEnabled(
GFX_3D_RENDERER *renderer, bool is_enabled)
{
Expand Down
2 changes: 2 additions & 0 deletions src/libtrx/include/libtrx/gfx/3d/3d_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void GFX_3D_Renderer_SetPrimType(
GFX_3D_RENDERER *renderer, GFX_3D_PRIM_TYPE value);
void GFX_3D_Renderer_SetTextureFilter(
GFX_3D_RENDERER *renderer, GFX_TEXTURE_FILTER filter);
void GFX_3D_Renderer_SetDepthWritesEnabled(
GFX_3D_RENDERER *renderer, bool is_enabled);
void GFX_3D_Renderer_SetDepthTestEnabled(
GFX_3D_RENDERER *renderer, bool is_enabled);
void GFX_3D_Renderer_SetBlendingMode(
Expand Down
88 changes: 67 additions & 21 deletions src/tr1/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static const int16_t *M_CalcVerticeLight(const int16_t *obj_ptr);
static const int16_t *M_CalcVerticeEnvMap(const int16_t *obj_ptr);
static const int16_t *M_CalcSkyboxLight(const int16_t *obj_ptr);
static const int16_t *M_CalcRoomVertices(const int16_t *obj_ptr);
static const int16_t *M_CalcRoomVerticesWibble(const int16_t *obj_ptr);
static int32_t M_CalcFogShade(int32_t depth);
static void M_CalcWibbleTable(void);

Expand Down Expand Up @@ -406,9 +407,9 @@ static const int16_t *M_CalcSkyboxLight(const int16_t *obj_ptr)

static const int16_t *M_CalcRoomVertices(const int16_t *obj_ptr)
{
int32_t vertex_count = *obj_ptr++;
const int32_t vertex_count = *obj_ptr++;

for (int i = 0; i < vertex_count; i++) {
for (int32_t i = 0; i < vertex_count; i++) {
PHD_VBUF *const vbuf = &m_VBuf[i];

// clang-format off
Expand All @@ -433,35 +434,31 @@ static const int16_t *M_CalcRoomVertices(const int16_t *obj_ptr)
const double zv = zv_int;
// clang-format on

m_VBuf[i].xv = xv;
m_VBuf[i].yv = yv;
m_VBuf[i].zv = zv;
m_VBuf[i].g = obj_ptr[3] & MAX_LIGHTING;
vbuf->xv = xv;
vbuf->yv = yv;
vbuf->zv = zv;
vbuf->g = obj_ptr[3] & MAX_LIGHTING;

if (zv < Output_GetNearZ()) {
m_VBuf[i].clip = 0x8000;
vbuf->clip = 0x8000;
} else {
int16_t clip_flags = 0;
const int32_t depth = zv_int >> W2V_SHIFT;
if (depth > Output_GetDrawDistMax()) {
m_VBuf[i].g = MAX_LIGHTING;
vbuf->g = MAX_LIGHTING;
if (!m_IsSkyboxEnabled) {
clip_flags |= 16;
}
} else if (depth) {
m_VBuf[i].g += M_CalcFogShade(depth);
vbuf->g += M_CalcFogShade(depth);
if (!m_IsWaterEffect) {
CLAMPG(m_VBuf[i].g, MAX_LIGHTING);
CLAMPG(vbuf->g, MAX_LIGHTING);
}
}

const double persp = g_PhdPersp / (double)zv;
double xs = Viewport_GetCenterX() + xv * persp;
double ys = Viewport_GetCenterY() + yv * persp;
if (m_IsWibbleEffect && !(obj_ptr[3] & NO_VERT_MOVE)) {
xs += m_WibbleTable[(m_WibbleOffset + (int)ys) & 0x1F];
ys += m_WibbleTable[(m_WibbleOffset + (int)xs) & 0x1F];
}
const double xs = Viewport_GetCenterX() + xv * persp;
const double ys = Viewport_GetCenterY() + yv * persp;

if (xs < g_PhdLeft) {
clip_flags |= 1;
Expand All @@ -476,23 +473,61 @@ static const int16_t *M_CalcRoomVertices(const int16_t *obj_ptr)
}

if (m_IsWaterEffect) {
m_VBuf[i].g += m_ShadeTable[(
vbuf->g += m_ShadeTable[(
((uint8_t)m_WibbleOffset
+ (uint8_t)m_RandTable[(vertex_count - i) % WIBBLE_SIZE])
% WIBBLE_SIZE)];
CLAMP(m_VBuf[i].g, 0, 0x1FFF);
CLAMP(vbuf->g, 0, 0x1FFF);
}

m_VBuf[i].xs = xs;
m_VBuf[i].ys = ys;
m_VBuf[i].clip = clip_flags;
vbuf->xs = xs;
vbuf->ys = ys;
vbuf->clip = clip_flags;
}
obj_ptr += 4;
}

return obj_ptr;
}

static const int16_t *M_CalcRoomVerticesWibble(const int16_t *obj_ptr)
{
const int32_t vertex_count = *obj_ptr++;

for (int32_t i = 0; i < vertex_count; i++) {
PHD_VBUF *const vbuf = &m_VBuf[i];
if (obj_ptr[3] & NO_VERT_MOVE) {
obj_ptr += 4;
continue;
}

double xs = vbuf->xs;
double ys = vbuf->ys;
xs += m_WibbleTable[(m_WibbleOffset + (int)ys) & (WIBBLE_SIZE - 1)];
ys += m_WibbleTable[(m_WibbleOffset + (int)xs) & (WIBBLE_SIZE - 1)];

int16_t clip_flags = vbuf->clip & ~15;
if (xs < g_PhdLeft) {
clip_flags |= 1;
} else if (xs > g_PhdRight) {
clip_flags |= 2;
}

if (ys < g_PhdTop) {
clip_flags |= 4;
} else if (ys > g_PhdBottom) {
clip_flags |= 8;
}

vbuf->xs = xs;
vbuf->ys = ys;
vbuf->clip = clip_flags;
obj_ptr += 4;
}

return obj_ptr;
}

static int32_t M_CalcFogShade(int32_t depth)
{
int32_t fog_begin = Output_GetDrawDistFade();
Expand Down Expand Up @@ -762,7 +797,18 @@ void Output_DrawSkybox(const int16_t *obj_ptr)

void Output_DrawRoom(const int16_t *obj_ptr)
{
const int16_t *const old_obj_ptr = obj_ptr;

obj_ptr = M_CalcRoomVertices(obj_ptr);

if (m_IsWibbleEffect) {
S_Output_DisableDepthWrites();
obj_ptr = M_DrawObjectGT4(obj_ptr + 1, *obj_ptr);
obj_ptr = M_DrawObjectGT3(obj_ptr + 1, *obj_ptr);
S_Output_EnableDepthWrites();
obj_ptr = M_CalcRoomVerticesWibble(old_obj_ptr);
}

obj_ptr = M_DrawObjectGT4(obj_ptr + 1, *obj_ptr);
obj_ptr = M_DrawObjectGT3(obj_ptr + 1, *obj_ptr);
obj_ptr = M_DrawRoomSprites(obj_ptr + 1, *obj_ptr);
Expand Down
10 changes: 10 additions & 0 deletions src/tr1/specific/s_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,16 @@ void S_Output_DisableTextureMode(void)
GFX_3D_Renderer_SetTexturingEnabled(m_Renderer3D, m_IsTextureMode);
}

void S_Output_EnableDepthWrites(void)
{
GFX_3D_Renderer_SetDepthWritesEnabled(m_Renderer3D, true);
}

void S_Output_DisableDepthWrites(void)
{
GFX_3D_Renderer_SetDepthWritesEnabled(m_Renderer3D, false);
}

void S_Output_EnableDepthTest(void)
{
GFX_3D_Renderer_SetDepthTestEnabled(m_Renderer3D, true);
Expand Down
2 changes: 2 additions & 0 deletions src/tr1/specific/s_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ void S_Output_Shutdown(void);

void S_Output_EnableTextureMode(void);
void S_Output_DisableTextureMode(void);
void S_Output_EnableDepthWrites(void);
void S_Output_DisableDepthWrites(void);
void S_Output_EnableDepthTest(void);
void S_Output_DisableDepthTest(void);

Expand Down

0 comments on commit 336ef90

Please sign in to comment.