4
votes

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.

5
Just like you, I'm unable to see anything obviously wrong with the code presented. If you're willing, could you post the whole project somewhere? There's got to be some really interesting going on.Tommy
I don't want to post the whole project, but the OpenGL code is in a self-contained UIView subclass:Xtapolapocetl
There's something wrong elsewhere in your code – the stuff you've provided works perfectly when wired hastily into a test project. The thing at pastie.org/1345953 is literally all I wrote, then I grabbed the Kindle graphic currently on Amazon's front page, resized it to 1024x1024 and saved it as testImage.png, adding it to the project as a resource. Running the program resulted in img802.imageshack.us/img802/1174/screenshot20101203at232.pngTommy

5 Answers

1
votes

After glTexImage2D, set the MIN/MAG filters with glTexParameter, the defaults use mipmaps so the texture is incomplete with that code.

1
votes

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.

1
votes

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.

0
votes

make sure that you are calling (glTexImage2D) with right formats(constants)

make sure that you are freeing resources of image after glTexImage2D

that's how i'm do it 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
votes

maybe you forgot to

glEnable(GL_TEXTURE_2D);

is such case, texture2D in the shader would return black, as the OP seems to suffer.