Getting pixel information from a surface

Started by
7 comments, last by malyskolacek 22 years, 5 months ago
Hi all! I wanted to do mouse selections and I decided I will try rendering all objects (every has its own color) onto a surface and then I get color of pixel, on which clicked the mouse. Rendering is ok, getting the pixel hopefully too, but I don''t know how to get color of a pixel from the integer copied from D3DLOCKED_RECT. The integer is a number like 3435921408. Here''s some code: if (FAILED(m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ))) return E_FAIL; if (FAILED(m_pd3dDevice->BeginScene())) return E_FAIL; SetupMatrices(); m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT); m_pd3dDevice->SetRenderState(D3DRS_AMBIENT,0xffffffff); for (int hh=0;hhEndScene())) return E_FAIL; //m_pPoint contains the position of the mouse when it clicks ScreenToClient(m_hWnd, &m_pPoint); LPDIRECT3DSURFACE8 pSurf; D3DLOCKED_RECT lRect; int offset; unsigned int pixel; m_pd3dDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurf); pSurf->LockRect(&lRect, NULL, D3DLOCK_READONLY); //I''am really not sure if this is right offset=m_pPoint.y*lRect.Pitch+m_pPoint.x*2; memcpy(&pixel, (BYTE*)lRect.pBits + offset, 2); //now in pixel I have an integer and I need to get color from it pSurf->UnlockRect(); pSurf->Release(); Thank you very much
Advertisement
Or can somebody post a link explaining this?
The integer is a number like 3435921408.
May be RGB_GETBLUE/RGB_GETRED/RGB_GETGREEN would help?
(f) Fisher
This helps, but it still doesn''t work
Perhaps this isn''t right:

offset=m_pPoint.y*lRect.Pitch+m_pPoint.x*2;
memcpy(&pixel, (BYTE*)lRect.pBits + offset, 2);

Can you look at this? If it has something to do with this, so I use D3DFMT_R5G6B5

Main problem is, that wherever I click, I get red 204. Blue and green change.
gmm..
i don''t know about dx8 but in dx7 when you''re locking the surface you can get lPitch - "Distance, in bytes, to the start of next line" and that''s it. And in your way i''m not sure with lRect.Pitch :-/ does it the same?
(f) Fisher
Problem is now solved! It was in wrong breaking the uint into R/G/B...
I''m having the same problem right now. Can you explain how it works because even with the code, I''m still not sure how to do it?
1) I clear the backbuffer and I render all meshes without any lights(except ambient 0xffffffff), fogs, textures, ...
2) Every mesh has its own unique color random generated at the very beginning of the program
3) Then I lock the backbuffer and I get the pixel with the mouse coordinates
4) The number I get is an uint. I compute this uint for each mesh this way: pixel2=((r/8)<<11) | ((g/4)<<5) | (b/8);
Attention: this only works in 16-bit mode, for 32- or 24-bit it should be computed a bit differently!
5) Finally I compare the uint from surface with every mesh''s uint and if none equals, you haven''t clicked any mesh.
That''s all, hope clear now
Thanks

This topic is closed to new replies.

Advertisement