Sign in to follow this  

Space conversions (getting everything properly to tangent space)

This topic is 1812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get parallax mapping working and no matter how much I try, the effect ends up being directional. I'm therefore somewhat sure that that I've messed up my tangent space conversions somewhere in the vertex shader. I've cleaned it up to make the code a bit more clear. Here's my (preliminary) test code for the vertex shader. Can anyone point out if there's something wrong with it?

 

 

 
vec3 gdConvertWorld2Object(in vec3 vec)
{
    return vec3(vec4(vec, 1) * inverse(gd_matModel));
}
 
vec3 gdConvertWorld2View(in vec3 vec)
{
    return vec3(gd_matModelView * vec4(vec, 1));
}
 
vec3 gdConvertView2Tangent(in vec3 vec)
{
    mat3 tbn = mat3(gd_InTangent, cross(gd_InNormal, gd_InTangent), gd_InNormal); //TODO: I know - it's missing handedness!
    mat3 tbni = inverse(tbn);
 
    return tbni * (inverse(gd_matModelView) * vec4(vec, 0.0)).xyz;
}
 
vec3 gdConvertObject2Tangent(in vec3 vec)
{
    vec3 v = vec3(gd_matModel * vec4(vec, 1.0)); //convert vertex to world space
    
    mat3 tbn = mat3(gd_InTangent, cross(gd_InNormal, gd_InTangent), gd_InNormal); //TODO: I know - it's missing handedness!
    mat3 tbni = inverse(tbn);
 
    return tbni * (inverse(gd_matModel) * vec4(v, 0)).xyz;
}
 
 
in vec3 vLightPositionWS;
out vec3 vecLightTangent;
out vec3 vecCameraTangent;
 
void main()
{
//gd_InVertex is in object space
vecLightTangent = gdConvertObject2Tangent(gdConvertWorld2Object(vLightPositionWS) - gd_InVertex);
vecCameraTangent = gdConvertObject2Tangent(gdConvertWorld2Object(gd_vecEyePositionWS.xyz) - gd_InVertex);
}
Edited by irreversible

Share this post


Link to post
Share on other sites
Sign in to follow this