Getting a pixel's colour

Started by
4 comments, last by methinks 20 years, 1 month ago
Is there a function in DX9 that allows me to get a pixel''s color by it''s coordinates? I''m trying to generate a mesh based on a displacement map, as well as later using this map to figure out how high to draw any characters walking on it. I know you can generate the mesh using a vertex shader, but unfortunately that''s outside of the scope of assignment...
Advertisement
You can LockRect() the backbuffer surface and then use the returned pointer to get the value at a particular pixel.

Note that this is a rather time-consuming way to do things. Graphics cards are not designed for efficient pixel transfer back to the CPU.

"Sneftel is correct, if rather vulgar." --Flarelocke
Thanks for the help

In this case, the fact that it''s slow isn''t a big deal... I scan the whole image once during loading, after that I only need one pixel per cycle.
Call me an idiot, but...

I''ve looked at the lockrect functions, and I get what it does, but looking through the docs, all I can find is how to send stuff back into the locked surface.
The only information it seems to actually get from the surface, is the handle, and the pixel format. (With is not the actual color.) How can I get the RGB (or greyscale in this case) value of an individual pixel? Is there any kind of function that goes anything like

color GetPixel(surface, x, y)

?
No, there is no "GetPixel" type of function. What you need to do is lock the surface and access the pixels via a pointer to the bits of the surface.

The code is exactly the same as that used to access the individual texels of textures, which has been discussed many times in the forum. A search through the forum archives should give you all the info you need.

neneboricua
Once you have LockRect()ed, you will have a structure containing a pointer to your pixel data. Simply look at the appropriate offset in that data (taking into account the pitch, which is also given to you), and you''ll find the pixel you want.

"Sneftel is correct, if rather vulgar." --Flarelocke

This topic is closed to new replies.

Advertisement