Jump to content

  • Log In with Google      Sign In   
  • Create Account


I CAN DO

Member Since 23 Mar 2005
Offline Last Active Dec 08 2013 10:13 AM

#5113849 GLSL BumpMap Math error

Posted by I CAN DO on 02 December 2013 - 03:16 PM

Come on guys.




#5113785 GLSL BumpMap Math error

Posted by I CAN DO on 02 December 2013 - 10:17 AM

I have a problem with OpenGL and GLSL to implement Bump Map.

Vertex shader:

#version 330 core



layout (std140) uniform Matrices {



mat4 projViewModelMatrix;

mat3 normalMatrix;

mat4 modelViewMatrix;

};



uniform mat4 View;

uniform vec3 LightPosition;

uniform vec3 CameraPosition;



in vec3 position;

in vec3 normal;

in vec2 texCoord;



out vec2 TexCoord;

out vec3 Normal;



// BUMP MAPPING

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

out vec3 lightVec;

out vec3 eyeVec;

in vec3 vTangent;

in vec3 bTangent;







void main()

{

Normal = normalize(normalMatrix * normal);

gl_Position = projViewModelMatrix * vec4(position, 1.0);

TexCoord = vec2(texCoord);



// BUMP MAPPING

vec3 n = normalize(normalMatrix * normal);

vec3 t = normalize(normalMatrix * vTangent);

vec3 b = normalize(normalMatrix * bTangent);



vec3 vVertex = vec3(modelViewMatrix * vec4(position, 1.0));

// vec3 tmpVec = LightPosition.xyz - vVertex.xyz;

vec3 tmpVec = (View * vec4(LightPosition, 1.0)).xyz - vVertex.xyz;



lightVec.x = dot(tmpVec, t);

lightVec.y = dot(tmpVec, b);

lightVec.z = dot(tmpVec, n);



tmpVec = -vVertex;





eyeVec.x = dot(tmpVec, t);

eyeVec.y = dot(tmpVec, b);

eyeVec.z = dot(tmpVec, n);





}

FragmentShader

#version 330
layout (std140) uniform Material {

vec4 diffuse;

vec4 ambient;

vec4 specular;

vec4 emissive;

float shininess;

int texCount;

};



uniform sampler2D texUnit;



in vec3 Normal;

in vec2 TexCoord;



out vec4 outputF;



//BUMP MAPPING ///////////////////////////////////////////



in vec3 lightVec;

in vec3 eyeVec;



uniform sampler2D normalMap;



uniform vec4 diffuseLight;

uniform vec4 ambientLight;

uniform vec4 specularLight;







main()

{

vec3 N2 = normalize(texture(normalMap, TexCoord).xyz * 2.0 - 1.0);

vec3 L2 = normalize(lightVec);

vec3 V2 = normalize(eyeVec);

vec3 R2 = normalize(-reflect(L2, N2));



float NdotL = max(0.0, dot(N2, L2));

float RdotV = max(0.0, dot(R2, V2));



// Compute final colours.

vec4 ambient = ambientLight * ambient;

vec4 diffuse = diffuseLight * diffuse * NdotL;

vec4 specular2 = specularLight * specular * (pow(RdotV, shininess) * 1);



vec4 base = texture2D(texUnit, TexCoord);



// Final colour.

outputF = base * ambient + base * diffuse + specular2;

}

There is some error there? I feel that the calculation of the direction of the light is wrong. Could it be?

 

The line:

vec3 tmpVec = (View * vec4(LightPosition, 1.0)).xyz - vVertex.xyz;

is OK?

the line:

tmpVec = -vVertex;

 

OK? or I need change to:

tmpVec = (View * vec4(CameraPosition, 1.0)).xyz - vVertex.xyz;

 

or

tmpVec = (CameraPosition).xyz - vVertex.xyz;

 

Please help me.

Thank you, best regards.

 

Attached Thumbnails

  • CarScreenshot4.jpg
  • CarScreenshot3.jpg
  • CarScreenshot2.jpg
  • CarScreenshot1.jpg



PARTNERS