• Create Account

## View space bump mapping refuses to work

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1luorax  Members

Posted 23 July 2014 - 06:47 AM

Recently I was implementing an SSAO algorithm, for which my dummy scene was not enough, so I decided I'd replace it with the popular 'Sponza' model. As I loaded it in, I noticed that my bump mapping implementation wasn't working properly, so I decided I'd fix it. After roughly 10 hours, and after having tested every possible and impossible combination of calculations, I decided I'd ask for help, because I clearly cannot see what's wrong with my code.

I'm using a deferred renderer, do all the lighting calculations in view/camera space, so I store view space normals in the GBuffer. I'm using Assimp for tangent calculation.

Here are a few screenshots of my shader's output:

Lit scene /w bump mapping: https://docs.google.com/file/d/0BzrnpA5nbZMJUkQ3U2I0N0NCR0U/edit

Lit scene /w bump mapping and reversing normal when dot(normal,bumpedNormal)<0.0: https://docs.google.com/file/d/0BzrnpA5nbZMJeldHamZkckc3bzg/edit

The results are clearly wrong, and even when normals are reversed, only a change in light angle happens.

// ... Deferred.glsl
//////////////////////////////////////////////////
//  Helpers
struct BasicData
{
vec2 uv;
vec3 posWorld;
vec3 normalCamera;
vec4 prevPos;
vec4 currentPos;
mat3 tbn;
};

//////////////////////////////////////////////////
//  Base pass
in vec4 vNormal: ATTRIBUTE_LOCATION_NORMAL,
in vec4 vTangent: ATTRIBUTE_LOCATION_TANGENT,
in vec2 vUv: ATTRIBUTE_LOCATION_UV,
out BasicData fromVS)
{
fromVS.posWorld=(modelData.mModel*vPos).xyz;
fromVS.currentPos=cameraParameters.mViewProjection*vec4(fromVS.posWorld,1);
fromVS.prevPos=previousCameraParameters.mViewProjection*previousModelData.mModel*vPos;

fromVS.normalCamera=normalize((modelData.mNormal*vNormal).xyz);
vec3 tangent=normalize((modelData.mNormal*vTangent).xyz);
tangent=normalize(tangent-dot(tangent,fromVS.normalCamera)*fromVS.normalCamera);
vec3 bitangent=cross(tangent,fromVS.normalCamera);
fromVS.tbn=mat3(tangent,bitangent,fromVS.normalCamera);

fromVS.uv=vUv;

gl_Position=fromVS.currentPos;
}

out vec4 albedo: 0,
out vec4 normal: 1,
out vec4 velocity: 2)
{
vec2 uv=fromVS.uv;

//////////////////////////////////////////////////
//  Bump & parallax mapping
if (material.bHasBumpMap==1.0)
{
// never evaluates to true as .mtl doesn't support displacement textures
if (material.bHasParallaxMap==1.0)
uv=getDisplacedUV(uv,normalize(cameraParameters.vEye-fromVS.posWorld),fromVS.tbn);

normal.rgb=getBumpedNormal(fromVS.tbn,uv);

if (dot(normal.rgb,fromVS.normalCamera.rgb)<0.0)
normal.rgb=-normal.rgb;
}
else
{
normal.rgb=normalize(fromVS.normalCamera);
}

//////////////////////////////////////////////////
//  Albedo
if (material.bHasDiffuse==1.0)
albedo.rgb=textureGamma(sDiffuse,uv).rgb;
else
albedo.rgb=vec3(1,1,1);

//////////////////////////////////////////////////
//  Specular
if (material.bHasSpecularMap==1.0)
{
albedo.a=2.0+material.fGlossiness/256.0;
normal.a=textureGamma(sSpecularMap,uv).r;
}
else
{
albedo.a=material.fGlossiness/256.0;
}

//albedo.rgb=abs(normal.rgb);
//normal.rgb=normalize(fromVS.normalCamera);

//////////////////////////////////////////////////
//  Velocity
vec2 currentPos=fromVS.currentPos.xy/fromVS.currentPos.w;
vec2 prevPos=fromVS.prevPos.xy/fromVS.prevPos.w;
velocity=packVelocityData(currentPos,prevPos,gl_FragCoord.z);
}

// ... Material.glsl

//////////////////////////////////////////////////
//  Bump mapping
vec3 getBumpedNormal(mat3 tbn, vec2 uv)
{
return normalize(tbn*(2.0*textureGamma(sBumpMap,uv).xyz-1.0));
}

The normals, tangents and the normal matrix are okay, as it can been on the screenshots (without bumping lighting is right), so the problem must be somewhere else.

Any help is greatly appreciated, I'd much prefer not to waste any more time on such a basic and easy feature/algorithm.

Edited by luorax, 23 July 2014 - 06:52 AM.

### #2alkisbkn  Members

Posted 23 July 2014 - 07:51 AM

I didn't have time to read through the entire code, but multiplying a tangent space normal map with the TBN matrix converts it to world space normal, unless I am mistaken.

Can you show us how you do your lighting?

### #3luorax  Members

Posted 23 July 2014 - 08:03 AM

TBN converts to whatever space the column vectors (tangent, bitangent, normals) are. In the above code they're multiplied by the normal matrix (inverse_transpose(view*model)), which takes them to view space, so that TBN should be tangent -> view. Lighting is also fine without normal mapping, or at least it seems so, which makes me pretty sure the problem is not related to the light calculation.

Just for the sake of completeness though, here is my point light code (the scene only has a few point lights I quickly put down to lit the 2 floors) - don't mind the variable name inconsistencies, I plan on making a refactor on some of these codes once I finish shadow mapping and such.

What's more interesting is that by looking at demo codes of various postprocessing effect I have on my HDD that came with an engine, I've found a couple of shaders that, aside from variable names, work exactly like my code, yet it works for them.

vec3 computePointLight(PointLight lightData,
vec4 worldPos,
vec4 eyePos,
vec3 normalCamera,
vec3 albedo,
vec3 specularColor,
float specularPower)
{
//////////////////////////////////////////////////
//  Make sure this fragment is lit
vec3 toLight=lightData.vSourceEye.xyz-eyePos.xyz;
float distSqr=dot(toLight,toLight);
float dist=sqrt(distSqr);
toLight/=dist;

return vec3(0.0);

//////////////////////////////////////////////////
//  Calculate light
float angle=max(dot(normalCamera,toLight),0.0);

float diffuseIntensity=angle*lightData.fDiffuseIntensity;
float specularIntensity=0.0;

float attenuation=1.0;
float attFactor=1.0f/(lightData.fAttenuationFactor+1.0f);

attenuation=1.0f/(attenuation*lightData.fAttenuationFactor+1.0f);
attenuation-=attFactor;
attenuation/=1.0f-attFactor;

if (lightData.fSpecularIntensity>0.0)
{
vec3 halfVector=normalize(toLight+normalize(-eyePos.xyz));

specularIntensity+=pow(max(dot(halfVector,normalCamera),0.0),specularPower)*lightData.fSpecularIntensity;
}

//////////////////////////////////////////////////

{
vec3 lightSpacePos=worldPos.xyz-lightData.vSourceEye.xyz;

else
}

//////////////////////////////////////////////////
//  Composite

}


Edited by luorax, 23 July 2014 - 08:09 AM.

### #4luorax  Members

Posted 23 July 2014 - 09:20 AM

Turns out the material has actual bump textures and not normal ones, so that's what causing the problems. I'll try to convert them to normal maps somehow and see if it works.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.