How to use VAOs in OpenGL ES 2.0

I've read that using VAOs is recommended but I don't know how. I am using the tegra android development pack (which uses OpenGL ES 2.0) and I can't find the methodes glGenVertexArrays and glBindVertexArray. Are VAOs supported in ES 2.0 (without extensions)?

Also I don't understand what the benefit of a VAO is. Currently I compile the shaders and use glBindAttribLocation. After that I define the vertex positions and colors for a triangle and use glVertexAttribPointer. Then I just draw the triangle again and again.

This is the program (without error checks):

static void createProgram() {
    // vertex shader
    const char* vertexShaderCode = getVertexShaderCode();
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderCode, NULL);
    glCompileShader(vertexShader);

    // fragment shader
    const char* fragmentShaderCode = getFragmentShaderCode();
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderCode, NULL);
    glCompileShader(fragmentShader);

    // create program
    program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);

    // Dem Programm sagen, unter welchem Index die Daten der Shader Attribute liegen
    glBindAttribLocation(program, 1, "in_pos");
    glBindAttribLocation(program, 2, "in_color");

    glLinkProgram(program);
}

And this is the triangle:

static void loadGeometry() {
    GLuint buffers[2];
    glGenBuffers(2, buffers);
    GLuint vboPosition = buffers[0];
    GLuint vboColor = buffers[1];

    const int VERTEX_POSITION_BUFFER_SIZE = 12;
    const int VERTEX_POSITION_BUFFER_BYTESIZE = sizeof(GLfloat)* VERTEX_POSITION_BUFFER_SIZE;

    GLfloat vertexPositionBuffer[VERTEX_POSITION_BUFFER_SIZE] = {
        0.0f, 0.5f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.0f, 1.0f,
        0.5f, -0.5f, 0.0f, 1.0f };

    glBindBuffer(GL_ARRAY_BUFFER, vboPosition);
    glBufferData(GL_ARRAY_BUFFER, VERTEX_POSITION_BUFFER_BYTESIZE, vertexPositionBuffer, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);

    // Repeat for color...
}

And in the draw call I just do

glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 3);
链接地址: http://www.djcxy.com/p/52436.html

上一篇: FocusEvent没有得到JFormattedTextField的最后一个值,我怎么能得到它?

下一篇: 如何在OpenGL ES 2.0中使用VAO