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 *
try:
from OpenGL import GL
except:
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)
glCompileShader(shaderVert)
glCompileShader(shaderFrag)
glAttachShader(own["program"], shaderVert)
glAttachShader(own["program"], shaderFrag)
glLinkProgram(own["program"])
glDeleteShader(shaderVert)
glDeleteShader(shaderFrag)
# Create a new VAO (Vertex Array Object) and bind it
vertex_array_object = Buffer(GL_INT, 1)
glGenVertexArrays(1, vertex_array_object)
glBindVertexArray(vertex_array_object[0])
# 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.
glEnableVertexAttribArray(0);
# 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)
glBindVertexArray(0)
# Unbind other stuff
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
#The draw loop
def display():
glUseProgram(own["program"])
try:
glBindVertexArray(vertex_array_object[0])
#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)
finally:
# Unbind the VAO (Important, otherwise Blender will crash)
glBindVertexArray(0)
# Unbind Program (Important, otherwise Blender UI will be over drawen after stop)
glUseProgram(0)
logic.getCurrentScene().post_draw = [display]
def end():
glUseProgram(0)
glDeleteVertexArrays(1, vertex_array_object)
glDeleteBuffers(1, vertexPositions)
glDeleteBuffers(1, indexPositions)
logic.endGame()