OpenGL ES 2.0 texturing

I'm trying to render a simple textured quad in OpenGL ES 2.0 on an iPhone. The geometry is fine and I get the expected quad if I use a solid color in my shader:

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

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

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

The image data is loaded from a UIImage, scaled to fit within 1024x1024, and loaded into a texture like so:

    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 are all correct, as examined in the debugger.

When I change my fragment shader to use the texture:

    gl_FragColor = texture2D (tex, texCoord);

... and bind the texture and render like so:

    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. A black quad. glGetError() doesn't return an error and glIsTexture(_texture) returns true.

What am I doing wrong here? I've been over and over every example I could find online, but everybody is doing it exactly as I am, and the debugger shows my parameters to the various GL functions are what I expect them to be.


在glTexImage2D之后,使用glTexParameter设置MIN / MAG滤镜,默认情况下使用mipmaps,因此该代码的纹理不完整。


I was experiencing the same issue (black quad) and could not find an answer until a response by jfcalvo from this question led me to the cause. Basically make sure you are not loading the texture in a different thread.


make sure you set the texture wrap parameters to GL_CLAMP_TO_EDGE in both S and T directions. Without this, the texture is incomplete and will appear black.

链接地址: http://www.djcxy.com/p/5814.html

上一篇: OpenGL纹理混合问题

下一篇: OpenGL ES 2.0纹理