Does glGenTextures and glBindTexture needs to be called before doing glTexImage2D and glTexSubImage2D

StackOverflow https://stackoverflow.com/questions/15287172

Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top