NightKn8
NightKn8

Reputation: 379

Texture is not loading through glGenerateMipmap()

I can't load texture through glGenerateMipmap on my cube.

I load the BMP texture from resource.h file.

My load function is called on WM_CREATE and looks like this:

void LoadTEX()
{
 GLuint texture;  
 HBITMAP GLtex;
 BITMAP tex;
 byte Texture=TEX;
 GLtex= (HBITMAP)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(Texture), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);

  GetObject(GLtex,sizeof(tex), &tex);
  glPixelStorei(GL_UNPACK_ALIGNMENT,sizeof(Texture));
  glEnable(GL_TEXTURE_2D);
  glGenTextures(sizeof(Texture), &texture);
  glBindTexture( GL_TEXTURE_2D, texture );
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
  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);
  glTexImage2D( GL_TEXTURE_2D,
            0,
            GL_RGB,
            tex.bmWidth,
            tex.bmHeight,
            0,
            GL_RGB,GL_UNSIGNED_BYTE,
            tex.bmBits);

 glDeleteTextures(sizeof(Texture), &texture);
}

On WM_PAINT:

 glColor3f(1.0f,1.0f,1.0f);
 glEnable(GL_TEXTURE_2D);
 glBindTexture( GL_TEXTURE_2D, texture );
 // glActiveTexture(GL_TEXTURE0);
 // glGenerateMipmap(GL_TEXTURE_2D);
 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, tex.bmWidth, tex.bmHeight, GL_RGB, GL_UNSIGNED_BYTE, tex.bmBits);

The above code works fine (texture is in place) but the problem is that it gives me 40 FPS which is very poor result (without texture been bonded I get around 300 FPS).

Second is that I don't want to use gluBuild2DMipmaps because it's a bit old, instead I want to use glGenerateMipmap. Problem is that if I comment out gluBuild2DMipmaps and uncomment glActiveTexture(GL_TEXTURE0);, glGenerateMipmap(GL_TEXTURE_2D) nothing is shown.

My PIXELFORMATDESCRIPTOR looks like:

PIXELFORMATDESCRIPTOR pfd;
int format;

// get the device context (DC)
*hdc = GetDC( hwnd );

// set the pixel format for the DC
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat( *hdc, &pfd );
SetPixelFormat( *hdc, format, &pfd );

All this running on ATI and OPENGL 3.1 support. I call the glEnable(GL_TEXTURE_2D); so this should work with ati cards so the problem is elsewhere.

Upvotes: 0

Views: 2835

Answers (2)

Victor  Laskin
Victor Laskin

Reputation: 2647

I use the following order during loading of texture:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, n2width, n2height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixmap);

glHint(GL_GENERATE_MIPMAP_HINT, GL_FASTEST);//GL_FASTEST);
glGenerateMipmap(GL_TEXTURE_2D); // Generate mip mapping

And dont rebuild mipmaps on every frame.

Upvotes: 0

jozxyqk
jozxyqk

Reputation: 17416

In LoadTEX, don't delete the texture straight after loading it. (Also it should be glGenTextures(1, &texture) and glDeleteTextures(1, &texture) to generate and delete one texture handle)

Then call glGenerateMipmap after loading it with glTexImage2D. Unless the texture changes and you need to regenerate the mipmaps each frame, in which case leave the call where it is.

glActiveTexture(GL_TEXTURE0) is the default anyway. If you were binding other textures at the same time you'd use glActiveTexture(GL_TEXTURE1) or glActiveTexture(GL_TEXTURE0 + textureIndex) etc.

I'm not sure on the specifics of windows bitmap loading, but double check the glPixelStorei line, given the other sizeof(Texture) uses I have to assume this may be incorrect too. To be safe, just set it to 1 byte.

Upvotes: 2

Related Questions