Saturday, November 16, 2019

OpenGL Bump Map -- Texture artifacts ?


I am learning OpenGL (and learning the math behind it) and I'm making a simple OBJ viewer, nothing fancy. I have diffuse, specular and ambient light/texture working fine and now I am implementing the bump mapping. I based my code on what I learned with the OpenGL 4.3 Redbook and "Mathematics for 3D game programming and computer graphics" book.


There is probably something I didn't understand correctly, I just hope you will be able to help me :)


Anyway this is the result I get (from a free asset used just for testing -- the results are fine in blender) : Results


This is the diffuse texture and the bump map : Diffuse texture Bump texture


Fragment shader code :


void main() {
vec3 normalDirection = normalize(tangenteSpace*(texture2D( bump_tex, f_texcoord ).rgb*2.0 - 1.0));
//vec3 normalDirection = normalize(mat3(invTransp)*vertNormal);
vec3 viewDirection = tangenteSpace*normalize(vec3(invView * vec4(0.0, 0.0, 0.0, 1.0) - position));


float attenuation = 1.0;//none
vec3 vertToLightSource = vec3(light.position - position*light.position.w);
float distance = length(vertToLightSource);
attenuation = mix(1.0,1.0/(light.constantAttenuation +
light.linearAttenuation *distance +
light.quadraticAttenuation*distance*distance),light.position.w);
vec3 lightDirection = tangenteSpace*(vertToLightSource/distance);

//spotlight if spotCutoff <= 90

float clampedCosine = max(0.0, dot(-lightDirection, normalize(light.spotDirection)));
float tempMix = mix(attenuation * pow(clampedCosine, light.spotExponent),0.0,clampedCosine < cos(light.spotCutoff * 3.14159 / 180.0));//if outside of spotlight nothing
attenuation = mix(attenuation,tempMix,light.spotCutoff <= 90.0);

//should there be any attenuation ?
attenuation = mix(attenuation,1.0,light.position.w);

//------------------------------ ambiant light
vec3 ambientLight = vec3(ambientScene)*vec3(mat.Ka);


//------------------------------ specular light
//get the reflection
vec4 specularMapPixel = texture2D(spec_tex, f_texcoord).rgba;
vec3 specularColor = specularMapPixel.rgb;
float shininess = specularMapPixel.a;
vec3 specularReflection = mix(
attenuation * vec3(light.specular) * vec3(mat.Ks)* pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)),mat.Ns*shininess) *specularColor.rgb,
vec3(0.0, 0.0, 0.0),
dot(normalDirection, lightDirection) < 0.0
);


vec3 diffuseReflection = attenuation*
vec3(light.diffuse)*vec3(mat.Kd)
*max(0.0,dot(normalDirection,lightDirection));

vec4 Color = vec4(vec4(specularReflection,1.0) + vec4(ambientLight,1)*texture2D(amb_tex, f_texcoord) + vec4(diffuseReflection,1.0)*texture2D(dif_tex, f_texcoord));

FragColor = Color;
}


Vertex Shader Code :


void main()
{
//comnpute tangent space
tangenteSpace[0] = mat3(mv)*normalize(tangent.xyz);
tangenteSpace[2] = mat3(mv)*normalize(vertNormal);
tangenteSpace[1] = mat3(mv)*normalize(cross(tangenteSpace[0],tangenteSpace[2])*tangent.w);
tangenteSpace = transpose(tangenteSpace);

//position in world space

position = m * vertCoord;
f_texcoord = vec2(texCoord);

gl_Position = mvp * vertCoord;
}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...