0

I'm trying to implement a GPU based height map the simplest (and fastest) way that I know how. I passed a (.png, D3DX11CreateShaderResourceViewFromFile()) texture into the shader, and I'm attempting to sample it for the current pixel value. Seeing a float4, I'm currently assigning a color value from a channel to offset the y value.

Texture2D colorMap : register(t0);
SamplerState colorSampler : register(s0);

...

VOut VShader(float4 position : POSITION, float2 Texture : TEXCOORD0, float3 Normal : NORMAL)
{
    VOut output;

    float4 colors = colorMap.SampleLevel(colorSampler, float4(position.x*0.001, position.z*0.001, 0,0 ),0);
    position.y = colors.x*128;

    output.position = mul(position, WVP);
    output.texture0 = Texture;
    output.normal = Normal;

    return output;
}

The texture is imported correctly, and I've inserted another texture and was able to successfully blend the texture with another texture (through multiplication of values), so I know that the float4 struct contains values of a sort capable of having arithmetic performed on it.

In the Vertex function, attempting to extract the values yields nothing on a grid: enter image description here

The concept seemed simple enough on paper...

Community
  • 1
  • 1
Rashid Ellis
  • 330
  • 3
  • 22

1 Answers1

1

Since you're using a Texture2D, the Location parameter needs to be a float2.

Also, make sure that location goes from (0,0) to (1,1). For your mapping to be correct, the grid would need to be placed from (0,0,0) to (1000,0,1000).

If this is the case then this should work:

SampleLevel(colorSampler, position.xz*0.001 ,0);

Edit

I'm curious as to how you're testing this. I tried compiling your code, with added definitions for VOut and WVP, and it fails. One of the errors is that location parameter which is a float4 and should be a float2. The other error I get is the name of the function; it should be main.

If you happen to be using Visual Studio, I strongly recommend using the Graphics Debugging tools and check all the variables. I suspect the colorMap texture might be bound to the pixel shader but not the vertex shader.

  • Just to confirm, I should be passing texture coordinates to the location parameter? or the position in from vertex?(x,y). Tried both and it's like the value is null. – Rashid Ellis Dec 03 '14 at 04:44
  • That parameter represents a texture coordinate, but you can use whatever float2 you want, such as a modified position. I'm curious as to how you're testing this. I tried compiling your code, with added definitions for VOut and WVP, and it fails. One of the errors is that location parameter which is a float4 and should be a float2. The other error I get is the name of the function; it should be main. – Andres Urquijo Dec 03 '14 at 16:23
  • Vertex function name can be any thing. At least in VS hlsl compiler. Should `SampleLevel()` return `float4`? – Rashid Ellis Dec 03 '14 at 17:09
  • xD You're my new friend. Texture wasn't bound to vertex shader, it's why my values were like NaN or weird. – Rashid Ellis Dec 03 '14 at 18:46