Advertisement

Texture saving

Started by September 24, 2003 05:24 PM
4 comments, last by dogan 21 years, 5 months ago
Okay, I''ve been trying for months to write a program that loads textures and then saves them all into one binary file. It couldn''t use MFC, so CFile and CArchive are right out. The only thing I could find was the old c-style fwrite/freaad functions. So far, I have something like: FILE* TextureFile; TextureFile=fopen("Textures.foo","wb"); fwrite(&texture[1],sizeof(GLubyte),1,TextureFile); but it doesn''t work. Any answers?
Marathon Redux: MY mega project.http://marathonredux.forerunners.org
I think there are two errors. The first is that with your fwrite command you only write one byte(sizeof(GLubyte)) not the image. The second error is (I think) that &texture[1] is not the pointer to the image buffer but only to the number that identifies the texture (1,2,3,4..).

If you want to load textures and write them in one file you haven't to use OpenGL, for example you only have to load some bitmaps, that are your textures,and save them in one file.
Here are some examples:

#include "gl/glaux.h"

/* With this function you store the image in a buffer */
AUX_RGBImageRec* LoadBMP(char *file)
{
FILE *f=NULL;
if(!file) return NULL;
f=fopen(file,"r");
if(f)
{
fclose(f);
return(auxDIBImageLoad(file));
}
return NULL;
}

/* AUX_RGBImageRec is a struct:
typedef struct _AUX_RGBImageRec
{
GLint sizeX, sizeY; /*Dimensions of your image*/
unsigned char *data; /*Buffer that contain your texture*/
} AUX_RGBImageRec;
*/

void AppendImageToFile(char* szFile, AUX_RGBImageRec* image)
{
/*You open your file to append your image on it*/
FILE *f=fopen(szFile,"ab");
fwrite(&image->sizeX,sizeof(GLint),1,f);
fwrite(&image->sizeY,sizeof(GLint),1,f);
fwrite(image->data,image->sizeX*image->sizeY*3,1,f); /*for a RGB image the buffer size is 3bytes per pixel and there are sizeX*sizeY pixels*/
fclose(f);
}

I wrote these functions at the moment so they are incomplete and maybe with errors but I hope you'll have an idea of what I mean. ;-)

________________________________________________________
www.wizardofoz.fx.to

[edited by - BioLich on September 24, 2003 6:58:56 PM]
________________________________________________________www.wizardofoz.tk
Advertisement
Thank you! That works great.
Also, one other thing: Nehe mentioned there was a way to overcome the square bitmap limitation, particularly I need to do it with TGAs. Does anyone know what he is talking about?
Marathon Redux: MY mega project.http://marathonredux.forerunners.org
I''m sure there are better solutions but you may try this:
Instead of having many textures you could have few but big square textures with more than one bitmap on them.
For example if you have some rectangle textures(A is 128x64, B is 80x64, C is 48x64) you could create a square bitmap of 128x128 pixel, and then you could copy in it your three images like this:
|-------------|
| A 128x128 |
| |
| |
|_____________|
|B80x64|C48x64|
|-------------|
With this method you are sure that your image is load correctly because it is still a square, but now you have no limitations with size of the textures you put in the square.
And you can do this with any image format (BMP,TGA,JPG..).
I made a mistake and posted anonimously before.. oops!

The correct scheme is this:
|_____________||             ||  A 128x128  ||             ||_____________||B80x64|C48x64||______|______|


________________________________________________________
www.wizardofoz.fx.to

[edited by - BioLich on September 25, 2003 4:45:48 AM]
________________________________________________________www.wizardofoz.tk
@dogan

The method NeHe is refering to is mipmapping he covers it in tutorial 7

This topic is closed to new replies.

Advertisement