Skip to content

Fog range starts and saturates at twice the declared distance - #83

Closed
zoncabe wants to merge 1 commit into
HailToDodongo:mainfrom
zoncabe:fix-fog-range
Closed

zoncabe wants to merge 1 commit into
HailToDodongo:mainfrom
zoncabe:fix-fog-range

Conversation

@zoncabe

@zoncabe zoncabe commented Sep 14, 2026

Copy link
Copy Markdown

t3d_fog_set_range doubles the offset and halves the scale. Both ends of the
range land at twice the value passed in.

tiny3d/src/t3d/t3d.c

Lines 568 to 569 in 73d822f

float scale = 16384.0f / diff;
float offset = -near * 2.0f;

Cause

Ucode computes (clipZ + offset) * scale and subtracts that from a full s16.
Zero fog needs clipZ == near, full fog needs the product to reach 32767. That
gives offset = -near and scale = 32768 / (far - near).

Current code halves one and doubles the other:

float scale = 16384.0f / diff;
float offset = -near * 2.0f;

Zero fog then lands at clipZ == 2*near, and saturation at 2*far.

Magma, in libdragon, runs the same computation on the same clip-space Z with
neither factor:

float factor = 1.0f / diff;
float offset = -parms->end;

Measurement

Four posts on one axis, 10 metres apart, FPS camera walking the line, both fog
distances printed on screen. Range set to 0-25 metres, fog saturates at 50.

Screenshots of both builds attached, before and after the fix. Both ROMs are
attached as well.

Fix

float scale = 32768.0f / diff;
float offset = -near;

Impact

Behaviour change, visible in every existing project. Every declared fog range
changes meaning, including the ones under examples/.

@HailToDodongo

Copy link
Copy Markdown
Owner

already fixed in PR #81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants