GLES错误:启用了顶点属性数组,没有绑定数据

时间:2016-06-27 19:19:04

标签: android c++ opengl-es android-ndk opengl-es-2.0

OpenGl新手,目前正在使用Android NDK。我目前收到此错误,无法弄清楚。我觉得它是基本的东西,但我可能是错的。 appRender由java渲染端每帧运行。

错误:

E/emuglGLESv2_enc: glDrawArrays: a vertex attribute array is enabled with no data bound

app.cpp:

void appRender(long tick, int width, int height){
    const float vertices[] =
    {
         0.0f,  0.5f, 0.0f,
        -0.5f, -0.5f, 0.0f,
         0.5f, -0.5f, 0.0f
    };

    glClear(GL_COLOR_BUFFER_BIT);

    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Setup colors for each vertex
    GLfloat colors[3*4];
    for (int v = 0; v < 3; v++){
        colors[4*v+0] = 0;
        colors[4*v+1] = 1;
        colors[4*v+2] = 0;
        colors[4*v+3] = 1;
    }

    // Setup color buffer
    GLuint colorBuffer;
    glGenBuffers(1, &colorBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(
        1,
        3,
        GL_FLOAT,
        GL_FALSE,
        0,
        (void*)0
        );

    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    glVertexAttribPointer(
        1,
        4,
        GL_FLOAT,
        GL_FALSE,
        0,
        (void*)0
        );


    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    glBindTexture(GL_TEXTURE_2D, 0);
    //textRenderer.RenderTexts(0.5f);
    //boxRenderer.RenderBoxes(0.5f);
}

1 个答案:

答案 0 :(得分:1)

所以我找到了,是的,我很糟糕。

glVertexAttribPointer(1,3,...) -> glVertexAttribPointer(0,3,...)