BGL and OpenGL

Hi all,
from what I could find Blender 2.8 uses OpenGL version 3.3,and supports GSL 3.3 .
but BGL does not have any of the OpenGL 3.3 functions I know of.
For one I don’t see the glGenVertexArrays for creating VAOs.
Do we have access to these functions and the BGL documentation needs an update,or we are not allowed access to that?

Check this: https://developer.blender.org/rB3c7a538c9b73ff7ab87ba508c5d44433d6c2877b

thanks…it is as I assumed.The 3.3 functions are not listed in the documentation. Phew.
I’ve been learning OpenGL for a month and a half,so I can draw stuff in blender.It would be a bummer if I couldn’t use even the basic 3.3 functions.

Not to create separate post that is related to the topic…I’ll post my question here

I went in and tried to play with drawing with OpenGL in blender.
using the logic I have from working with the python wrapper for OpenGL I came up with this code:

import bgl
import bpy

data = [-0.1,-0.1,0,
     0.1,-0.1,0,
       0, 0.1,0]
data = bgl.Buffer(bgl.GL_FLOAT,9,data)
vbo = bgl.Buffer(bgl.GL_INT,1)
vao = bgl.Buffer(bgl.GL_INT,1)
draw_handle = None
vertex_shader_source = """
#version 330
layout(location = 0)in vec3 pos;
void main()
{
    gl_Position = vec4(pos,1.0);
}
"""
fragment_shader_source = """
#version 330
out vec4 FragColor;
void main()
{
     FragColor = vec4(0.91, 0.255, 0.094,1.0);   
}

"""
shader_program = bgl.glCreateProgram()

#VAO VBO Data
bgl.glGenVertexArrays(1,vao)
bgl.glBindVertexArray(vao[0])

bgl.glGenBuffers(1,vbo)
bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER,vbo[0])
bgl.glBufferData(bgl.GL_ARRAY_BUFFER,4*len(data),data,bgl.GL_STATIC_DRAW)
bgl.glVertexAttribPointer(0,3,bgl.GL_FLOAT,bgl.GL_FALSE,0,None)
bgl.glEnableVertexAttribArray(0)

bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER,0)
bgl.glBindVertexArray(0)
print(f'VAO:{vao[0]} -  VBO:{vbo[0]}')

# Build Shader
vertex_shader = bgl.GL_INT
fragment_shader = bgl.GL_INT
bgl.glShaderSource(vertex_shader,vertex_shader_source)
bgl.glCompileShader(vertex_shader)
bgl.glShaderSource(fragment_shader,fragment_shader_source)
bgl.glCompileShader(fragment_shader)
bgl.glAttachShader(shader_program,vertex_shader)
bgl.glAttachShader(shader_program,fragment_shader)
bgl.glLinkProgram(shader_program)
bgl.glDeleteShader(vertex_shader)
bgl.glDeleteShader(fragment_shader)

# Draw
def draw():
bgl.glUseProgram(shader_program)
bgl.glBindVertexArray(vao[0])
bgl.glDrawArrays(bgl.GL_TRIANGLES,0,3)

# Clear
def in_3_sec():

bpy.types.SpaceView3D.draw_handler_remove(draw_handle,'WINDOW') 
bgl.glDeleteVertexArrays(1,vao)
bgl.glDeleteBuffers(1,vbo)   
bgl.glDeleteProgram(shader_program)

draw_handle = bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
bpy.app.timers.register(in_3_sec, first_interval=3)

But all I get is the screen in the 3d viewport getting all white, and of course going back to normal after the 3sec. Where did I go wrong with this?

Your problem might be that you use glVertexAttribPointer which seems to be broken.
In case you are trying to compile yourself, try this patch, it worked for me: https://developer.blender.org/D2404

Thanks,GottfriedHofmann.
I have no idea how to compile blender.I guess I’ll wait till this is fixed.

Oh, actually I just checked a latest build without the patch and it is fixed there as well (so the error must be somewhere else in the script)

Might be me not using the latest 2.8 …will update and see if it works

Edit: now it does not make the screen white but I don’t see a render as well.Looking at the AttribPointer I did noticed that I forgot to make the stride to 12…
but the offset or the pointer I have no idea how to make that work.In pyOpenGL I use to give a ctype.c_void_p, but here blender asks for “Buffer” ?

Here is example code (assumes from bgl import *):

# color attribute
col_attr_buf = Buffer(GL_INT, 1)
col_attr_buf[0] = 12
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 32, col_attr_buf)

Edit: The code above will not work, see How to modify an instance of gpu.types.GPUVertBuf?

1 Like