Xtapolapocetl
Xtapolapocetl

Reputation: 733

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.

Upvotes: 4

Views: 2253

Answers (5)

diego.martinez
diego.martinez

Reputation: 1051

maybe you forgot to

glEnable(GL_TEXTURE_2D);

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

Upvotes: 1

Nick
Nick

Reputation: 1758

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.

Upvotes: 2

Yuriy Vikulov
Yuriy Vikulov

Reputation: 2499

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();

Upvotes: 1

ajeetdl
ajeetdl

Reputation: 1284

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.

Upvotes: 2

Dr. Snoopy
Dr. Snoopy

Reputation: 56397

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

Upvotes: 2

Related Questions