Erwan Daniel
Erwan Daniel

Reputation: 1537

glGetTexImage unexpected results

I try to read texture data from GPU to save it in an image file using glGetTexImage But when I open the image I get a mix of pixel instead of my expected result.

char buffer[size.x * size.y]; // simple one channel buffer
// Copy source to local buffer
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, source);
glGetTexImage(GL_TEXTURE_2D, 0, GL_UNSIGNED_BYTE, GL_ALPHA, buffer);
saveImage("before.pgm", buffer, size.x, size.y);

During the texture creation I do this:

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

// Define texture characteristics with no data. It will be filled later
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size.x, size.y, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);

/* We require 1 byte alignment when uploading or downloading texture data */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

Upvotes: 2

Views: 153

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

You've swapped the format and type argument in the call to glGetTexImage. It has to be:

glGetTexImage(GL_TEXTURE_2D, 0, GL_UNSIGNED_BYTE, GL_ALPHA, buffer);

glGetTexImage(GL_TEXTURE_2D, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);

Since the format and the type are not accepted values, this will cause an INVALID_ENUM error.

Upvotes: 1

Related Questions