Mipmapping in OpenGL

Codesmith picture Codesmith · Apr 13, 2013 · Viewed 7.4k times · Source

I'm having a lot of trouble getting mipmaps to work. I'm using OpenGL 1.1, and I don't have glu, so I'm using the following texture initiation code:

  glGenTextures(1,&texname);
  glBindTexture(GL_TEXTURE_2D,texname);
  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_MAG_FILTER,GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST);
  w=width;h=height;
  for(int i=0;i<mipmaps;i++,w/=2,h/=2)
    glTexImage2D(GL_TEXTURE_2D,i,GL_RGBA8,w,h,0,GL_RGBA,GL_UNSIGNED_BYTE,tex[i]);

Variables:

// data types:
unsigned long int *tex[20];
int mipmaps, width, height, w, h;
GLuint texname;

tex is an array that holds the list of the texture mipmap pixel arrays. The mipmaps are processed correctly (I tested them individually). mipmaps is the number of mipmaps that reduce down the original image to a 1x1 pixel texture (the original texture is 256x256 - so at this point in the code it's 8). width and height are the dimensions of the original texture (256x256).

The result is that it doesn't even use a texture. Everything just appears flat grays (gray due to the lighting).

Is there something I'm forgetting? I've checked this reference, and I can't find any conflicts.

Other details: In total, I'm enabling GL_DEPTH_TEST, GL_TEXTURE_2D, GL_LIGHTING, GL_CULL_FACE, GL_FOG (and GL_LIGHT0, GL_LIGHT1 which probably don't make a difference). Also, I am using Mesa 3D's implementation of OpenGL (Mesa version 4.0 which translates to OpenGL version 1.3) if that might have anything to do with it.

EDIT:

The issue is, the texture works fine (not using mipmaps) the moment I change GL_NEAREST_MIPMAP_NEAREST to GL_NEAREST. So, I can't see how it could be any other code - at least I can't think of anything else it might be.

Answer

derpface picture derpface · Apr 13, 2013

The value of mipmaps is 8. Your image is 256x256. Therefore, you should have 9 levels of mipmapping (256,128,64,32,16,8,4,2,1). If one is missing, you'll lose your texture.