Skip to content

Fix copy-paste bug in BumpMapEffect::ComputeTangent#151

Open
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
CodeReclaimers:fix/bumpmap-copypaste
Open

Fix copy-paste bug in BumpMapEffect::ComputeTangent#151
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
CodeReclaimers:fix/bumpmap-copypaste

Conversation

@CodeReclaimers
Copy link
Contributor

Summary

  • Length(deltaPos1) is tested twice on line 168; second check should test Length(deltaPos2). Without this, a zero-length deltaPos2 is not detected and the function proceeds with a degenerate tangent frame.
-    if (Length(deltaPos1) <= epsilon || Length(deltaPos1) <= epsilon )
+    if (Length(deltaPos1) <= epsilon || Length(deltaPos2) <= epsilon)

Note: This is a compiled Graphics source file, so a standalone header-only test is not feasible.

Length(deltaPos1) is tested twice on line 168; second check should
test Length(deltaPos2). Without this, a zero-length deltaPos2 is
not detected and the function proceeds with a degenerate tangent frame.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@CodeReclaimers
Copy link
Contributor Author

Test program

#include <Mathematics/Vector3.h>
#include <Mathematics/Vector2.h>
#include <cstdlib>
#include <iostream>

using namespace gte;

static bool DetectDegenerate_Buggy(
    Vector3<float> const& position0,
    Vector3<float> const& position1,
    Vector3<float> const& position2)
{
    Vector3<float> deltaPos1 = position1 - position0;
    Vector3<float> deltaPos2 = position2 - position0;
    float const epsilon = 1e-08f;
    if (Length(deltaPos1) <= epsilon || Length(deltaPos1) <= epsilon)
        return true;
    return false;
}

static bool DetectDegenerate_Fixed(
    Vector3<float> const& position0,
    Vector3<float> const& position1,
    Vector3<float> const& position2)
{
    Vector3<float> deltaPos1 = position1 - position0;
    Vector3<float> deltaPos2 = position2 - position0;
    float const epsilon = 1e-08f;
    if (Length(deltaPos1) <= epsilon || Length(deltaPos2) <= epsilon)
        return true;
    return false;
}

int main()
{
    Vector3<float> p0 = { 0.0f, 0.0f, 0.0f };
    Vector3<float> p1 = { 1.0f, 0.0f, 0.0f };
    Vector3<float> p2 = { 0.0f, 0.0f, 0.0f };  // same as p0

    bool buggyDetected = DetectDegenerate_Buggy(p0, p1, p2);
    bool fixedDetected = DetectDegenerate_Fixed(p0, p1, p2);

    if (!fixedDetected || buggyDetected)
    {
        std::cerr << "FAIL" << std::endl;
        return EXIT_FAILURE;
    }

    std::cout << "PASS: fixed version correctly detects zero-length deltaPos2" << std::endl;
    return EXIT_SUCCESS;
}

Before fix: Buggy detects degenerate: no — zero-length deltaPos2 not detected
After fix: Fixed detects degenerate: yes — correctly returns false for degenerate triangle

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.

1 participant