Skip to main content
added 3 characters in body
Source Link
Julien Guertault
  • 4.4k
  • 2
  • 24
  • 47

Without looking at the code, from the look of the pictures, my intuition would be that somewhere the code is using the inverse of the matrix it should.

Looking at the code seems to confirm so, but there are some other problems too.

mTBN = transpose(mat3(t,b,n)); //transpose makes it from model to to tangent space?

mat3(t,b,n) is a transform from world space to tangent space. The inverse of such a matrix is indeed its transpose, so mTBN transforms from tangent space to world space, which is not what you want.

tanViewPos = mTBN * cameraPos;
tanFragPos = mTBN * worldPos.xyz;

You cannot do something like that. mTBN is a 3x3 matrix: without a 4th column and a non-zero homogeneous coordinate, you can only transform directions, not positions. You need to first get the camera direction relative to the vertex, then transform that direction.

// Beware of the vector direction.
// Here viewDir points from the object toward the viewer.
vec3 worldCameraDirworldViewDir = normalize(vPositioncameraPos - cameraPosworldPos.xyz);
tanViewDir = mTBN * worldCameraDir;worldViewDir;

Let us know in comments if that helps solving your issue.

Without looking at the code, from the look of the pictures, my intuition would be that somewhere the code is using the inverse of the matrix it should.

Looking at the code seems to confirm so, but there are some other problems too.

mTBN = transpose(mat3(t,b,n)); //transpose makes it from model to to tangent space?

mat3(t,b,n) is a transform from world space to tangent space. The inverse of such a matrix is indeed its transpose, so mTBN transforms from tangent space to world space, which is not what you want.

tanViewPos = mTBN * cameraPos;
tanFragPos = mTBN * worldPos.xyz;

You cannot do something like that. mTBN is a 3x3 matrix: without a 4th column and a non-zero homogeneous coordinate, you can only transform directions, not positions. You need to first get the camera direction relative to the vertex, then transform that direction.

vec3 worldCameraDir = normalize(vPosition - cameraPos);
tanViewDir = mTBN * worldCameraDir;

Let us know in comments if that helps solving your issue.

Without looking at the code, from the look of the pictures, my intuition would be that somewhere the code is using the inverse of the matrix it should.

Looking at the code seems to confirm so, but there are some other problems too.

mTBN = transpose(mat3(t,b,n)); //transpose makes it from model to to tangent space?

mat3(t,b,n) is a transform from world space to tangent space. The inverse of such a matrix is indeed its transpose, so mTBN transforms from tangent space to world space, which is not what you want.

tanViewPos = mTBN * cameraPos;
tanFragPos = mTBN * worldPos.xyz;

You cannot do something like that. mTBN is a 3x3 matrix: without a 4th column and a non-zero homogeneous coordinate, you can only transform directions, not positions. You need to first get the camera direction relative to the vertex, then transform that direction.

// Beware of the vector direction.
// Here viewDir points from the object toward the viewer.
vec3 worldViewDir = normalize(cameraPos - worldPos.xyz);
tanViewDir = mTBN * worldViewDir;

Let us know in comments if that helps solving your issue.

Source Link
Julien Guertault
  • 4.4k
  • 2
  • 24
  • 47

Without looking at the code, from the look of the pictures, my intuition would be that somewhere the code is using the inverse of the matrix it should.

Looking at the code seems to confirm so, but there are some other problems too.

mTBN = transpose(mat3(t,b,n)); //transpose makes it from model to to tangent space?

mat3(t,b,n) is a transform from world space to tangent space. The inverse of such a matrix is indeed its transpose, so mTBN transforms from tangent space to world space, which is not what you want.

tanViewPos = mTBN * cameraPos;
tanFragPos = mTBN * worldPos.xyz;

You cannot do something like that. mTBN is a 3x3 matrix: without a 4th column and a non-zero homogeneous coordinate, you can only transform directions, not positions. You need to first get the camera direction relative to the vertex, then transform that direction.

vec3 worldCameraDir = normalize(vPosition - cameraPos);
tanViewDir = mTBN * worldCameraDir;

Let us know in comments if that helps solving your issue.