PDA

View Full Version : Store a 2D texture to file



ionas_
02-26-2015, 02:20 AM
I am working on a project where i am working on the parameterization of a mesh as a 2D array. Anyway, at this point i had to switch to OpenGL and as i am new to it i would like to ask how i can save textures created as an image (*.bmp, *.jpg e.t.c.).



unsigned int uiTex[2];
int iNRes=1024;
int res =1024;
glGenTextures(2, uiTex);
glBindTexture(GL_TEXTURE_2D, uiTex[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, res, res, 0, GL_RGBA, GL_FLOAT, NULL);
glBindTexture(GL_TEXTURE_2D, uiTex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iNRes, iNRes, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

......(here is the part where the image is created)


An example of saving the created texture to a bmp file would help me a lot!

mhagain
02-26-2015, 04:42 AM
glGetTexImage (https://www.opengl.org/sdk/docs/man3/xhtml/glGetTexImage.xml) will get you the texture data which you can then save out; I suggest using TGA instead of BMP or JPG because the returned pointer from glGetTexImage can be written immediately after the TGA header without any further processing.

ionas_
02-26-2015, 06:13 AM
Thanks for the quick reply. Having the pointer from the function glGetTexImage, how can i save the image in the format you mentioned?



unsigned int uiTex[2];
int iNRes=1024;
int res =1024;
glGenTextures(2, uiTex);
glBindTexture(GL_TEXTURE_2D, uiTex[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, res, res, 0, GL_RGBA, GL_FLOAT, NULL);
glBindTexture(GL_TEXTURE_2D, uiTex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iNRes, iNRes, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

......(here is the part where the image is created)

glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, gim);

Alfonse Reinheart
02-26-2015, 07:00 AM
You read the TGA file specification (http://www.paulbourke.net/dataformats/tga/) and write bytes that conform to it.

Unless you expect us to write your code for you, you're going to have to ask a more specific question than that. Personally, I'd just use an image library that has saving capabilities. But the TGA format isn't difficult to write.

GClements
02-26-2015, 10:37 AM
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, res, res, 0, GL_RGBA, GL_FLOAT, NULL);


An example of saving the created texture to a bmp file would help me a lot!

Most image formats can't handle GL_RGBA32F (BMP certainly can't). TIFF probably can (it tries to support everything), but I doubt that much software which "supports TIFF" can handle it.

Most formats are limited to 8 bits per component; a few support 16 bits per component. For 32-bit floats, you might be better off using a generalised container format such as HDF.

ionas_
02-27-2015, 12:14 AM
Thanks a lot i.. An image format that deals with floating points is what i need! (e.g ppm). i'll check *.hdf