How to get screen pixel color?
How to get screen pixel color?
I was wondering: does a function like
SColor IVideoDriver::getPixelScreen(position2d<s32>);
exist? I'm sure there's a way. I get what I need with creating IImage with IVideoDriver::createScreenshot() and using it's method getPixel() but this is very slow and unefficient ...
SColor IVideoDriver::getPixelScreen(position2d<s32>);
exist? I'm sure there's a way. I get what I need with creating IImage with IVideoDriver::createScreenshot() and using it's method getPixel() but this is very slow and unefficient ...
if you're under Windows, then maybe this can help you :
Code: Select all
// link against Gdi32.lib (libGdi32.a) !!!
#include <windows.h>
HDC grabDesktop() {
HWND hwnd;
HDC ddc;
HDC mdc;
RECT sz;
HBITMAP hbmp;
hwnd = GetDesktopWindow();
ddc = GetDC(hwnd);
GetWindowRect(hwnd, &sz);
mdc = CreateCompatibleDC(ddc);
hbmp = CreateCompatibleBitmap(ddc, sz.right, sz.bottom);
SelectObject(mdc, hbmp);
BitBlt(mdc, 0, 0, sz.right, sz.bottom, ddc, 0, 0, SRCCOPY);
ReleaseDC(hwnd, ddc);
return (mdc);
}
int main(int argc, char* argv[]) {
HDC hDC;
COLORREF cref;
// to get the pixel at 100,100
int posX = 100;
int posY = 100;
hDC = grabDesktop();
cref = GetPixel(hDC, posX, posY);
printf("%02X %02X %02X", GetRValue(cref), GetGValue(cref), GetBValue(cref));
}
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Why are you creating a screenshot???
virtual bool irr::video::IVideoDriver::setRenderTarget ( video::ITexture * texture,
bool clearBackBuffer = true,
bool clearZBuffer = true,
SColor color = video::SColor(0, 0, 0, 0)
)
Just reuse the same texture, that way it won't keep creating a new texture in memory. Then you call
virtual void* irr::video::ITexture::lock ( bool readOnly = false )
So you can access the individual pixel of the textures with the void* returned.
When you are done, use
virtual void irr::video::ITexture::unlock ( )
to tell Irrlicht the texture is accessible again.
Then you can use setrendertarget again to put the screen buffer back again.
Then you use virtual void irr::video::IVideoDriver::draw2DImage ( const video::ITexture * texture,
const core::position2d< s32 > & destPos
)
To draw back the texture on the screen buffer.
You don't need a screenshot, this is terribly wasteful, and no, I won't write you a shader, I've got other things to do and this should have all went into beginner's help.
virtual bool irr::video::IVideoDriver::setRenderTarget ( video::ITexture * texture,
bool clearBackBuffer = true,
bool clearZBuffer = true,
SColor color = video::SColor(0, 0, 0, 0)
)
Just reuse the same texture, that way it won't keep creating a new texture in memory. Then you call
virtual void* irr::video::ITexture::lock ( bool readOnly = false )
So you can access the individual pixel of the textures with the void* returned.
When you are done, use
virtual void irr::video::ITexture::unlock ( )
to tell Irrlicht the texture is accessible again.
Then you can use setrendertarget again to put the screen buffer back again.
Then you use virtual void irr::video::IVideoDriver::draw2DImage ( const video::ITexture * texture,
const core::position2d< s32 > & destPos
)
To draw back the texture on the screen buffer.
You don't need a screenshot, this is terribly wasteful, and no, I won't write you a shader, I've got other things to do and this should have all went into beginner's help.
you're welcome !!!terier wrote:thank you very much, Acki
but there seems to be an error with the code I posted before...
I now changed it and made it a bit more usable for your needs:
Code: Select all
irr::video::SColor getPixelColor(irr::core::position2d<s32> pos){
static HWND hwnd = GetDesktopWindow();
static HDC ddc = GetDC(hwnd);
static HDC mdc = CreateCompatibleDC(ddc);
RECT sz;
GetWindowRect(hwnd, &sz);
HBITMAP hbmp = CreateCompatibleBitmap(ddc, sz.right, sz.bottom);
SelectObject(mdc, hbmp);
BitBlt(mdc, 0, 0, sz.right, sz.bottom, ddc, 0, 0, SRCCOPY);
COLORREF cref;
cref = GetPixel(mdc, pos.X, pos.Y);
DeleteObject(hbmp);
return irr::video::SColor(255, GetRValue(cref), GetGValue(cref), GetBValue(cref));
}
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
yeah, found the error in the first code (stupid me released a context that must be deleted) !!!Acki wrote:but there seems to be an error with the code I posted before...
now here is the clean and corrected code:
Code: Select all
video::SColor getPixel(core::position2d<s32> pos){
static HWND hwnd = GetDesktopWindow();
HDC ddc = GetDC(hwnd);
HDC mdc = CreateCompatibleDC(ddc);
RECT sz;
GetWindowRect(hwnd, &sz);
HBITMAP hbmp = CreateCompatibleBitmap(ddc, sz.right, sz.bottom);
SelectObject(mdc, hbmp);
BitBlt(mdc, 0, 0, sz.right, sz.bottom, ddc, 0, 0, SRCCOPY);
COLORREF cref;
cref = GetPixel(mdc, pos.X, pos.Y);
DeleteObject(hbmp);
DeleteDC(mdc);
ReleaseDC(hwnd, ddc);
return video::SColor(255, GetRValue(cref), GetGValue(cref), GetBValue(cref));
}
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Speed !?!?!hybrid wrote:Why would that code be any better than rendering to texture and using that data?
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
well, I'm too lazy for this, but maybe you write the same function but using RTT, so we can make a benchmark comparison...
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Make sure to use readOnly = true if you don't want to re-upload the entire texture again every frame.
I'd be interested to see a performance comparison with Acki's method too. Does it work with all drivers?
I'd be interested to see a performance comparison with Acki's method too. Does it work with all drivers?
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net