为什么我得到“glDrawElements:没有绑定到命令的数据 - 忽略”?

时间:2016-01-09 14:23:13

标签: android opengl-es-2.0

我一直在尝试使用OpenGL2.0 ES在Android上绘制一个带纹理的平面。但是,我一直收到一个logcat错误,说“glDrawElements:没有数据绑定到命令 - 忽略”(标签:emuglGLESv2_enc),飞机不再出现了。

我的飞机:

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

}

顶点着色器:

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

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

片段着色器:

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

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

模拟器在android 5.1.1上运行。

1 个答案:

答案 0 :(得分:0)

尝试将所有与Shader相关的调用移动到GL线程中,即onSurfaceCreated()onSurfaceChanged()onDrawFrame()。 更多信息:glCreateShader and glCreateProgram fail on android