OpenGL - need a simple concept clarification

后端 未结 1 1558
夕颜
夕颜 2020-12-21 09:05

I\'m new to OpenGL. I understand how to create the vertex and fragments shaders and how to create vertex arrays and put them on the buffer, but how do i link the two? Meanin

相关标签:
1条回答
  • 2020-12-21 09:22

    There's no really such thing as "currently active buffer". (Well, there is but it's relevant only at the time of VAO specification with the non direct-state-access API.)

    The vertex array object (VAO) holds pointers (set through glVertexAttribPointer) into the data within the memory buffers. The bindings to those buffers are a part of the state of the VAO. When you bind a VAO then subsequent glDraw* commands will transfer the vertices from the buffers bound to the current VAO to the pipeline.

    For simplicity of comprehension, here is what a VAO roughly is, with the relevant OpenGL 4.5 direct-state-access function names used to set that state:

    struct VertexArrayObject
    {
        // VertexArrayElementBuffer
        uint element_buffer;
    
        struct Binding {
            // VertexArrayVertexBuffers
            uint buffer;
            intptr offset;
            sizei stride;
    
            // VertexArrayBindingDivisor
            uint divisor;
        } bindings[];
    
        struct Attrib {
            // VertexArrayAttribBinding
            uint binding; // This is an index into bindings[]
    
            // EnableVertexArrayAttrib
            bool enabled;
    
            // VertexArrayAttrib*Format
            int size;
            enum type;
            boolean normalized;
            boolean integer;
            boolean long;
            uint relativeoffset;
        } attribs[];
    };
    

    When you call glVertexAttribPointer it essentially does the following on the currently bound VAO:

    vao.attribs[index].binding = index;
    vao.attribs[index].size = size;
    vao.attribs[index].type = type;
    vao.attribs[index].normalized = normalized;
    vao.attribs[index].relativeoffset = 0;
    vao.bindings[index].buffer = current ARRAY_BUFFER;
    vao.bindings[index].offset = pointer;
    vao.bindings[index].stride = stride; // if stride == 0 computes based on size and type
    
    0 讨论(0)
提交回复
热议问题