loading time

Started by
38 comments, last by q3stanky 22 years, 3 months ago
Well, I''ve used my code in actual programs--and the textures load and work perfectly.

So, the code *works*, as is.

But you''re right. Your way of doing it is more tidy, has less looping, and is better.

Thnx...I think I''ll try to modify it sometime in the future.
Advertisement
Hmm.

Using the BMP format is a really bad idea for OpenGL, IMO. Go with your own texture format. If you do the whole converting, swapping and mipmap building offline, it doesn''t matter how fast or slow your loaders are.

- AH
Refering to the last post,
yes it would be easily to edit/create some bitmaps ( or other file formats you know and your graphic software can handle ) with your graphic software and then writing a conversion ultility which loads the bitmap ( or ... ) and creates the mipmaps and so on.
So that you only have to load your texture format at runtime which is much faster than loading the data plus the swaping around.

Hint1: To create a file loader yourself, you only have to lookup the file format specificatiion and then write a routine that reads out the file with the help of information which is coming with the file because of it''s file format which specs you''ve looked up.

Hint2: You don''t have to write a routine that handles all versions of different storing supported by the file format,
if you don''t use all the different versions.

Hope that helped a little.

Hint3: Go to http://www.wotsit.org that will definitely help

- AH
Just a small hint

instead of making 30 TextureImage[] spaces, just use one (the first one or a pointer to it, ie TextureImage[0]) and load each into that one at a time, then make your map off it, then clear it, load the next one and so forth. This would really slow down the system with the 64 mb of ram as it has to have 16 to 20 to keep windows running happily, then it has to load the rest of the data in twice and then it just clears the TextureImage[] (30 of them) afterwards. Makes the hard drive chug around alot, and if you have to access two points on a hard drive it really slows it down.

PS - also a reason to defrag.
Beer - the love catalystgood ol' homepage
sorry, didn''t realise it was already two pages and taht someone posted an example that showed what I jsut said.
Beer - the love catalystgood ol' homepage
Just to go off on a tangent for a moment...

Is it just me, or does the process of creating mipmaps positively scream out for a recursive function? In essence, you keep dividing the size of the texture until it gets to 1x1. At which point you could return a large list of all the steps for easy shoving into a file.
"Diplomacy is the ability to tell someone to go to tell in such a way as they look forward to the trip."
Recursive ? Essentially yes, it is a recursive algorithm. Closely related to quadtree algorithms, although only one leaf of each depth level is followed, not 4. But imagine the stack size you would need for a 2048*2048 (or more) texture... And it wouldn''t be very efficient either.

That''s why every mipmap generator I''ve seen uses an iterative approach: use the previously generated mipmap level to create the current one.

- AH
On a second thought, you wouldn''t need much stack space at all. Because only one leaf is followed at each recursive level. But on the other hand, this defeats the whole recursive idea, since your recursion essentially degrades into an iteration (only one recursive call is performed at each level).

- AH
You could do something like so:

  /* assuming you have this defined in your program already!.int texture[30]; //Or array of textures #''s!!*/struct MyBitmap{ unsigned short Width; unsigned short Height; unsigned long TotalSize;// unsigned char BitDepth;//Don''t need bit depth, assuming 24-bit unsigned char *img;};//sx,sy are sizex and sizey//Function returns a pointer to bitmap in memory.unsigned char LoadBitmap(char *filename, MyBitmap &mb){ unsigned char *tmp; FILE *in; short tx,ty; mb.img = NULL; //Initialize to NULL! unsigned long tSize, offset1, offset2=0; in = fopen(filename,"rb"); //Read binary! if (!in) //in == NULL  return 0; //Bitmap not loaded!/*  If you are positive you''re only loading bitmaps, it''s a waste to test! if (getc(in)!=''B'' || getc(in)!=''M'') {  //Not a valid bitmap file!  fclose(in);  return 0; }*/ fseek(in,18,0); mb.Width = getw(in); //Get Width fseek(in,22,0); mb.Height = getw(in); //Get Height/* don''t need to get bit-depth! fseek(in,28,0); mb.BitDepth = getc(in);*/ mb.TotalSize = mb.Width*mb.Height*3; mb.img = new unsigned char[mb.TotalSize]; if (mb.img==NULL) {  fclose(in);  return 0; //Not enough memory! } tmp = new unsigned char[mb.TotalSize]; if (tmp==NULL) //Not enough for our buffer! {//you could just load from file instead of memory if this fails.. but waste of time :o)  fclose(in);  free mb.img; //Free image;  return 0; } fread(tmp,1,mb.TotalSize,in); //Read in complete image! offset2=0; for (ty=mb.Height-1;ty!=-1;--ty) //bitmap is upside down!! {  offset1 = ty*mb.Width*3;  for (tx=0;tx!=mb.Width;++ty) //And left to right!  {//Inverts Y and R&B values at the same time!   mb.img[offset1+2]=tmp[offset2];   mb.img[offset1+1]=tmp[offset2+1];   mb.img[offset1]=tmp[offset2+2];   offset2+=3;   offset1+=3;  } } fclose(in); return 1;}int LoadTexture(char *filename){ MyBitmap mb; int tNum; if (!LoadBitmap(filename,mb))  return -1; //File not loaded! glGenTextures(1, &tNum); //Generate 1 texture! glBindTexture(GL_TEXTURE_2D, tNum); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, mb.Width, mb.Height, GL_RGB, GL_UNSIGNED_BYTE, mb.img); free mb.img; //Free memory! return tNum; //Returns the texture number!}char InitTextures(void){ int ctr; //No need to initialize texture[]. texture[0]=LoadTexture("Data/mars.bmp"); texture[1]=LoadTexture("Data/sonne2.bmp");// finish loading textures like above! for (ctr=0;ctr!=30;++ctr) //Loop through all 30!  if (texture[ctr]==-1)   return 0; //Texture not found! return 1; //All textures loaded!!}int main(void){//Simply check: if (!InitTextures()) {  //Print error loading textures error here!  return -1; }//Proceed.. textures are now loaded!}  


Disclaimer: I wrote this in this little text box window, and it is untested, and may contain syntax and/or grammer errors. If this doesn''t work or you cannot get this to work properly, I can test this and get it in working order need be.

BillyB

ps. If you or anyone has any questions pertaining this response, or any other questions about bitmap loading in general, feel free to contact me at BillyB@mrsnj.com.

This topic is closed to new replies.

Advertisement