Simple OpenGL texture map not working?

2019-02-07 00:06发布

I'm trying to figure out texture mapping in OpenGL and I can't get a simple example to work.

The polygon is being drawn, though it's not textured but just a solid color. Also the bitmap is being loaded correctly into sprite1[] as I was successfully using glDrawPixels up til now.

I use glGenTextures to get my tex name, but I notice it doesn't change texName1; this GLuint is whatever I initialize it to, even after the call to glGenTextures...

I have enabled GL_TEXTURE_2D.

Heres the code:

GLuint texName1 = 0;

glGenTextures(1, &texName1);
glBindTexture(GL_TEXTURE_2D, texName1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, sprite1[18], sprite1[22], 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, &sprite1[54]);

glColor3f(1, 1, 0);
glBindTexture(GL_TEXTURE_2D, texName1);
glBegin(GL_QUADS);
    glTexCoord2f (0.0, 0.0);
    glVertex3f (0.0, 0.0, -5.0f);
    glTexCoord2f (1.0, 0.0);
    glVertex3f (.5, 0.0, -5.0f);
    glTexCoord2f (1.0, 1.0);
    glVertex3f (.5, .5, -5.0f);
    glTexCoord2f (0.0, 1.0);
    glVertex3f (0.0, .5, -5.0f);
glEnd();

UPDATE: I'm at a loss. Here's everything I've tried:

  1. Turns out I was initializing my texture before OGL was initialized. The texture is initialized (glGenTextures->glTexImage2D) in a class constructor and drawn (glBegin->glEnd) in a member function that is called every frame. genTextures appears to be working correctly now and I'm getting a name of 1.

  2. Every possible combination of GL_RGBA8, GL_BGRA_EXT (GL_BGRA doesn't work on my system; I need the _EXT), and I even removed the alpha channel from the bitmap and tried all combinations of GL_RGB, GL_BGR_EXT, etc etc. No luck.

  3. Tried procedurally creating a bitmap and using that

  4. Made sure GL_COLOR_MATERIAL isn't enabled.

  5. Changed bitmap size to 32x32.

  6. Tried glTexEnvi instead of glTexEnvf.

7条回答
在下西门庆
2楼-- · 2019-02-07 01:07

In addition to mentat's note that you might have a problem with non-power-of-two texture dimensions, you mention the texture name generation not changing the name.

That sounds as if you're calling glGenTextures() too early, i.e. before initializing OpenGL. If you're not, then I suggest adding code just after the call to glGenTextures() that check the OpenGL error state, by calling glGetError().

查看更多
登录 后发表回答