OpenGL VAO best practices

后端 未结 4 1779
谎友^
谎友^ 2020-11-28 18:50

Im facing an issue which I believe to be VAO-dependant, but Im not sure..

I am not sure about the correct usage of a VAO, what I used to do during GL initialization

4条回答
  •  囚心锁ツ
    2020-11-28 19:34

    Robert's answer above worked for me when I tried it. For what it's worth here is the code, in Go, of using multiple Vertex Attribute Objects:

    // VAO 1

    vao1 := gl.GenVertexArray()
    vao1.Bind()
    
    vbo1 := gl.GenBuffer()
    vbo1.Bind(gl.ARRAY_BUFFER)
    
    verticies1 := []float32{0, 0, 0, 0, 1, 0, 1, 1, 0}
    gl.BufferData(gl.ARRAY_BUFFER, len(verticies1)*4, verticies1, gl.STATIC_DRAW)
    
    pa1 := program.GetAttribLocation("position")
    pa1.AttribPointer(3, gl.FLOAT, false, 0, nil)
    pa1.EnableArray()
    defer pa1.DisableArray()
    
    vao1.Unbind()
    
    // VAO 2
    
    vao2 := gl.GenVertexArray()
    vao2.Bind()
    
    vbo2 := gl.GenBuffer()
    vbo2.Bind(gl.ARRAY_BUFFER)
    
    verticies2 := []float32{-1, -1, 0, -1, 0, 0, 0, 0, 0}
    gl.BufferData(gl.ARRAY_BUFFER, len(verticies2)*4, verticies2, gl.STATIC_DRAW)
    
    pa2 := program.GetAttribLocation("position")
    pa2.AttribPointer(3, gl.FLOAT, false, 0, nil)
    pa2.EnableArray()
    defer pa2.DisableArray()
    
    vao2.Unbind()
    

    Then in your main loop you can use them as such:

    for !window.ShouldClose() {
        gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
    
        vao1.Bind()
        gl.DrawArrays(gl.TRIANGLES, 0, 3)
        vao1.Unbind()
    
        vao2.Bind()
        gl.DrawArrays(gl.TRIANGLES, 0, 3)
        vao2.Unbind()
    
        window.SwapBuffers()
        glfw.PollEvents()
    
        if window.GetKey(glfw.KeyEscape) == glfw.Press {
            window.SetShouldClose(true)
        }
    }
    

    If you want to see the full source, it is available as a Gist and derived from the examples in go-gl:

    https://gist.github.com/mdmarek/0f73890ae2547cdba3a7

    Thanks everyone for the original answers, I had the same question as ECrownofFire.

提交回复
热议问题