# Heightmap terrain texturing

This topic is 3340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm trying to remove the texture stretching and distortion on steep sections of the terrain(cliffs). I'm using projective texturing. However, it doesn't work correctly. To get the UVs to the range [0,1] I'm doing this: tex.xy /= tex.w; tex.x = 0.5f * tex.x + 0.5f; tex.y = -0.5f * tex.y + 0.5f; Using wrap mode and scaling accordingly should work for tiling. But I'm getting weird results.(too much tilling) and projection doesn't work correctly. How do I create the viewmatrix, the ortho-projectionmatrix and the texture tiling correctly? Thanks in advance.

##### Share on other sites
try this to create the matrices

//for the projection matrixD3DXMATRIX matTerrainProj;D3DXMATRIX matTerrainView;D3DXMatrixOrthoLH(      	&matTerrainProj,	terrainwidth,	terrainheight,	1.0f, //doesnt matter	1000000.0f //doesnt matter either);//use these to determine which way the projection goes, with camera at 0,0,0...//this is for a top down projection:float yaw=D3DXToRadian(0);  float pitch=D3DXToRadian(90);float roll=D3DXToRadian(0) ;D3DXQUATERNION qRsun;D3DXMATRIX matOrientation;D3DXQuaternionRotationYawPitchRoll( &qRsun, yaw, pitch, roll );D3DXMatrixAffineTransformation( &matOrientation, 1.25f, NULL, &qRsun, &D3DXVECTOR3(0,0,0));D3DXMatrixInverse( &matTerrainView, NULL, &matOrientation );

OK, after using that code to calculate the matrices, pass them to your shader and then in the shader calculate the final projection matrix:

float4 projTexCoord=worldpos;
projTexCoord=mul(pos,matTerrainView);
projTexCoord=mul(projTexCoord,matTerrainProj);
projTexCoord.xy/projTexCoord.z;

now you can sample using tex2D() because you have divided xy/z.. so sample=tex2D(sampler,projTexCoord.xy*scale).

I have skipped one part that is not needed: normally for projection you want to do something like this:
projTexCoord.xy-=0.5f; //which centers the coords correctly, but with tiling this is not needed.

##### Share on other sites
I want to texture the cliffs, so I need to do a side projection.
In your code, is the origin (0,0,0) the center of the terrain?
In my application (0,0,0) is the left top corner of the heightmap.
Why are you multipling the matrices in the shader? Is it wrong, if I multiply just projTexCoord by matWorldViewProj? I can use the combined matrix. This is how I'm transforming vertices into homogeneous clip space.
As far as I know w-component is for homogeneous divide, so I'm doing this:
projTexCoord.xy /= projTexCoord.w;
Is that wrong?

##### Share on other sites
Yes, 0,0,0 is the center in my terrain, but it shouldn't matter as the matrices you want will be orthagonal...for side projections use:

and

Yes you can the matrix mult in the app, I just do them in the shader because of some other reasons. Using z works better for me sometimes, but i suppose it doesnt matter.