1

I have been trying to draw a plane with a texture on it, using OpenGL2.0 ES on android. However, I keep getting a logcat error saying "glDrawElements: no data bound to the command - ignoring" (tag: emuglGLESv2_enc) and the plane does not show up anymore.

My plane:

public class Wall {
private FloatBuffer data;
private ShortBuffer indices;
private final int program;
private final int positionHandle;
private final int textureHandle;
private final int samplerHandle;
private final int mvpHandle;
private final int[] buffers = new int[2];
private final int[] textures = new int[1]; 
private final float[] mvpMatrix = new float[16];

public Wall(String v, String f, Bitmap b) { //vertex shader, fragment shader and texture
    data = ByteBuffer.allocateDirect(80).order(ByteOrder.nativeOrder()).asFloatBuffer();
    data.put(new float[]{-20.0f, .0f, -20.0f, .0f, .0f, //x, y, z, texCoord x, texCoord y
                        -20.0f, .0f, 20.0f, .0f, 1.0f,
                        20.0f, .0f, -20.0f, 1.0f, .0f,
                        20.0f, .0f, 20.0f, 1.0f, 1.0f});
    data.position(0);

    indices = ByteBuffer.allocateDirect(8).order(ByteOrder.nativeOrder()).asShortBuffer();
    indices.put(new short[]{0, 1, 2, 3});
    indices.position(0);

    program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, OwnRenderer.getShader(GLES20.GL_VERTEX_SHADER, v)); //compile the shader
    GLES20.glAttachShader(program, OwnRenderer.getShader(GLES20.GL_FRAGMENT_SHADER, f));
    GLES20.glLinkProgram(program);

    positionHandle = GLES20.glGetAttribLocation(program, "inPosition");
    textureHandle = GLES20.glGetAttribLocation(program, "inTexture");
    samplerHandle = GLES20.glGetUniformLocation(program, "tex");
    mvpHandle = GLES20.glGetUniformLocation(program, "mvpMatrix");

    GLES20.glGenBuffers(2, buffers, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, 80, data, GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 8, indices, GLES20.GL_STATIC_DRAW);

    GLES20.glGenTextures(1, textures, 0);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
    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);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, b, 0);
    b.recycle();

    setMVP();
}

public void draw() {
    GLES20.glUseProgram(program);

    GLES20.glUniform1i(samplerHandle, 0);
    GLES20.glUniformMatrix4fv(mvpHandle, 1, false, mvpMatrix, 0);
    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 20, 0);
    GLES20.glEnableVertexAttribArray(textureHandle);
    GLES20.glVertexAttribPointer(textureHandle, 2, GLES20.GL_FLOAT, false, 20, 12);


    GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, 4, GLES20.GL_UNSIGNED_SHORT, 0);
    GLES20.glDisableVertexAttribArray(positionHandle);
    GLES20.glDisableVertexAttribArray(textureHandle);
}

private void setMVP() {
    //First scaling, then rotation, then translation.
    Matrix.setIdentityM(mvpMatrix, 0);
    Matrix.multiplyMM(mvpMatrix, 0, MainActivity.viewMatrix, 0, mvpMatrix, 0);
    Matrix.multiplyMM(mvpMatrix, 0, MainActivity.projectionMatrix, 0, mvpMatrix, 0);
}

}

Vertex shader:

uniform mat4 mvpMatrix;
attribute vec4 inPosition;
attribute vec2 inTexture;
varying vec2 outTexture;

void main() {
    gl_Position = mvpMatrix * inPosition;
    outTexture = inTexture;
}

Fragment shader:

precision mediump float;
uniform Sampler2D tex;
varying vec2 outTexture;

void main() {
    gl_FragColor = texture(tex, outTexture);
}

Emulator runs on android 5.1.1.

Robo
  • 11
  • 2

1 Answers1

0

Try moving all the Shader related calls into GL Thread, that is onSurfaceCreated(), onSurfaceChanged() and onDrawFrame(). More at : glCreateShader and glCreateProgram fail on android

Community
  • 1
  • 1
Rahul Shukla
  • 1,292
  • 9
  • 25