Texturing OpenGL ES 2.0

I am trying to make a simple textured square in OpenGL ES 2.0 on an iPhone. The geometry is beautiful and I get the expected square if I use solid color in my shader:

  gl_FragColor = vec4 (1.0, 0.0, 0.0, 1.0);

And I get the expected gradients if I directly render the texture coordinates:

  gl_FragColor = vec4 (texCoord.x, texCoord.y, 0.0, 1.0);

Image data is loaded from UIImage, scaled to 1024x1024 and loaded into the texture as follows:

  glGenTextures (1, & _texture);
     glBindTexture (GL_TEXTURE_2D, _texture);

     glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, 
         GL_UNSIGNED_BYTE, data);

width , height , and the contents of data all correct, as described in the debugger.

When I change my fragment shader to use a texture:

  gl_FragColor = texture2D (tex, texCoord);

... and bind the texture and do this:

  glActiveTexture (GL_TEXTURE0);
     glBindTexture (GL_TEXTURE_2D, _texture);

     // this is unnecessary, as it defaults to 0, but for completeness ...
     GLuint texLoc = glGetUniformLocation (_program, "tex");          
     glUniform1i (texLoc, 0);

     glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);

... I get nothing. Black square. glGetError() does not return an error, and glIsTexture(_texture) returns true.

What am I doing wrong here? I was aware of all the examples that I could find on the Internet, but everyone does it exactly the same as I do, and the debugger shows my parameters for various GL functions - this is what I expect from them.

+4
source share
5 answers

After glTexImage2D, set the MIN / MAG filters with glTexParameter, mipmaps are used by default so that the texture is incomplete with this code.

+1
source

I ran into the same problem (black box) and could not find the answer until jfcalvo's answer from this question brought me to business. Basically make sure you are not loading the texture into another stream.

+1
source

make sure you set the texture wrapping options to GL_CLAMP_TO_EDGE in both directions S and T. Without this, the texture will be incomplete and turn black.

+1
source

make sure you call (glTexImage2D) with the correct formats (constants)

make sure you free image resources after glTexImage2D

what am i doing this on android:

  int[] textures = new int[1]; GLES20.glGenTextures(1, textures, 0); mTextureID = textures[0]; GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID); GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT); InputStream is = mContext.getResources() .openRawResource(R.drawable.ywemmo2); Bitmap bitmap; try { bitmap = BitmapFactory.decodeStream(is); } finally { try { is.close(); } catch(IOException e) { // Ignore. } GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); bitmap.recycle(); 
0
source

You may have forgotten

glEnable (GL_TEXTURE_2D);

- such a case, texture2D in the shader will be black, since the OP seems to suffer.

0
source

All Articles