Skip to main content
GameDev.net gamedev.net
🔒 Locked

GLubyte array and getPixel function

Started by too_many_stars Mar 19, 2018 at 8:20 PM 2 replies 1.7k views
Original Post
too_many_stars
too_many_stars

Hello Everyone,

I am having issues with extracting a pixel from a GLubyte array.

Here is what I have


GLubyte* pixels = SOIL_load_image( file_path.c_str() , &width, &height, &channels, SOIL_LOAD_AUTO ); // load the pixel array

//later in the code 

//RGB is holds GLint r, g, b

RGB Texture::getPixel( const int x, const int y ) const{

	if( x < 0 || x > width -1 || y < 0 || y > height -1 ) 
		return RGB();

	int w = channels * width;
	int idx = y * w + x;

	GLubyte r = pixels[ idx + 0 ];
	GLubyte g = pixels[ idx + 1 ];
	GLubyte b = pixels[ idx + 2 ];

	return RGB( (GLint) r , (GLint) g , (GLint)  b );

I think I am tying into the array correctly. I do get integer values for my RGB struct, but they are all wrong. I get colors that don't even appear on the texture.

If anyone knows what I am doing wrong, please let me know.


Thanks so much,


Mike

Sponji
Sponji

You are only multiplying y by the number of channels, so how about:


int idx = (y * width + x) * channels;


Derp
too_many_stars
too_many_stars

Hi Sponji,

Thanks so much, that's the correct answer. I thought I was tying into the array properly ( I even did a few examples on paper to make sure it worked).


Mike

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.