Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

LockedRect pBits is always 0

This topic is 4836 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

I'm trying to implement heightmaps in my engine, and have currently stumbled onto a bit of a bug.
#define GetB(c) (((c))&255)

D3DXCreateTextureFromFile(pDevice, "map.bmp", &p_HeightTex);
D3DLOCKED_RECT  lockedrect;
ZeroMemory(&lockedrect, sizeof(lockedrect));

F_RET(p_HeightTex->LockRect(0, &lockedrect, NULL, 0));

DWORD* pBits=(DWORD*)lockedrect.pBits;
int index=0;

for(int y=0; y<256; y++)
{
	for(int x=0; x<256; x++)
	{
		int height = (GetB(pBits[(y*256)+x]))*0.2f;
	}
}
With this code height is always equal to zero, regardless of x or y.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Sfpiano
I'm trying to implement heightmaps in my engine, and have currently stumbled onto a bit of a bug.

(snipped code)

With this code height is always equal to zero, regardless of x or y.



Without knowing the format of map.bmp, it is difficut to gauge exactly what is wrong. Are you sure that the texture actually loads, is 256x256, and loaded as a 32-bit format? Also, is the first byte of the pixel what you are expecting (blue?) and contain values greater than 5 (based on your .2 value)?

This code seems to work fine for me (loading a 256x256 32-bit texture, I get non-zero values).

Paul


Share this post


Link to post
Share on other sites
>>pBits[(y*lockedrect.Pitch)+x]

Be careful with this. lockedrect.Pitch is the number of bytes, not the number of pixels.

Share this post


Link to post
Share on other sites
Quote:
Original post by don
>>pBits[(y*lockedrect.Pitch)+x]

Be careful with this. lockedrect.Pitch is the number of bytes, not the number of pixels.


Ah, thanks for the correction. So a division by sizeof(DWORD) is needed. But I suppose it isn't guaranteed to have Pitch divisible by sizeof(DWORD). So:
BYTE* pBits = ...;
...
height = *reinterpret_cast<DWORD*>(&pBits[y *lockedrect.Pitch + x * 4]);

Share this post


Link to post
Share on other sites
Have you tried using D3DXCreateTextureFromFileEx(), and specifying the format you want to load? As paulble said, D3DXCreateTextureFromFile() will use whatever format the BMP is in, which is probably 24-bit. Although that shouldn't cause the lock to fail.
Are you sure that LockRect() returns D3D_OK? WHat does your F_RET macro do? Do you have the debug D3D runtime installed? The only reason that pBits should be NULL is if LockRect() fails, and with the debug runtime installed, you'll get a useful error message telling you what the problem is.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
D3DXCreateTextureFromFile() will use whatever format the BMP is in, which is probably 24-bit. Although that shouldn't cause the lock to fail.

But, for this reason you also cannot guarantee that the binary data will be DWORD and/or what order the channels are in... In truth, you don't really seem to know what data you're reading [smile]


Quote:
Original post by Evil Steve
The only reason that pBits should be NULL is if LockRect() fails

I interpretted the post as being that the mathematics for calculating height returns 0 - not that the pBits pointer is NULL. Things would start to explode with that code if you were using a null pointer [grin]. I could be wrong though.

Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
I interpretted the post as being that the mathematics for calculating height returns 0 - not that the pBits pointer is NULL. Things would start to explode with that code if you were using a null pointer [grin]. I could be wrong though.
Ah yes. I really need to start reading the posts as well as the topic titles [wink]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!