Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple OpenGL texture map not working?

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.

like image 324
Tony R Avatar asked Apr 12 '09 20:04

Tony R


4 Answers

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().

like image 76
unwind Avatar answered Nov 05 '22 07:11

unwind


In your comments, you say your bitmap is 29x20 pixels. Afaik to generate a valid texture, OpenGL requires that the image size (on each dimension) be a power of 2. It doesn't need to be a square, it can be a rectangle though. You can overcome this by using some OpenGL extensions like GL_ARB_texture_rectangle.

like image 21
mentat Avatar answered Nov 05 '22 07:11

mentat


I'll put this here as I had the same issue and found another post explaining the issue. The iPhone does support GL_BGRA(GL_EXT_BGRA) but seemingly only as an input format and not as an internal format. So, if you change the glTexImage2D call to have an internal format of GL_RGBA then it works.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, &sprite1[54]);

I hope this helps someone else that stumbles upon this post.

like image 2
Christian Avatar answered Nov 05 '22 06:11

Christian


Some random ideas:

  • GL_COLOR_MATERIAL might be enabled
  • change "glTexEnvf" to "glTexEnvi" and see if that helps
  • if texName1 is 0 after glGenTextures you might not have an active OpenGL context

For error checking I recommend writing a small function that prints readable output for the most common results from glGetErrors and use that to find the line that creates the error. Another possibility would be to use something like GLIntercept, BuGLe or gDEBugger.

like image 1
Maurice Gilden Avatar answered Nov 05 '22 08:11

Maurice Gilden