Parallax mapping bug (screens & code imcluded)

ok so this is my code:

vertex program:

// Uniform variables
uniform vec3 u_CameraPosition;

// Varying variables
varying float v_Distance;
varying vec2 v_Coordinates;
varying vec3 v_ViewDirection;
varying vec3 v_LightDirection;

// Attributes
attribute vec3 a_Tangent;
attribute vec3 a_Bitangent;

void main()
{
	// Texture coordinates
	v_Coordinates = gl_MultiTexCoord0.xy;

	// Vertex position
	vec4 VertexPosition = gl_ModelViewMatrix*gl_Vertex;

	vec3 Tangent = gl_NormalMatrix*a_Tangent; 
	vec3 Bitangent = gl_NormalMatrix*a_Bitangent; 
	vec3 Normal = gl_NormalMatrix*gl_Normal; 

	// Light direction
	vec3 LightVector = gl_LightSource[0].position.xyz-VertexPosition.xyz;

	v_LightDirection.x  = dot( Tangent, LightVector.xyz );
	v_LightDirection.y  = dot( Bitangent, LightVector.xyz );
	v_LightDirection.z  = dot( Normal, LightVector.xyz );

	// Distance to light
	v_Distance = length( LightVector );

	// TBN matrix
	mat3 TangentSpace;
	TangentSpace[0] = Tangent; 
	TangentSpace[1] = Bitangent; 
	TangentSpace[2] = Normal; 
	v_ViewDirection= vec3(-VertexPosition)*TangentSpace ;

	// Transform vertex
	gl_Position = ftransform();  
}

fragment program:

// Uniform variables
uniform float u_Factor;
uniform sampler2D u_Texture0;
uniform sampler2D u_Texture1;
uniform sampler2D u_Texture2;

// Varying variables
varying float v_Distance;
varying vec2 v_Coordinates;
varying vec3 v_ViewDirection;
varying vec3 v_LightDirection;

void main()
{
	// Normalized light direction
	vec3 LightDirection = normalize( v_LightDirection );

	// Normalized Normal value
	vec3 Normal = normalize( (texture2D(u_Texture1, v_Coordinates).xyz*2.0)-1.0 );

	// Normal . light direction
	float NdotL= dot( Normal, LightDirection ); 

	// Reflection vector
	vec3 Reflection = normalize( ((2.0*Normal )*NdotL)-LightDirection ); 

	// Normalized view direction
	vec3 ViewDirection = normalize( v_ViewDirection );

	// Reflection . view direction
	float RdotV = max( 0.0, dot(Reflection, ViewDirection) );

	// Height value
	float Height = (texture2D(u_Texture2, v_Coordinates).r)*u_Factor;

	// Parallax'ed texture coordinates
	vec2 Coordinates = v_Coordinates+(ViewDirection.xy*Height);

	// Color value
	vec4 Color = texture2D( u_Texture0, Coordinates );

	// Final colors
	vec4 TotalAmbient = gl_FrontMaterial.ambient*Color; 
	vec4 TotalDiffuse = gl_FrontMaterial.diffuse*NdotL*Color; 
	vec4 TotalSpecular = gl_FrontMaterial.specular*(pow(RdotV, gl_FrontMaterial.shininess));

	// Summarize colors
	gl_FragColor = TotalAmbient+TotalDiffuse+TotalSpecular;
}

take a look at the first screenshot:

can you see that the bricks are “facing” the camera? actually, you should see the top side of the bricks. so it seemed to me that the y-axis of the direction vector has to be inverted. i added this line to my fragment program:

// HACK!
ViewDirection = vec3( ViewDirection.x, -ViewDirection.y, ViewDirection.z );

and voila, it it seemed to work:

so i thought “strange, but who cares as long as it works”. but then i applied the shader to the floor, and the problem occured again… if i remove my hack from the shader, is looks correct on the floor - with the hack it looks correct on all other surfaces… the code is based on my bump mapping shader where everything worked fine. all i added is the distortion of the uv coordinates

what am i doing wrong? thanks!

The normal from the normal map is in tangent space and the lightvector is in some other space.
No need to transform by NormalMatrix. Look at how other people do it and you can just take their code.

vec3 Tangent = gl_NormalMatrixa_Tangent;
vec3 Bitangent = gl_NormalMatrix
a_Bitangent;
vec3 Normal = gl_NormalMatrix*gl_Normal;

mh if i do not multiply by the normal matrix then the lighting is totally ****ed up :stuck_out_tongue: i just realized that i’m not using my uniform u_CameraPosition at all in the vertex shader. but i guess i have to incorporate this, right? so far i’m simply passing the camera’s position to the shader, do i have to pre-multiply by the camera’s view matrix or sth like that?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.