Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reading Pixel Data in DirectX 9


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 EpicCupcakes   Members   -  Reputation: 130

Like
0Likes
Like

Posted 08 March 2012 - 06:56 PM

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!

Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1159

Like
1Likes
Like

Posted 08 March 2012 - 07:06 PM

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.

#3 EpicCupcakes   Members   -  Reputation: 130

Like
0Likes
Like

Posted 08 March 2012 - 07:14 PM

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?

#4 RulerOfNothing   Members   -  Reputation: 1159

Like
1Likes
Like

Posted 08 March 2012 - 07:41 PM

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)

#5 EpicCupcakes   Members   -  Reputation: 130

Like
0Likes
Like

Posted 08 March 2012 - 07:51 PM

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!

#6 EpicCupcakes   Members   -  Reputation: 130

Like
0Likes
Like

Posted 08 March 2012 - 08:16 PM

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?

#7 RulerOfNothing   Members   -  Reputation: 1159

Like
0Likes
Like

Posted 08 March 2012 - 09:44 PM

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.

#8 Tom KQT   Members   -  Reputation: 1539

Like
0Likes
Like

Posted 09 March 2012 - 05:09 AM

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).

#9 EpicCupcakes   Members   -  Reputation: 130

Like
0Likes
Like

Posted 09 March 2012 - 04:53 PM

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?

#10 Codarki   Members   -  Reputation: 462

Like
0Likes
Like

Posted 09 March 2012 - 07:01 PM

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.

#11 Tom KQT   Members   -  Reputation: 1539

Like
0Likes
Like

Posted 10 March 2012 - 04:07 AM

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?


Try this for example http://www.toymaker.info/Games/html/direct3d_faq.html#D3D9
They are reading all pixels in the texture in two for loops, so it's easy to convert it into reading just one specific pixel.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS