Jump to content
  • Advertisement
Sign in to follow this  
famous

glGetTexImage returns 0

This topic is 2856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey,

I'm trying to export an 8 bit depth map to a tga image. Unfortunately glGetTexImage just sets outputImage to 0. As a reference I also posted my PNG export function which works perfectly well. Any ideas where my mistake is?

void ParallaxTest::SaveTextureToTGA(GLuint texture, char *filename)
{
glBindTexture(GL_TEXTURE_2D, texture);

int width, height, format;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&width);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&height);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTERNAL_FORMAT,&format);

unsigned char *outputImage = 0;

// Allocate the neccessary memory.
outputImage = (unsigned char*)malloc(width * height * 3);

// Clear the variable.
memset(outputImage, 0, width * height * 3);

glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_BYTE, outputImage);

// Call our WriteTGA() function to write the tga.
WriteTGA(filename, width, height, (unsigned char*)outputImage);

// Clear the allocated memory.
free(outputImage);
}




void ParallaxTest::SaveTextureToPNG(GLuint texture, std::string Filename)
{
glBindTexture(GL_TEXTURE_2D, texture);

int textureWidth, textureHeight, textureInternalFormat;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&textureWidth);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&textureHeight);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_INTERNAL_FORMAT,&textureInternalFormat);

std::vector<unsigned char> TexData, PngData;

int Pack;
glGetIntegerv(GL_PACK_ALIGNMENT, &Pack);
TexData.resize(textureWidth*textureHeight*4);

glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &TexData[0]);

LodePNG::Encoder encoder;
encoder.encode(PngData, TexData, textureHeight, textureWidth);
LodePNG::saveFile(PngData, Filename);
}

Share this post


Link to post
Share on other sites
Advertisement
SaveTextureToPNG fetches textureInternalFormat but never uses its value. The glGetTexImage call in that function explicitly specifies the format GL_RGBA. SaveTextureToTGA fetches "format", but is the *internal format*. If you test its value, you will probably see (GL_RGBA8) 0x8058. glGetTexImage wants the "format" not the "internal format".

Share this post


Link to post
Share on other sites
yep, it works now. Though for some reason my output texture is now horizontally mirrored. Any idea why this is? Here is the export function

bool ParallaxTest::WriteTGA(char *file, short int width, short int height, unsigned char *outImage)
{
// To save a screen shot is just like reading in a image. All you do
// is the opposite. Istead of calling fread to read in data you call
// fwrite to save it.

FILE *pFile; // The file pointer.
unsigned char uselessChar; // used for useless char.
short int uselessInt; // used for useless int.
unsigned char imageType; // Type of image we are saving.
int index; // used with the for loop.
unsigned char bits; // Bit depth.
long Size; // Size of the picture.
int colorMode;
unsigned char tempColors;

// Open file for output.
pFile = fopen(file, "wb");

// Check if the file opened or not.
if(!pFile) { fclose(pFile); return false; }

// Set the image type, the color mode, and the bit depth.
imageType = 2; colorMode = 3; bits = 24;

// Set these two to 0.
uselessChar = 0; uselessInt = 0;

// Write useless data.
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);

// Now image type.
fwrite(&imageType, sizeof(unsigned char), 1, pFile);

// Write useless data.
fwrite(&uselessInt, sizeof(short int), 1, pFile);
fwrite(&uselessInt, sizeof(short int), 1, pFile);
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
fwrite(&uselessInt, sizeof(short int), 1, pFile);
fwrite(&uselessInt, sizeof(short int), 1, pFile);

// Write the size that you want.
fwrite(&width, sizeof(short int), 1, pFile);
fwrite(&height, sizeof(short int), 1, pFile);
fwrite(&bits, sizeof(unsigned char), 1, pFile);

// Write useless data.
fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);

// Get image size.
Size = width * height * colorMode;

// Now switch image from RGB to BGR.
for(index = 0; index < Size; index += colorMode)
{
tempColors = outImage[index];
outImage[index] = outImage[index + 2];
outImage[index + 2] = tempColors;
}

// Finally write the image.
fwrite(outImage, sizeof(unsigned char), Size, pFile);

// close the file.
fclose(pFile);

return true;
}

Share this post


Link to post
Share on other sites
It`s really simple answer:
in math and image-orientation u got numbered XY from left-BOTTOM BUT at computer viewport u got XY from left-TOP. So when u get image from viewport and than u wanna write it to image(PNG/TGA/BMP) u r getting horizontal mirrored picture

Share this post


Link to post
Share on other sites
I do query an image, though...
Additionally my PNG export works perfectly fine. Does that mean I have to switch the "wrong" indices by hand? Seems to be a pretty complicated formula...

Share this post


Link to post
Share on other sites
I think that's only way unfortunately. Mirroring isnt so hard u just have to think about it:)
The simplest way: copy everything to tab of pixel[width][height] and swap like:
for(int i=0;i<height/2;i++)
{
swap(pixel[x],pixel[x][height-i]);
}
You can do this another way(more efficient and harder) but you need to think more

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!