1

Hi I have 35 images to draw in a display. I am drawing it in 7X5 grid. The images are downloaded from internet. Each time an image is downloaded I try to draw the whole 35 images. But some of them are not downloaded yet. So I draw some default tile for them instead. The problem is every time an image is downloaded I am drawing the previously drawn images again too. I want to reduce it. So I was thinking about doing something like Texture Atlas. I am trying to do it manually. I am doing it by making a big image with glTexImage2D and adding subimages to it with glTexSubImage2D.

glGenTextures(1, tex);
glBindTexture(GL_TEXTURE_2D, (*tex));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, textureImageInfo->format, textureImageInfo->texWidth, textureImageInfo->texHeight, 0, textureImageInfo->format, GL_UNSIGNED_BYTE, NULL);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, textureImageInfo->imageWidth, textureImageInfo->imageHeight, textureImageInfo->format, GL_UNSIGNED_BYTE, textureImageInfo->image);

I call 35 glTexSubImage2D to add all 35 images to a big glTexImage2D. Here I wrote only one for easier explanation. Then finally I do

glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, this->tileCoordList); 
glTexCoordPointer(3, GL_FLOAT, 0, this->tileTextureCoordList);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);

Now what I am confused with is do I need to generate and bind textures 35 times too for 35 call of glTexSubImage2D ?? Or just doing once is enough. The actual problem I don't understand whats binding the texture has got to do anything with it. Thanks.

Tahlil
  • 2,550
  • 4
  • 36
  • 71
  • If you only have one texture then you only need to get one texture name from glGenTextures() and one call to glBindTexture(). – rhashimoto Mar 08 '13 at 05:21
  • so I need 35 calls of glGenTextures() here? Because I am adding 35 texture subimages with `glTexSubImage2D`. – Tahlil Mar 08 '13 at 05:43
  • 1
    No. You have one texture. `glTexSubImage` fills part of your one texture. – rhashimoto Mar 08 '13 at 06:11
  • 1
    As for "*The actual problem I don't understand whats binding the texture has got to do anything with it.*", [I've answered that too often](http://stackoverflow.com/questions/8866904/need-help-understanding-the-differences-and-relationship-between-glactivetexture/8887844#8887844) to [keep answering it over and over again](http://www.opengl.org/wiki/OpenGL_Object). – Nicol Bolas Mar 08 '13 at 06:13

1 Answers1

8

There is no such thing as a "subimage" in a texture. There are only images in a texture. glTexImage2D allocates storage for a particular image in the texture, and optionally uploads data to that image. glTexSubImage2D only uploads data to an image. The "sub" means that you can update part of the image, not necessarily all of it.

glTexImage2D is like malloc followed by memcpy. glTexSubImage2D is just a memcpy. That's why you have to call glTexImage2D first.

Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829