microsoft / DirectXMath

DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps
https://walbourn.github.io/introducing-directxmath/
MIT License
1.52k stars 234 forks source link

Wrong Vector Reflect Formula #106

Closed OdysseyGameWorks closed 4 years ago

OdysseyGameWorks commented 4 years ago

https://github.com/microsoft/DirectXMath/blob/31f80002fa22180883c5aef6d518c98b86f9a7ec/Inc/DirectXMathVector.inl#L7045

The correct formula will be : Result = ((2 * dot(Incident, Normal)) * Normal) - Incident

https://www.fabrizioduroni.it/2017/08/25/how-to-calculate-reflection-vector.html

walbourn commented 4 years ago

Hmmm..

This formula and implementation for the no-intrinsics version go way back to when it was xboxmath.

    XMVECTOR Result;

    // Result = Incident - (2 * dot(Incident, Normal)) * Normal
    Result = XMVector2Dot(Incident, Normal);
    Result = XMVectorAdd(Result, Result);
    Result = XMVectorNegativeMultiplySubtract(Result, Normal, Incident);

    return Result;

I'll have to track down where they got that formulation.

walbourn commented 4 years ago

If you look at wikipedia the canonical formula is what is used in DirectXMath.

Looking in Glassner, An Introduction to Ray Tracing, Academic Press (1989), pg. 132-133, the formulation is also in agreement with DirectXMath. See the scan attached for the full derivation.

GlassnerReflect

BTW, it's a classic book, I recommend it for any graphics programmer if you can find a copy