1

I am using following method to create OpenGL ES texture on android.

 private int createTexture(int width, int height, int i) {
    int[] mTextureHandles = new int[1];
    GLES20.glGenTextures(1, mTextureHandles, 0);
    int textureID = mTextureHandles[0];
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    return textureID;
}

Then uploading a bitmap to this texture and simply rendering it using GLSurfaceView's Renderer.

Most of the times it is working as expected,

normal working image

But randomly the texture is displayed like this. (here GL_TEXTURE_WRAP_S mode is GLES20.GL_CLAMP_TO_EDGE

clamp mode used

After changing wrap mode.

If GL_TEXTURE_WRAP_S = GLES20.GL_REPEAT then (again randomly) texture is displayed like this(notice the color change).

repeat mode used

I have already tried using power of 2 texture.

Code for vertex buffer

 private FloatBuffer createVertexBuffer(RectF glCoords) {

  //   RectF glCoords contains gl vertices.
  //   current Rect read from Logcat.
  //   left = -0.5833333, top = 0.5, right = 0.5833334, bottom = -0.5

    float[] vertices = new float[]{glCoords.left, glCoords.top, 0, 1, // V1 - top left
            glCoords.left, glCoords.bottom, 0, 1, // V2 - bottom left
            glCoords.right, glCoords.bottom, 0, 1, // V3 - bottom right
            glCoords.right, glCoords.top, 0, 1 // V4 - top right
    };
    FloatBuffer vBuffer = ByteBuffer.allocateDirect(vertices.length * bytesPerFloat)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    vBuffer.put(vertices);
    vBuffer.position(0);
    return vBuffer;
}

IndicesBuffer is not used as I am using Triangle-Fan to render triangles.

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);

It would be really helpful if someone can point out as to what could be causing this

genpfault
  • 51,148
  • 11
  • 85
  • 139
Rishabh Dhawan
  • 519
  • 1
  • 3
  • 11
  • The brown-ish part is unlikely to be caused by wrapping modes. There has to be some geometry drawn. If it's random, look for uninitalized variables in the code that fills the vertex/index buffers. – Vlad Nov 18 '19 at 19:57
  • @Vlad thanks for ur comment, I have added vertex buffer code also. – Rishabh Dhawan Nov 18 '19 at 20:13

1 Answers1

2

Your geometry seems to be a 4-vertex rectangle (quad), but the code wants to draw a fan with 6 vertices - GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);

Those extra 2 vertices aren't initialized, not specified in the vertex array. OpenGL will read beyond the defined part of the array. The results would be random, and I'd even expect crashes.

Replace 6 with 4 in glDrawArrays() call, this should fix the problem.

Vlad
  • 5,450
  • 1
  • 12
  • 19