Search code examples
androidopengl-esopengl-es-2.0texture2d

OpenGL ES2.0 glTexImage2D() returns GL_INVALID_VALUE


I have a problem trying to generate a texture (for a font atlas) on one of my Android devices (Samsung N5100 tablet). The below code works fine on iOS and on my Samsung S4.

glTextImage2D() keeps returning GL_INVALID_VALUE.

At first I thought it was a power of 2 issue but I've tried hardcoding various power of 2 values and it continues to fail. GL_MAX_TEXTURE_SIZE returns a value of 4096 but I can't even get 16x16 to work.

  glActiveTexture( GL_TEXTURE0 );
  glGenTextures( 1, &m_TextureAtlasStrip );
  glBindTexture( GL_TEXTURE_2D, m_TextureAtlasStrip );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );

  if( CheckGLError() )
    LogDebugf( "FAIL(9)" );

  GLint f = 0;
  glGetIntegerv( GL_MAX_TEXTURE_SIZE, &f );
  LogDebugf( "GL_MAX_TEXTURE_SIZE: %d", f );

  if( CheckGLError() )
    LogDebugf( "FAIL(9F)" );

//nPow2RoundWidth = 2048;
//nPow2RoundWidth  = 1024;
//nPow2RoundWidth  = 512;
nPow2RoundWidth  = 16;
nPow2RoundHeight = 16;
LogDebugf( "nPow2RoundWidth: %d  nPow2RoundHeight: %d", nPow2RoundWidth, nPow2RoundHeight );

  glTexImage2D( GL_TEXTURE_2D, 0, GL_RED, nPow2RoundWidth, nPow2RoundHeight, 0, GL_RED, GL_UNSIGNED_BYTE, 0 );

  if( CheckGLError() )
  {
    LogDebugf( "AT FAIL(10)" );
    exit( 1 );
  }

  LogDebugf( "WORKED AT (10)" );
  exit( 1 );

What would cause this?

UPDATE: If I change GL_RED to GL_RGB it works..

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, nPow2RoundWidth, nPow2RoundHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0 );

Solution

  • Table 3.8 of the spec defines the valid values for internalformat and format of glTexImage2D

    They are, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB and GL_RGBA.

    Depending on exactly what you're trying to achieve, you will probably find GL_LUMINANCE or GL_ALPHA can be suitable substitutes.

    Alternatively, this extension does support a red (and a red-green) texture, I think it's quite common, but isn't everywhere.