Sign in to follow this  
texel3d

Problem with Displacement

Recommended Posts

Hi, I am tring to use displacement to render an heightmap. I can render a basic displacement without any problem. But to optimize rendering, i want to displace only the part of the screen which is in the camera frustum. I have an idea to avoid the use of quad tree or ROAM. I want to project a flat grid of vertices from the camera to the ground, and than, displace the projected vertices with the texture of the heightmap. But i have a problem and i don't know how to solve it. When i project the grid to the ground, It seems to be Ok. But when i displace the projected vertices, their heights are heigher and heigher when vertices are far from the camera. You can see the problem on this video: http://texelinside.free.fr/bugs/displacement.avi.SWF You can see the projector which project the grid on the ground. In the beginning of the video the displacement texture is 100% black (the projection seems to be good). But when i select an height map texture, you can see the problem. When vertices are close to the camera the height of vertices is low and increase with distance. Maybe i have done an error in my shader ?
Vs_Output VertexShaderEffect(App_Input IN)
{
	Vs_Output vs_out;
	
	float4 pos = mul(float4(IN.position, 1), g_matWorld); // Position * WorldMatrix
	
		// Compute texture coordinates. pos could be used instead of posTexture.
		// But it allow me to do some tests
		float4 posTexture = mul(float4(IN.position, 1), g_matWorld);
		float4 texCoord;
		texCoord.x = (posTexture.x / 4096.0f);
		texCoord.y = (posTexture.z / 4096.0f);
		texCoord.z = 0;
		texCoord.w = 0;

		float4 d = tex2Dlod(diffuseSampler,texCoord); // Texture color
		float displace = d.x; // Height
		pos.y = pos.y + 100.0f*displace; // Displacement
		
	pos = mul(pos, g_matViewProj); // Position * ViewMatrix * ProjectionMatrix

	vs_out.position = pos;
	vs_out.color = float4(0.0, displace, 0.0, 1.0);
	vs_out.texture0 = texCoord;

   return vs_out;
}
Where: g_matWorld = TransformationMatrix * TerrainPlanarProjectionMatrix; TransformationMatrix: Translation and rotation of the projector. TerrainPlanarProjectionMatrix: Computed using D3DXMatrixShadow.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this