Vertex Buffer Object with bgl?

Hi! I tried to make an object using vbo with bgl API but it crashes.

Here is my test file: (the original polygon can be replaced with something with less polygons.

I commented the code for vbo. I don’t know if I made something wrong or if vbo is not supported. I don’t know how to deal with bgl.Buffer and bgl.glGenBuffers as I can already make a buffer and fill it without calling bgl.glBufferData …

If an openGL master could have a look at it, I thank him very much!

I don’t have fixed your script. Instead I have modified one of my PyOpenGL examples to work with BGL.
But there is a problem that it is not possible to generate a void bgl buffer which is necessary for glVertexAttribPointer (VAO) and glDrawElements (VBO).
So we still need PyOpenGL.

#HG1 23.06.2016
#Modern OpenGL draw using glDrawElements(GL_TRIANGLES) with BGL VBO binding, using GLSL shader
#Bind vertex postions by location

from bge import logic
from bgl import *

    from OpenGL import GL
    print (" Error: PyOpenGL not installed properly ")
own = logic.getCurrentController().owner
sizeOfFloat = 4
sizeOfInt = 4

#create the shaders
vertex_shader = """
#version 330
layout(location = 0) in vec4 position;
void main()
    gl_Position = position;

fragment_shader = """
#version 330
out vec4 outputColor;
void main()
    outputColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);

#Create the VBO
vertices = [ 0.0,  0.6,  0.0, 
            -0.6, -0.6,  0.0, 
             0.6, -0.6,  0.0]

#Create the index buffer object
indices = [0, 1, 2]

vertBuf = Buffer(GL_FLOAT, len(vertices), vertices)
indexBuf = Buffer(GL_INT, len(indices), indices)

# Shader
own["program"] = glCreateProgram()

shaderVert = glCreateShader(GL_VERTEX_SHADER)
shaderFrag = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(shaderVert, vertex_shader)
glShaderSource(shaderFrag, fragment_shader)


glAttachShader(own["program"], shaderVert)
glAttachShader(own["program"], shaderFrag)



# Create a new VAO (Vertex Array Object) and bind it
vertex_array_object = Buffer(GL_INT, 1)
glGenVertexArrays(1, vertex_array_object)

# Generate VBO buffers to hold our vertices and indices, it is better to inititialize it first.
vertexPositions = Buffer(GL_INT, 1)
glGenBuffers(1, vertexPositions)
glBindBuffer(GL_ARRAY_BUFFER, vertexPositions[0])
glBufferData(GL_ARRAY_BUFFER, len(vertices) * sizeOfFloat, vertBuf, GL_STATIC_DRAW)

indexPositions = Buffer(GL_INT, 1)
glGenBuffers(1, indexPositions)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexPositions[0])
glBufferData(GL_ELEMENT_ARRAY_BUFFER, len(indices) * sizeOfInt, indexBuf, GL_STATIC_DRAW)

# Get the position of the 'position' layout(location = 0) in parameter of our shader and bind it.

# Describe the position data layout in the buffer
#NULL = Buffer(GL_INT, 1, [0])
#glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, NULL)
GL.glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, None)

# Unbind the VAO first (Important, otherwise nothing will be drawen and Blender UI will be over drawen after stop)

# Unbind other stuff
glBindBuffer(GL_ARRAY_BUFFER, 0)

#The draw loop
def display():

        #glDrawArrays(GL_TRIANGLES, 0, len(vertices)//3)

        #NULL = Buffer(GL_INT, 1, [0])
        #glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, NULL)
        GL.glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

        # Unbind the VAO (Important, otherwise Blender will crash)
        # Unbind Program (Important, otherwise Blender UI will be over drawen after stop)

logic.getCurrentScene().post_draw = [display]

def end():
    glDeleteVertexArrays(1, vertex_array_object)
    glDeleteBuffers(1, vertexPositions)
    glDeleteBuffers(1, indexPositions)

Thanks very much HG1! I’ll have a look and try to undertand.