Sign in to follow this  
RegularKid

Grabbing texture pixel values

Recommended Posts

RegularKid    139
Hi. I've created a texture from the back buffer using glCopyTexImage2D. So, I have a GLuint holding the texture ID (which is 128x128), however I would like to be able to grab out each pixel value from the texture. So, if I was to write the texture out to a textfile, I would be able to do the following:
{
	FILE *outFile;
        unsigned long pixelRGBValue;

	outFile = fopen("Texture.txt", "wt");

	for(int i = 0; i < 128; i++)
	{
		for(int j = 0; j < 128; j++)
		{
			fprintf(outFile, "%08x,", pixelRGBValue);
		}
		fprintf("\n");
	}

	fclose(outFile);
}

This obviously doesn't work since pixelRGBValue doesn't hold anything. Anyone know how to grab each pixel RGB value out of the texture? Any help would be great!

Share this post


Link to post
Share on other sites
soconne    105
Here is your function you need to use:

unsigned char *pixelData;

glBindTexture(GL_TEXTURE_2D, TextureID);
glGetTexImage(GL_TEXTURE_2D, 0, (GL_RGB or GL_RGBA), GL_UNSIGNED_BYTE, (void *)pixelData);


Then pixelData will be filled with the texture's pixels. Set GL_RGB or GL_RGBA according to if your texture has an alpha channel or not.

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