Question on Mipmapping

Hello, I’ve some basic questions on mipmapping. I found that level parameter in glTexImage2D is set to ‘0’ when we’d like to generate mipmap. For example:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);

Is it by default OpenGL taking care of mipmaps and we don’t know how many levels will be generated.
Now when we need to explicitly change level from ‘0’ to some other value , i.e. ‘4’ and call glTexImage2D as follows:

glTexImage2D(GL_TEXTURE_2D, 4, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, data);

When do we also need to call the following parameters:

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4);

Any clarification will be highly appreciated!

As clearly stated on the OpenGL Wiki article about allocating storage for textures, glTexImage2D allocates the storage for a single mipmap level. So if you want 5 mipmap levels, you have to allocate 5 mipmap levels, 0 through 4. Each with a call to glTexImage2D.

And you must calculate the size of each mipmap level manually.

You may find ‘gluBuild2dmipmaps’ to be helpful.

But what happened when I don’t explicitly define the mipmap levels:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);

Will OpenGL generate a number of mipmap level automatically?

[QUOTE=Lee_Jennifer_82;1281402]But what happened when I don’t explicitly define the mipmap levels:

Will OpenGL generate a number of mipmap level automatically?[/QUOTE]

You can always read the documentation for glGenerateMipmap, bearing in mind that what it doesn’t say can be as important as what it does say: glGenerateMipmap - OpenGL 4 Reference Pages

In this case, the only completeness requirement is that a cube map or cube map array texture must be cube-complete; regular 2D (etc) textures don’t have a completeness requirement documented so you can infer from that that there is no completeness requirement.

Driver bugs are, of course, always the exception to this rule. If it was me I wouldn’t trust the driver as far as I could throw it to get this right (fair disclosure: I haven’t tested all drivers) so I’d call glTexImage2D with NULL data to specify the other levels first.