Tile maps and glTexSubImage2D

I have had a look through the forum for an answer on this, but I have not been able to find a code snippet that adequately explains what I am doing wrong.

I have a TGA file that contains a couple of “tiles”. The TGA file is 128 x 128 pixels in size and each tile is 64 x 64. i.e. there are four seperate tiles in the image. So obviously I want to be able to select the appropriate image during the drawing routine and glTexSubImage2D seems to be ideal.

glBindTexture(GL_TEXTURE_2D, g_textures[0].texID);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 64, 64, GL_RGBA, GL_UNSIGNED_BYTE, &g_textures[0].imageData);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 100.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(200.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(200.0f, 100.0f);
glEnd();

Using the previous code I thought I was selecting the portion of the texture 64 x 64 pixels in size from the origin 0, 0 of the image (bottom left hand corner). But it crashes the program?

It runs if I select a width and height of 32 x 32, however the entire texture (all 128 x 128) is rendered to the quad with a 32 x 32 black square that corresponds to the above coordinates.

I guess I am misinterperating how this function works?? Any ideas?

TexSubImage is used to update a part of a texture with new data. If you want to map a part of the texture onto your quad, change the texture coordinates instead. They are the ones specifying what part of the texture that maps where to the quad. So for the lower left tile, use these texture coordinates.

glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 100.0f);
glTexCoord2f(0.0f, 0.25f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.25f, 0.25f); glVertex2f(200.0f, 0.0f);
glTexCoord2f(0.25f, 0.0f); glVertex2f(200.0f, 100.0f);

Geez, That simple ay…

Just ONE more question regarding tile maps. If I have a large selection of tiles, with differing sizes contained on the one texture, (for example tiles and creature sprites etc), what would be the best method of storing the information regarding the size of the individual images so that it is possible to dynamically adjust the glTexCoords?

Oh, and just ONE more question… if I have the one texture in memory that contains four tiles… and for each quad (of which there may be 400 on screen), I use only one portion of the texture by changing the texture coordinates… What are the memory implications? How much memory would I be actually using?

Thanks

1: Tricky, but maybe a second file that describes all subimages (like coordinates in the big image, and other usefull info).

2: If you upload the whole image, the texture will of course use as much memory as the image needs, even if you only use a part of it. OpenGL can’t possibly know that you will never use some parts of it until after you have used it, in which case it’s too late to make a decision.

2: If you upload the whole image, the texture will of course use as much memory as the image needs, even if you only use a part of it. OpenGL can’t possibly know that you will never use some parts of it until after you have used it, in which case it’s too late to make a decision.

I assumed it would have to store the entire image in memory, but if I texture 200 quads on screen with a 16kb texture (i.e. I have loaded one texture, but I am applying the texture to multiple poly’s), does that mean that the total video memory I am consuming is 200 x 16kb?

Please forgive my ignorance here - I have not been using OpenGL for long.

No, you’re only using 16kb. The texture is reused each time you use it.

Excellent - thanks very much for your help, much appreciated!