#1 Members - Reputation: 122
Posted 08 March 2012 - 06:56 PM
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!
#2 Members - Reputation: 811
Posted 08 March 2012 - 07:06 PM
#3 Members - Reputation: 122
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 Members - Reputation: 811
Posted 08 March 2012 - 07:41 PM
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 Members - Reputation: 122
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 Members - Reputation: 122
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?
#8 Members - Reputation: 640
Posted 09 March 2012 - 05:09 AM
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 Members - Reputation: 122
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 Members - Reputation: 463
Posted 09 March 2012 - 07:01 PM
Any simulation you want to do with GPU better stay at there.
#11 Members - Reputation: 640
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.






