Sign in to follow this  
akira32

How do I read a pixel color of texture with D3DFMT_A32B32G32R32F

Recommended Posts

How do I read a pixel color of texture with format of D3DFMT_A32B32G32R32F? How do I calcuate the offset of the pBits pointer of D3DLOCKED_RECT? As below, There is my source code about D3DFMT_A32B32G32R32F
	IDirect3DTexture9 *g_pTexture2;
	D3DXCreateTextureFromFile(g_pD3DDevice, "eyefortest.png", &g_pTexture2);

	HRESULT hr;

	D3DLOCKED_RECT LockedRect;
	D3DSURFACE_DESC Desc;

	hr = g_pTexture2->GetLevelDesc(0,&Desc);//D3DFMT_A32B32G32R32F
	hr = g_pTexture2->LockRect(0,&LockedRect,NULL,0);

	D3DXVECTOR4 *pbits = (D3DXVECTOR4 *) LockedRect.pBits;
	D3DXVECTOR4 vec4;

	//int index=0;

	for (UINT y=0;y<Desc.Height;y++)
	{
		DWORD dwOffset = y*Desc.Width;
		for (UINT x=0;x<Desc.Width;x++)
		{
			vec4=pbits[dwOffset+x];
		}
	}

	hr = g_pTexture2->UnlockRect(0);

As below, There is my source code about D3DFMT_X8R8G8B8
	HRESULT hr;

	D3DLOCKED_RECT LockedRect;
	D3DSURFACE_DESC Desc;

	hr = g_pTexture->GetLevelDesc(0,&Desc);//D3DFMT_X8R8G8B8
	hr = g_pTexture->LockRect(0,&LockedRect,NULL,0);

	DWORD *pbits = (DWORD *) LockedRect.pBits;

	for (UINT y=0;y<Desc.Height;y++)
	{
		DWORD dwOffset = y*Desc.Width;
		for (UINT x=0;x<Desc.Width;x++)
		{
			DWORD rgb=pbits[dwOffset+x];

			DWORD b=LOBYTE(rgb);
			DWORD g=(LOBYTE(((WORD)(rgb)) >> 8));
			DWORD r=(LOBYTE((rgb)>>16));
			DWORD a=(LOBYTE((rgb)>>24));

			//GetRValue
			int c=12;
		}
	}

	hr = g_pTexture->UnlockRect(0);

Are the source code of D3DFMT_A32B32G32R32F and D3DFMT_X8R8G8B8 I posted correct?

Share this post


Link to post
Share on other sites
Your code is correct - use of D3DXVECTOR4 is an elegant solution for FP32 data, but you could equally use normal float pointers.

However, your real problem is in somehow magically assuming that the texture is FP32.

D3DXCreateTextureFromFile(g_pD3DDevice, "eyefortest.png", &g_pTexture2);

This form of creating a texture will determine the texture dimensions/format (etc..) from the source file. Firstly, making this sort of assumption about external data is plain and simply dangerous (what if I change your data file - I could easily crash your program [evil]). Secondly, my knowledge of the PNG format is that it doesn't natively support ARGB32F formats.

Direct3D doesn't do any format conversion for you in this case - if it loads in 32bit ARGB8 data and you request 128bit ARGB32F data then you'll be doing a completely pointless binary cast that gives you rubbish outputs [smile]

Check the format of the incoming data and adapt your locking accordingly.

hth
Jack

Share this post


Link to post
Share on other sites

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