Texture pixel color

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
wolfulus
Posts: 4
Joined: Thu May 31, 2012 6:44 am

Texture pixel color

Post by wolfulus »

Hello guys, I'm new to Irrlicht, and I need to know: how do I get a pixel's color from a texture?

I do have a texture that is being updated every single frame (from an external buffer) using lock()/unlock(), and when user clicks the texture, I need retrieve the color at that specific point.

Can I create and drop an IImage every frame? Isn't that too expensive? :(
English is not my main language and I'm self-taught...
so please correct me if I write something wrong!
:)
Klunk
Posts: 264
Joined: Mon Jan 10, 2011 5:21 pm

Re: Texture pixel color

Post by Klunk »

are we talking about a texture on a 3d object or a 2D screen space image here ?
wolfulus
Posts: 4
Joined: Thu May 31, 2012 6:44 am

Re: Texture pixel color

Post by wolfulus »

Its just an ITexture being painted (2D) on the screen... and I do have the (x, y) of the pixel in the texture... my problem is just retrieving its color.
English is not my main language and I'm self-taught...
so please correct me if I write something wrong!
:)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Re: Texture pixel color

Post by greenya »

Here you can see how to set color - http://irrlicht.sourceforge.net/forum/v ... =7&t=38997
"Getting color" is almost the same. Ask here for more if you cannot understand.
wolfulus
Posts: 4
Joined: Thu May 31, 2012 6:44 am

Re: Texture pixel color

Post by wolfulus »

Seems like video::SColor doesn't have any getData member function. Refactored or just removed? I understood your logic, I'll try that without getData..

@@ EDIT @@

Well, I think I got it working now... thanks, I was doing the exact same thing as you did, but my math failed failed when getting the correct buffer position :)

Here's the code working... Both Code Block 1/2 works fine...

Code: Select all

 
 
irr::video::SColor Almirante::HtmlWindow::getPixelColor( int x, int y )
{
        SColor pixel = SColor(0, 0, 0, 0);
 
        if (!this->_texture) 
                return pixel;
 
        if (x < 0 || y < 0) 
                return pixel;
 
        if ( x >= this->_width || y >= this->_height) 
                return pixel;
 
        ///
        /// Code Block 1
        ///
 
        // auto video =  IrrlichtManager::instance()->getVideoDriver();
        // auto image = video->createImage(this->_texture, vector2d<s32>(0, 0), this->_texture->getOriginalSize());
        // pixel = image->getPixel(x, y);
        // image->drop();
        // return pixel;
 
        ///
        /// Code Bock 2
        ///
 
        auto pitch = this->_texture->getPitch();
        auto format = this->_texture->getColorFormat();
        auto bytes = video::IImage::getBitsPerPixelFromFormat(format) / 8;
 
        unsigned char* buffer = (unsigned char*) this->_texture->lock();
        if (buffer)
        {
                pixel = SColor(*(unsigned int*)(buffer + (y * pitch) + (x * bytes)));
                this->_texture->unlock();
        }
 
        return pixel;
 
}
 
 
But I have another question: does it has any difference at all (block 1 / block 2)? I mean... when creating an IImage from the texture, it does a copy of the texture buffer or just uses a pointer to the original buffer?

I'll be using block 2 anyways...

:)
English is not my main language and I'm self-taught...
so please correct me if I write something wrong!
:)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Texture pixel color

Post by hybrid »

Yes, in this case it would copy over the whole buffer. So block2 is faster.
wolfulus
Posts: 4
Joined: Thu May 31, 2012 6:44 am

Re: Texture pixel color

Post by wolfulus »

I see, thanks!
English is not my main language and I'm self-taught...
so please correct me if I write something wrong!
:)
Post Reply