Public Group

Reading Pixel Data in DirectX 9

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

Recommended Posts

Hello all. I'm a very new game programmer, and I've been working on my first game for a few weeks now, but I've hit a snag. I'm trying to create boundries in my game using pixels of a certain color specified to represent impassable walls, but I don't know how to actually check the color of a pixel in a point in space.

This problem is likely due to the fact that I don't know what variables are in the LPDIRECT3DSURFACE9 object, and I don't know how that object holds whatever it does hold or anything.

My question is ths: What variable(s) in the LPDIRECT3DSURFACE9 object hold that object's pixel color data, and how can I access, test, or write to that data?

Any answers will be greatly appreciated!

Share on other sites
To get the pixel data of a IDirect3DSurface9 object (what a LPDIRECT3DSURFACE9 points to), you need to use its LockRect function which will allow you to access the data on the surface. Remember to use IDirect3DSurface9::UnlockRect when you are finished with the pixel data.

Share on other sites

To get the pixel data of a IDirect3DSurface9 object (what a LPDIRECT3DSURFACE9 points to), you need to use its LockRect function which will allow you to access the data on the surface. Remember to use IDirect3DSurface9::UnlockRect when you are finished with the pixel data.

So you lock a portion of the surface first, then access the pixel data, and then unlock it when you are finished, right?
Could you tell me exactly how a person should access the pixel data, if for example, you wanted to check if the point (52x, 15y, 0Z) had a black pixel?

Share on other sites
The first step is to pass a pointer to a D3DLOCKED_RECT structure to the LockRect function, like this:
 D3DLOCKED_RECT lockedRect; surface->LockRect(&lockedRect,NULL,0); 
where surface should be replaced by the surface you want to get the pixel data from. Next you need to access the raw pixel data, however this depends on what format the surface is in. Assuming 32-bit colour, you could just do this:
 unsigned int *pixels=lockedRect.pBits; if(pixels[52+15*width]&COLOUR_MASK==0) { //then the pixel at (52,15) is black } 
where COLOUR_MASK is simply a representation of where the colour bytes are (so if you have a RGBA format it should be 0xFFFFFF00)

Share on other sites

The first step is to pass a pointer to a D3DLOCKED_RECT structure to the LockRect function, like this:
 D3DLOCKED_RECT lockedRect; surface->LockRect(&lockedRect,NULL,0); 
where surface should be replaced by the surface you want to get the pixel data from. Next you need to access the raw pixel data, however this depends on what format the surface is in. Assuming 32-bit colour, you could just do this:
 unsigned int *pixels=lockedRect.pBits; if(pixels[52+15*width]&COLOUR_MASK==0) { //then the pixel at (52,15) is black } 
where COLOUR_MASK is simply a representation of where the colour bytes are (so if you have a RGBA format it should be 0xFFFFFF00)

Thank you so much! You can't imagine how wonderfully useful this information is! I'll be sure to put this to good use. Thank you again for all your help!

Share on other sites

The first step is to pass a pointer to a D3DLOCKED_RECT structure to the LockRect function, like this:
 D3DLOCKED_RECT lockedRect; surface->LockRect(&lockedRect,NULL,0); 
where surface should be replaced by the surface you want to get the pixel data from. Next you need to access the raw pixel data, however this depends on what format the surface is in. Assuming 32-bit colour, you could just do this:
 unsigned int *pixels=lockedRect.pBits; if(pixels[52+15*width]&COLOUR_MASK==0) { //then the pixel at (52,15) is black } 
where COLOUR_MASK is simply a representation of where the colour bytes are (so if you have a RGBA format it should be 0xFFFFFF00)

One last question, and I'm through. When I try to compile your example, I get an error stating that "Conversion from 'void*' to pointer to non-'void' requires an explicit cast" reguarding the line:
unsigned int *pixels=lockedRect.pBits;

Do you know how I can correct this?

Share on other sites
Ah yes, you just change unsigned int *pixels=lockedRect.pBits; to unsigned int *pixels=(unsigned int*)lockedRect.pBits;. Sorry about that; it has been a while since I did this sort of thing.

Share on other sites
I should maybe just add that it's not safe to index the pixels this way. A row in the texture's memory doesn't have to be (texture_width_in_pixels * pixel_size) large, you should use lockedRect.Pitch instead.
It probably will work anyway, especially for power-of-two textures (512, 1024 etc), but you can never be really sure. And if you are using textures like 800x600 then the chance that DirectX will add some padding to the rows is very big. You may realise that a 800x600 texture in fact occupies the same memory as if it was 1024x600, with 224 unused blank "pixels" added to every row - then width of the texture is only 800 but pitch is (1024 * number_of_bytes_per_pixel).

Share on other sites

I should maybe just add that it's not safe to index the pixels this way. A row in the texture's memory doesn't have to be (texture_width_in_pixels * pixel_size) large, you should use lockedRect.Pitch instead.
It probably will work anyway, especially for power-of-two textures (512, 1024 etc), but you can never be really sure. And if you are using textures like 800x600 then the chance that DirectX will add some padding to the rows is very big. You may realise that a 800x600 texture in fact occupies the same memory as if it was 1024x600, with 224 unused blank "pixels" added to every row - then width of the texture is only 800 but pitch is (1024 * number_of_bytes_per_pixel).

I think I see what you mean, here. My program is now compiling correctly, but when I try to access the pixel's memory, I get an access violation message. Could you give an example as to how one would use lockedRect.Pitch to find out, for example, if a pixel at (64x, 14y) is black?

Share on other sites
You should very rearly (never) read any data back to system memory from gpu. And if you do, you want to reduce the data to minimal and do it asynchonously. One example I've encountered is reading a 1x1 pixel for current screen luminance, and asynchronously if possible.. The impassable walls you want is easily stored in system memory.

Any simulation you want to do with GPU better stay at there.

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• Forum Statistics

• Total Topics
633756
• Total Posts
3013708
×