PDA

View Full Version : Creating textures ???



Bruno
11-13-2000, 12:40 PM
Hi
Ok, i have a texture created inside Opengl with several calculations .., why can a save it to a disk, so that i can load it again, but without all of the previous computation ??

thanks
Bruno

frogger
11-13-2000, 03:20 PM
I assume that you don't care what format the texture is in or more specifically you just want to be able to reload the texture into opengl..... you could just dump the information to a binary file..... i.e. make your own format which would contain information like the size of the texture the format and of course the texture itself.... then just read it in as you would any other binary file ..... there is nothing really special about this procedure..... you are just dumping the info to a file .... this of course is the case abecause OpenGL does not support any particular (or any at all) image format..... so you have to write your own... This is essentially the same process as reading in any other file format though ...

you read some header
then you read in the info
and translate it to your needs....

your file format may be like this

in the header

width/height of texture
internal format of texture
type of texture (2d or 1d) (prob not necessary)
internal format of texture (e.g. float, byte... etc)
a dump of the texture itself.....

end of file


now.... you will have to be careful if you are using binary data when you move to other machines that use big/little endian data, but you have to do that with any binary (just thought I would remind you)

Anyway hth

frogger **ribbit**

Bruno
11-14-2000, 01:14 AM
Thanks for the answer, but what i really need to know is how do i do that!
Suppose i have the texture in this variable
GLint Texture[0];
How do i read the pixel values from there, so that i can start writing to a file?

Thanks
Bruno

frogger
11-14-2000, 07:59 AM
I assume that Texture[0] is just the name of the texture you created.... this is not what you need what you need is the data that you created for the texture

e.g.

#include <fstream.h>

GLuint Texture[1];
glGenTextures(1, Texture);
glTexImage2D....

...

ImageWidth=width of image
ImageHeight=Height of image

//this is what you need
GLubyte Image[checkImageHeight][ImageWidth][4];

.... create texture here however you are doing it (by getting it from a .bmp .jpg whatever... (Is this what you need help with?)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ImageWidth, ImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image);

....

Then if you want to write to a file you can do it several ways

e.g.

void writeImage(GLubyte * image, unsigned int width, unsigned int height, int format, int pixeltype)
{
//format and pixeltype will be GL_UNSIGNED_BYTE and GL_RGBA respectively in this case
//using streams
ostream out;

out.open("filename", ios::binary );

out<<width;
out<<height;
out<<format;
out<<pixeltype;

for (int i=0 i<height; i++)
{
for(int j=0; j<width; j++)
{ out<<Image[width][height][0];
out<<Image[width][height][1];
out<<Image[width][height][2];
out<<Image[width][height][3];
}
out.close();//this returns an int (I think) so you can check for errors....
}
}



[This message has been edited by frogger (edited 11-14-2000).]