Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[graphics] Fix issue with envmap math #3862

Merged
merged 3 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions common/custom_data/TFrag3Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ math::Vector3f vopmsub(math::Vector3f acc, math::Vector3f a, math::Vector3f b) {
}

/*!
* Compute the normal transformation for a TIE from the TIE matrix.
* Note that this isn't identical to the original game - we're missing the vf14 scaling factor
* For now, I just set this to 1, then normalize in the shader. Though I think we could avoid
* this by figuring out the value of vf14 here (I am just too lazy right now).
* Compute the normal transformation for a TIE from the TIE matrix. This will return properly scaled
* normals.
*/
std::array<math::Vector3f, 3> tie_normal_transform_v2(const std::array<math::Vector4f, 4>& m) {
// let:
Expand Down Expand Up @@ -186,11 +184,21 @@ std::array<math::Vector3f, 3> tie_normal_transform_v2(const std::array<math::Vec
// sqc2 vf12, -80(t8)
}

s16 saturate_for_s10(s16 s10) {
// our error should be 1 or less as an s8, or 4 as a s10.
ASSERT(s10 >= -520 && s10 < 520);
if (s10 < -512) {
return -512;
}
if (s10 > 511) {
return 511;
}
return s10;
}

u32 pack_to_gl_normal(s16 nx, s16 ny, s16 nz) {
ASSERT(nx >= -512 && nx <= 511);
ASSERT(ny >= -512 && ny <= 511);
ASSERT(nz >= -512 && nz <= 511);
return (nx & 0x3ff) | ((ny & 0x3ff) << 10) | ((nz & 0x3ff) << 20);
return (saturate_for_s10(nx) & 0x3ff) | ((saturate_for_s10(ny) & 0x3ff) << 10) |
((saturate_for_s10(nz) & 0x3ff) << 20);
}

/*!
Expand All @@ -202,10 +210,10 @@ u32 unpack_tie_normal(const std::array<math::Vector3f, 3>& mat, s8 nx, s8 ny, s8
nrm += mat[0] * nx;
nrm += mat[1] * ny;
nrm += mat[2] * nz;
// convert to s16 for OpenGL renderer
// nrm /= 0x100; // number from EE asm
// nrm *= 0x200; // for normalized s10 -> float conversion by OpenGL.
nrm *= 2; // for normalized s10 -> float conversion by OpenGL.

// game used signed 8-bit normals, but OpenGL uses signed 10-bit
// multiply by 2^2 = 4
nrm *= 4;

auto as_int = nrm.cast<s16>();

Expand Down
1 change: 0 additions & 1 deletion game/graphics/opengl_renderer/shaders/etie.vert
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ void main() {
vec3 nrm_vf23 = cam_no_persp[0].xyz * normal.x
+ cam_no_persp[1].xyz * normal.y
+ cam_no_persp[2].xyz * normal.z;
vec3 r_nrm = nrm_vf23;

// transform the point
vec4 vf17 = cam_no_persp[3];
Expand Down
Loading