maverick9888
maverick9888

Reputation: 512

Using glGenerateMipmap() with glCopyTexImage2D()

I wish to upload textures with non-zero mipmap levels using glCopyTexImage2D(). I am using following code for the same :

// Render Some Geometry

GLint mipmap_level = 1;

glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);  

glCopyTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,mipmap_level,GL_RGBA16_SNORM,0,0,128,128,0);
// Five other glCopyTexImage2D() calls to load textures

glGenerateMipmap(GL_TEXTURE_CUBE_MAP);

//Render geometry

Here, if I use mipmap_level = 1 the geometry is not drawn at all. How exactly does mipmap levels work in conjunction with glCopyTexImage2D() API ? I suppose that using level = 1 , loads 64x64 texture i.e. the first sampled mipmap. Using glGenerateMipmap() call before glCopyTexImage2D() will not make any sense. So how exactly the driver will load a non zero mipmap level using glCopyTexImage2D() ?

Upvotes: 0

Views: 1902

Answers (1)

Christian Rau
Christian Rau

Reputation: 45948

You first set the image for mipmap level 1 and then you call glGenerateMipmap to automatically generate mipmaps from the image of the first mipmap level, which is mipmap level 0. So all mipmap images following level 0 just get overwritten by the automatically generated images. glGenerateMipmap doesn't care how you set the image for mipmap level 0, it just assumes you did. On the other hand I don't see you specifying an image for level 0, so it will probably just contain rubbish or be regarded as incomplete, which will make any use of the corresponding texture fail.

In the end I hope this question doesn't just amount to knowing that in most programming languages one usually starts to count at 0 instead of 1, and so does OpenGL.

EDIT: As datenwolf points out in his comment, you can change the base mipmap level to be used for mipmap-filtering and as input for glGenerateMipmap by doing a

glTexParamteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 1);

But don't do this just because you want to start counting at 1 instead of 0. If on the other hand you have a valid image for mipmap level 0 and want to set one for level 1 and use this for computing the other levels, then changing the base level to 1 before calling glGenerateMipmap (and probably back to 0 aftwerwards) can be a good idea, though I cannot come up with a usecase for this approach right away.

Upvotes: 2

Related Questions