7
\$\begingroup\$

I am trying to manually build a mipmap from a series of images. For the sake of brevity, let's assume the file containing the images I want (from 256x256 to 32x32) have paths file_1...4.

What I do to load the texture is:

GLuint textureID;
glGenTextures(1,&textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4);

for i from 1 to 4
    unsigned char* image =  stbi_load_from_file(file_i, &width, &height, &comp, 3);
    glTexImage2D(GL_TEXTURE_2D, (i-1), GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    stbi_free_image(image);

When I go and try to use this created texture in a render all I got is a black result. Using singularly each texture works properly, so I excluded they're malformed. What can it be?

\$\endgroup\$
3
  • \$\begingroup\$ You have 4 mip-levels, correct? From 0 to 3. If yes, then GL_TEXTURE_MAX_LEVEL should be 3, not 4. \$\endgroup\$
    – glampert
    Commented Oct 4, 2014 at 1:45
  • \$\begingroup\$ Oh my what a stupid error I did, thank you @glampert ! \$\endgroup\$
    – gufranc
    Commented Oct 6, 2014 at 6:36
  • \$\begingroup\$ Ok, I'll post an "official" answer then ;) \$\endgroup\$
    – glampert
    Commented Oct 6, 2014 at 13:47

1 Answer 1

5
\$\begingroup\$

The only apparent error in your code is that you seem to specifying 4 mip-levels, from level 0 to level 3, however, you set GL_TEXTURE_MAX_LEVEL to 4. The max level is the zero-based index of the last mip-level, so in this case, it should be 3:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
\$\endgroup\$
1
  • \$\begingroup\$ Per documentation the default value for this is 1000, which suggests that values too high just get silently clamped. It's shouldn't be normally required to set this unless you're not loading a full mipmap chain. opengl.org/sdk/docs/man/html/glTexParameter.xhtml \$\endgroup\$ Commented Oct 6, 2014 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .