Sign in to follow this  
LivingLegend

Get Pixel From Direct X

Recommended Posts

Hello, I am making a game and as part of the user interface I am rendering a second (simplified scene) in the background, but I do not want to display this. The idea behind this is that I can detetmine which 3D object the user clicked on. For example, if the users mouse pointer is at coordinates (10,10), I can find what colour (10,10) is in the simplified scene and this will tell me which object they are clicking on. However, I can't find a way to find the colour of a given coordinate on a directx scene which hasn't been rendered. Obviously I don't want to render this simplified scene to the screen. Does anyone know how I could achieve this? I've tried all sorts of properties with picture boxes etc... I can't find a way. Any help would be greatly appreciated

Share this post


Link to post
Share on other sites
Do you really need to know the colour of the pixel? The usual way of doing this is called Picking (Google for it), where you convert a 2D pixel position into a 3D ray, and see what objects in your scene that ray intersects (Usually choosing the closest one).

If you do need to know the colour of the pixel, the best way would probably be to render the simplified scene to an off screen render target (Probably a texture), and double or triple buffer it to avoid stalls. Then read from that instead of the backbuffer (Since doing so is extremely expensive). There might be a way to do this with queries, I'm not sure.

Share this post


Link to post
Share on other sites
One way to do this would be to render to the screen, but use a scissor test to limit rendering to only the pixel under the cursor. This pixel is hidden anyway (by the cursor), so the user won't see the extra rendering, and you can get that pixel using GetRenderTargetData. If you use a lockable back buffer, you could also just lock and get the specific pixel. I'm not sure which would be faster. GetRenderTargetData transfers a lot of data to the CPU side, but a lockable buffer is less optimal for rendering.

An alternative is to render to a small render target (1x1 pixel, optimally, though that might not work, as some drivers have problems with such small targets), and set the transform such that it will render only the part at the cursor. This is somewhat tricky, but it'd be faster than the previous options.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this