Heightmap in Shader not working

Posted by CSharkVisibleBasix on Game Development See other posts from Game Development or by CSharkVisibleBasix
Published on 2012-09-01T09:23:16Z Indexed on 2012/09/01 9:50 UTC
Read the original article Hit count: 363

Filed under:
|

I'm trying to implement GPU based geometry clipmapping and have problems to apply a simple heightmap to my terrain. For the heightmap I use a simple texture with the surface format "single". I've taken the texture from here. To apply it to my terrain, I use the following shader code:

texture Heightmap;
sampler2D HeightmapSampler = sampler_state {
  Texture = <Heightmap>;
  MinFilter = Point;
  MagFilter = Point;
  MipFilter = Point;
  AddressU = Mirror;
  AddressV = Mirror;
};

Vertex Shader:

float4 worldPos = mul(float4(pos.x,0.0f,pos.y, 1.0f), worldMatrix);
float elevation = tex2Dlod(HeightmapSampler, float4(worldPos.x, worldPos.z,0,0));
worldPos.y = elevation * 128;

The complete vertex shader (also containig clipmapping transforms) looks like this:

float4x4 View : View;
float4x4 Projection : Projection;
float3 CameraPos : CameraPosition;
float LODscale; //The LOD ring index 0:highest x:lowest
float2 Position; //The position of the part in the world
texture Heightmap;
sampler2D HeightmapSampler = sampler_state {
    Texture = <Heightmap>;
    MinFilter = Point;
    MagFilter = Point;
    MipFilter = Point;
AddressU = Mirror;
AddressV = Mirror;
};

//Accept only float2 because we extract 3rd dim out of Heightmap
float4 wireframe_VS(float2 pos : POSITION) : POSITION{
    float4x4 worldMatrix = float4x4(
      LODscale, 0, 0, 0,
      0, LODscale, 0, 0,
      0, 0, LODscale, 0,
      - Position.x * 64 * LODscale + CameraPos.x, 0, Position.y * 64 * LODscale + CameraPos.z, 1);

  float4 worldPos = mul(float4(pos.x,0.0f,pos.y, 1.0f), worldMatrix);
  float elevation = tex2Dlod(HeightmapSampler, float4(worldPos.x, worldPos.z,0,0));
  worldPos.y = elevation * 128;
  float4 viewPos = mul(worldPos, View);
  float4 projPos = mul(viewPos, Projection);

  return projPos;
}

© Game Development or respective owner

Related posts about hlsl

Related posts about heightmap