Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vertex texture fetch problem

Started by RobM Apr 2, 2009 at 9:59 AM 1 replies 1k views
Original Post
RobM
RobM
Hi I'm doing some investigation into vertex texture fetching and I have an issue I don't seem to be able to solve. I have created a 512x512 vertex texture using this:

D3DXCreateTexture(<myDevice>, 512, 512, 1, D3DUSAGE_DYNAMIC, D3DFMT_R32F, D3DPOOL_DEFAULT, &m_vertexTexture);


I then lock it and fill it with data from a bitmap I've loaded. I won't post that code for brevity, but when I inspect the (float*) pBits, I see what I'd expect. For my simple demo, I have a 512x512 texture with one white pixel in each corner, so my pBits for the vertex texture looks like: imageData[0] = 255, imageData[1] -> [510] = 0, imageData[511] = 255, and so on. In my shader, I set up the sampler like this:

sampler DisplacementMap =
sampler_state
{
   Texture = <g_displacementMap>;
   MipFilter = POINT;
   MinFilter = POINT;
   MagFilter = NONE;
};


and I'm using it in the vertex shader like this (after setting g_displacementMap to my vertex texture in the code):

float height = tex2Dlod(DisplacementMap, float4(Position.x, Position.z, 0.0f, 0.0f)).x;


I know Position.x and Position.z are correct as the grid renders correctly. Every height I get back from the tex2Dlod function is 255. The reason I know Position.x and Position.z (my vertex shader inputs) are correct is because I can do this:

if (Position.x == 4 && Position.z == 4)
   height = 0.0f;


..which gives me a flat grid but with the height of grid point (vertex) 4,4 at zero and the rest at 255. Any ideas? I feel like I've tried everything. Edit: I'm using Dx9.0c Thanks
harveypekar
harveypekar
Textures are addressed between [0 1] normally. Because you don't set the AddressU etc properties, the results are undefined from the code you supplied (could be tiled, clamped, whatever).

Are all your positions integers? This could explain the 255 result, as you would be sampling at (0,0),(0,1),(0,2), which with tiling would always be the same pixel.

Solution :

1) Add extra float2 attribute to your vertex format spanning [0,1] over your entire terrain, and use that to sample your texture

2) divide your position by the extends of your terrain, bringing it in [0 1] eg
Position.xz / Terrain_dimensions.xz
RobM
RobM
Oh boy, how embarrassing! I've been texturing things for years - I'm kicking myself right now!

It's working fine now, thanks!!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.