Sign in to follow this  

Reading float 32 texture values

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, Im making my own editor, and right now I'm going to save te height on the heightmap into a texture. First, i used a A8R8G8B8 texture for that, and to load I use D3DLOCKED_RECT to gain access to te texture, like this:
	LPDIRECT3DTEXTURE9 Heightmap;
	D3DXCreateTextureFromFile(DIRECTX->GetDevice(), a_FileName, &Heightmap);

	D3DLOCKED_RECT lockedRect;
	::ZeroMemory(&lockedRect, sizeof(lockedRect));

	if(FAILED(Heightmap->LockRect(0, &lockedRect, NULL, 0)))
	{
		return false;
	}

	DWORD* Bits = (DWORD*)lockedRect.pBits;
	int index = 0;

	for(int y = m_Depth; y >= 0; y --)
	{
		for(int x = 0; x < (m_Depth + 1); x ++)
		{
			index = (m_Depth - y) * (m_Depth + 1) + x;
			m_EditorBuffer[index].Pos.y = ((Bits[(y*a_Mapsize)+x])&255);
		}
	}

	Heightmap->UnlockRect(0);
	Heightmap->Release();
	Heightmap = NULL;
Well, it works well and so, but the program, when reading the height values from the texture, it rounds off the value. Like this: 0.7f -> 0.0f, -13.9f -> -13.0f. Well then, I need to use a float32 texture to get better precision, but now I dont know how to get the values from the texture. The old way isn't working, and by the way, cause I use float32 texture, I can't see on the texture if it got the right height values. Here is the shader I use to create the heightmap:
float WorldSize;

struct VS_OUT
{
	float4 Pos 		: POSITION;
	float  Color	: TEXCOORD0;
};

VS_OUT ScreenQuadVS(
		float3 Position : POSITION) 
{
    VS_OUT OUT;

//cause writing to a texture, only X and Y is important
    OUT.Pos.x = Position.x / WorldSize;
    OUT.Pos.y = (Position.z / 2.0f) / (WorldSize / 2.0f);
    OUT.Pos.z = 0.0f;
    OUT.Pos.w = 1.0f;

//take the height and make it into a color value
//adds 128 cause i don't want any negativ values on the texture 
    OUT.Color = (Position.y + 128.0f);

    return OUT;
}

float4 PSHeightTexture(float Color	: TEXCOORD0	) : COLOR0
{	
    return Color;
}

technique CreateHeightmap
{
    pass P0
    {
        VertexShader = compile vs_2_0 ScreenQuadVS();
        PixelShader  = compile ps_2_0 PSHeightTexture();
    }
}
Atleast this shader worked on the other texture, complaine if you think it is necessary :þ Help me anyone :)

Share this post


Link to post
Share on other sites
Exactly which D3DFMT_?? value are you using> _R32F? If so, casting your pointer to a regular float should be sufficient. If you're using 4xFP32 textures (A32R32G32B32F) then you can be clever and cast to a D3DXVECTOR4...

Your rendering VS/PS looks a little odd - for FP32 you'd be okay, but that shader shouldn't really work on an A8R8G8B8 texture. Your output is, presumably, 0-255 based on geometry in the range -128 to +127? Shaders operate in 0.0 to 1.0 ranges for both inputs and outputs unless you're using the FP16 or FP32 formats.


hth
Jack

Share this post


Link to post
Share on other sites
Hello,

What about using alpha test when writing to a Float32 texture? Is there anyway to make it work? (discard texels in PS according to alpha value?)

Thanks.

Share this post


Link to post
Share on other sites
To my knowledge there's no native way to do Alphatesting on FP rendertargets. Use the HLSL intrinsic function clip() in your pixel shader to achieve a comparable effect.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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