4

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Xtapolapocetl
  • 613
  • 5
  • 21
  • 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 Dec 03 '10 at 17:56
  • I don't want to post the whole project, but the OpenGL code is in a self-contained UIView subclass: – Xtapolapocetl Dec 03 '10 at 18:04
  • http://dl.dropbox.com/u/163839/FilteredImageView/FilteredImageView.h – Xtapolapocetl Dec 03 '10 at 18:04
  • http://dl.dropbox.com/u/163839/FilteredImageView/FilteredImageView.mm – Xtapolapocetl Dec 03 '10 at 18:05
  • Sorry for the fact that this is still basically "prototype" code. It's somewhat of a mess. – Xtapolapocetl Dec 03 '10 at 18:05
  • 2
    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 http://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 http://img802.imageshack.us/img802/1174/screenshot20101203at232.png – Tommy Dec 03 '10 at 23:33
  • Huh. Very strange. Not sure where else the problem could be, but I'll check it out. Thanks for your help. – Xtapolapocetl Dec 04 '10 at 03:26
  • You don't have any idea what could cause this, do you? Like I said, it all works but the texture, and this is just wrapped in a couple of nested UIScrollViews. No more OpenGL anywhere in the app. – Xtapolapocetl Dec 04 '10 at 03:36
  • Can't think of a thing. I tried nesting your view in a couple of scrollviews (see the project: http://www.filedropper.com/filteredtest), everything still appears to work fine. – Tommy Dec 04 '10 at 23:27

5 Answers5

2

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

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
2

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.

Community
  • 1
  • 1
ajeetdl
  • 1,254
  • 1
  • 13
  • 17
2

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.

Nick
  • 1,692
  • 3
  • 21
  • 35
1

maybe you forgot to

glEnable(GL_TEXTURE_2D);

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

diego.martinez
  • 1,051
  • 2
  • 11
  • 25
1

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();
Yuriy Vikulov
  • 2,469
  • 5
  • 25
  • 32