Simple OpenGL texture map not working?

I am trying to determine the display of textures in OpenGL, and I cannot get a simple example to work.

A polygon is drawn, although it is not textured, but a solid color. Also, the bitmap loads correctly in sprite1 [], since I have successfully used glDrawPixels so far.

I use glGenTextures to get my tex name, but I noticed that it does not change texName1; this GLuint is what I initialize, even after calling glGenTextures ...

I have included GL_TEXTURE_2D.

Here is 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 is all I tried:

  • Turns out I initialized my texture before initializing OGL. The texture is initialized (glGenTextures-> glTexImage2D) in the class constructor and is drawn (glBegin-> glEnd) in the member function that is called every frame. genTextures seems to work correctly and I get the name 1.

  • Every possible combination of GL_RGBA8, GL_BGRA_EXT (GL_BGRA does not work on my system, I need _EXT), and I even removed the alpha channel from the bitmap and tried all the combinations GL_RGB, GL_BGR_EXT, etc. etc. Bad luck.

  • Tested procedural raster image creation and use of this

  • Make sure GL_COLOR_MATERIAL is not enabled.

  • Resized bitmap to 32x32.

  • Tried glTexEnvi instead of glTexEnvf.

+7
c ++ opengl textures
source share
7 answers

Wow ... Okay, I found a problem. My call to glEnable was glEnable (GL_BLEND | GL_TEXTURE_2D). Using glGetError, I saw that for this call I got GL_INVALID_ENUM, so I moved GL_TEXTURE_2D to my own inclusion and bingo function. I think logical OR is not allowed for glEnable ...?

In any case, thanks to everyone for their help ... for me it was very informative.

+1
source share

Besides mentat note , you may have a problem with texture parameters without the power of two, you indicate that texture generation does not change the name.

It sounds like you are calling glGenTextures () too early, i.e. before initializing OpenGL. If you do not, I suggest adding code immediately after calling glGenTextures() , which checks the OpenGL error status by calling glGetError() .

+6
source share

In your comments, you say that your bitmap is 29x20 pixels in size. Afaik to create a reliable texture, OpenGL requires that the image size (for each dimension) be power 2. It does not have to be a square, but it can be a rectangle. You can overcome this by using some OpenGL extensions, such as GL_ARB_texture_rectangle .

+3
source share

I will put this here because I had the same problem and found another article explaining the problem. The iPhone supports GL_BGRA (GL_EXT_BGRA), but apparently only as an input format and not as an internal format. So, if you changed the call to glTexImage2D to the GL_RGBA internal format, 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 who came across this post.

+2
source share

Some random ideas:

  • GL_COLOR_MATERIAL can be enabled
  • change "glTexEnvf" to "glTexEnvi" and see if that helps
  • If texName1 is 0 after glGenTextures, you may 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 this to find the line that creates the error. Another possibility would be to use something like GLIntercept , BuGLe or gDEBugger .

+1
source share

My OpenGL is rusty, but I remember the same problems with glTexImage2D . Finally I managed to get it to work, but I always had more luck with gluBuild2DMipmaps so I ended up with

 gluBuild2DMipmaps ( GL_TEXTURE_2D, type, i.width, i.height, type, GL_UNSIGNED_BYTE, i.data ); 

which replaced

 glTexImage2D ( GL_TEXTURE_2D, 0, type, i.width, i.height, 0, type, GL_UNSIGNED_BYTE, i.data ); 
+1
source share

The first thing I checked was setting the color material as mentioned by ShadowIce, and then checking the texture file to ensure its reasonable size (e.g. 256x256) and RGB bitmap. If the file has even a small problem, it will NOT display correctly no matter how hard you try.

Then I will stop trying to just debug this code and instead look at what you changed to the tutorial on the NeHe website .

NeHe is always a good place to check if you are trying to do something in OpenGL. Textures are probably the hardest thing to do right, and they only get more complicated as your other GL skills increase.

0
source share

All Articles