When should glVertexAttribPointer be called?

后端 未结 4 1036
萌比男神i
萌比男神i 2020-12-07 11:33

It\'s not obvious from the documentation when glVertexAttribPointer should be called. It looks like it\'s part of VBO initialisation, but I notice example code

4条回答
  •  情书的邮戳
    2020-12-07 11:57

    glVertexAttribPointer has to be called (in most cases) when the appropriate (i.e. the one you want to use) VBO is bound. Then last parameter to it is offset in said buffer.

    The last parameter is defined particularly nice in the reference manual:

    Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target. The initial value is 0.

    Appropriate Vertex Pointer Bindings with VBO sources are stored inside VAO, and you should use that if possible.

    Short example (excuse my pseudocode):

    // Setup
    CreateVAO(); BindVAO();
    CreateVBO(); BindVBO();
    VertexAttribPointer(/*id*/ 0, 3, GL_FLOAT, /*starting at offset*/ 0);
    
    // We have specified Vertex attribute bound to location 0,
    // with size of 3 floats, starting at offset 0 of VBO we've just created.        
    
    //Draw
    BindVAO();
    Draw();
    

提交回复
热议问题