0

I my code I am currently working on, I have to transform a vector in my fragment shader to another coordinate space (simliar to the transformation between the cameras space and the lights space with shadow mapping), do some calculations in this coordinate space, and transform it back.

Since the results were not satisfying, and I was searching for the cause, in the end I checked if I just transform a vector, and then transform it back with the inverse of the transformation matrix it is equal to the original vector.

Something like this:

vec4 transformedVec4 = transformMat4 * originalVec4;
vec4 ReTransformedVec4 = inverse(transformMat4) * transformedVec4;

if(transformedVec4 == ReTransformedVec4)
{
    //some code to display if equal
}

I was really surprised to see, that my code never enters the if-statement.

Does anybody know where this comes from? Is it simply the floating point precision, that causes the results to be unequal?

nurgan
  • 309
  • 1
  • 4
  • 22

1 Answers1

4

yes it is a combination of the floating point and the imprecise nature of inverting a transformation matrix (inverse(transformMat4)*transformMat4 is not exactly the identity)

if you want to know closeness then you can use the distance between the vectors

if(distance(transformedVec4,ReTransformedVec4) <0.01)
{
    //some code to display if equal
}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106