Loading And Finding Textures...

SephirothSephiroth Fayetteville, NC, USA
OK, I have just started trying to load more than my two textures so that I can have up to 32766 textures in any given level. I can't figure out how to properly identify the textures though! How should I do this? My level is made up of up to 4billion "objects", each with a texture name. Now I can easily load the texture that each object has, but if I used the same wall texture on 400 walls, this would load my texture 400 times, wasting resources. Even so, how would I then tell OpenGL which texture to bind to?! Here is what I need:

1) Some check to see if a texture of name "x" has been loaded once, and if so, return and don't load it again.

2) A way to tell OpenGL which texture number to bind to after the textures have been loaded and the view is being drawn. Remember, my level data has a NAME for each texture, but OpenGL requires a the number that texture was bound to when it was generated (mip-mapped).

Thanks in advance for any help I can get. Before I was just loading three textures, and easily referencing them with loop data, but for a REALISTIC world we'll need tons of textures per level.



  • ok, my ideas... not too good, but this was a two minute thought process...

    1) you could use a linked list containing texture/object names with a corresponding texture numbers. if its in the list then dont load it, if it isnt load and add it too the list. not too hard, but not too good when you want speed with 4 billion objects... sheesh

    2) binary or other type of trees. use a texture/object name as the key and the texture number as the data. this would be considerably faster than the linked lists. not too hard to code, but not that fun.

    3) write some hash functions. even more suckier to code, but if you do it right you could save lots of time looking up texture/object information. that is, if you have the hash optimize itself. i probly wouldnt do this, but then i am lazy...

    4) if you are using classes for objects then use a static class member for a texture so you only have to load it once for all objects of that type. i doubt you are using classes for objects, im guessing an object is just a group of vertices.

    as i said this was a two minute thought process, so the best idea is probably still out there. i hope that i have at least given you some inspiration or something... i dunno, its 2 am, im cracked out... blah... ok, bye

  • Hi Seph :)
    i've come accross this problem before. The way I handled it was to have an 'offline' process, where the textures are sorted into a library. Offline just means not ingame. From my experience, the game should be like a king & everything it needs is handed to it on a plate, all sorted, all ready. Anyway, here's the sequence of events..
    In your offline texture library creator, write something that can access all the models that are going to be used in the game. Then, step through each model and find the textures it uses. When you come accross a new one, write the texture to a library file, and save its index. For example....

    load in model "Racecar".
    Racecar has one texture called paintjob.bmp
    Look through your library ( its empty at this stage as its a new one ) for the string "paintjob.bmp"
    cant find it, so write the texture to a file. At this point it'll be the first one, and assign it the index '0'.

    next model....
    load in model "JammyDodger"
    JammyDodger has one texture called pastry.bmp
    Look through your library ( it has one entry at this stage ) for the string "pastry.bmp"
    cant find it, so add the texture to the end of your texture file. At this point it'll be the second one, and assign it the index '1'.

    next model....
    load in model "Viper"
    Viper has one texture called paintjob.bmp, the same as racecar.
    Look through your library ( it has two entry's at this stage ) for the string "paintjob.bmp"
    Its there already, so just copy its index which will be '0'

    Using this method you can build all your textures into one file as well. Which is good for some consoles as the file system on some console cd's have problems with lots of files.It also keeps other people pinching your textures. It also helps keep track of how much space your using.

    Well, its one way to do it :)

    have fun


  • SephirothSephiroth Fayetteville, NC, USA
    Well, here's what I have right now. The program should load both textures in my level (one for the walls and one for the floor/ceiling), but only the floor/ceiling texture shows up, and the walls become invisible!
    //Here are global vars
    char texlist[MAX_TEXTURES][32];
    GLuint texture[MAX_TEXTURES];

    //This is called on when drawing is performed
    unsigned long FindTexture(char *texname)
    char texbuf[32];

    sprintf(texbuf, "%s", texname);
    for(unsigned long loop = 0; loop < MAX_TEXTURES; loop++)
    if(strstr(texlist[loop], texbuf) != NULL)
    return (loop + 1);

    return 1;

    //Here's the bitmap loader...
    BOOL LoadBMP()
    char texbuf[256];
    int texnum = 0;
    BOOL status;
    GLubyte fixer, image[32][32][3];
    FILE *texfile;

    for(unsigned long loop = 0; loop < area.numObjects; loop++)
    status = TRUE;
    for(unsigned long t_loop = 0; t_loop < MAX_TEXTURES; t_loop++)
    if(strstr(texlist[t_loop], area.object[loop].Texture) != NULL)
    status = FALSE;
    sprintf(texlist[texnum], "%s", area.object[loop].Texture);

    glGenTextures(texnum, &texture[0]);

    for(unsigned long loop = 0; loop < texnum; loop++)
    MessageBox(glwnd, texlist[loop], "LoadBMP(unsigned long)", MB_OK);
    sprintf(texbuf, "Textures\%s", texlist[loop]);
    if((texfile = fopen(texbuf, "rb")) == NULL)
    return FALSE;

    fread(&bmfh, sizeof(BITMAPFILEHEADER), 1, texfile);
    fread(&bmih, sizeof(BITMAPINFOHEADER), 1, texfile);
    fread(&image, sizeof(image), 1, texfile);

    for(short int x_loop = 0; x_loop < 32; x_loop++)
    for(short int y_loop = 0; y_loop < 32; y_loop++)
    fixer = image[y_loop][x_loop][0];
    image[y_loop][x_loop][0] = image[y_loop][x_loop][2];
    image[y_loop][x_loop][2] = fixer;

    glBindTexture(GL_TEXTURE_2D, texture[loop]);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 32, 32, GL_RGB, GL_UNSIGNED_BYTE, image);

    return TRUE;

    //And finally, the part of the drawing routine
    //that draws the level...
    glNormal3f(0.0f, 0.0f, 1.0f);
    for(unsigned long o_loop = 0; o_loop < area.numObjects; o_loop++)
    glBindTexture(GL_TEXTURE_2D, FindTexture(area.object[o_loop].Texture));
    for(int t_loop = 0; t_loop < area.object[o_loop].numTriangles; t_loop++)
    glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[0].u, area.object[o_loop].triangle[t_loop].vertex[0].v);
    glVertex3f(area.object[o_loop].triangle[t_loop].vertex[0].x, area.object[o_loop].triangle[t_loop].vertex[0].y, area.object[o_loop].triangle[t_loop].vertex[0].z);

    glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[1].u, area.object[o_loop].triangle[t_loop].vertex[1].v);
    glVertex3f(area.object[o_loop].triangle[t_loop].vertex[1].x, area.object[o_loop].triangle[t_loop].vertex[1].y, area.object[o_loop].triangle[t_loop].vertex[1].z);

    glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[2].u, area.object[o_loop].triangle[t_loop].vertex[2].v);
    glVertex3f(area.object[o_loop].triangle[t_loop].vertex[2].x, area.object[o_loop].triangle[t_loop].vertex[2].y, area.object[o_loop].triangle[t_loop].vertex[2].z);
    Do you see anything wrong with that? Thanks for any help you can provide.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!