Towards A Better BGL API

When you read the OpenGL specs, all the functions and constants are defined without the “gl” and “GL_” prefixes respectively. When an actual API is implemented in a language like C, it is usual to add these.

This makes sense in a language without namespaces, but in Python, which has them, it doesn’t really make sense. Consider a simple sequence like

bgl.glBegin(bgl.GL_LINES)
bgl.glVertex2f(*v1)
bgl.glVertex2f(*v2)
bgl.glEnd()

Don’t you find all that “bgl.gl” and “bgl.GL_” redundant? Wouldn’t it be simpler written as

gl.Begin(GL.LINES)
gl.Vertex2f(*v1)
gl.Vertex2f(*v2)
gl.End()

? I wrote a utility function called gen_gl() which lets you do exactly that: with a simple call like

gl, GL, glu = gen_gl()

it extracts and renames the contents of the bgl module into separate gl, GL and glu modules, letting you make all your OpenGL calls in a much cleaner fashion.

Question: What OpenGL specs, please?

Answer: In python, you are right, namespace arn’t necessary BUT:

  • all functions can to be overload so gl specs are not the same as the bgl functions. BGL is special for Blender and arn’t the same as the official OpenGL specs.
  • the functions are too local or global so the glvertex2f() arn’t the same as official .vertex2f() function of specs because Blender can modify their contents.

You tip with renamed functions is usefull if you know what you make but not for overload python capabilies for all users.
I don’t say you arn’t right but use it with prudence…

Byebye, good luck.
Spirou4D

Why, the official ones, of course. What did you think I meant?

Nope

Take the most popular OpenGL wrappers for python PyOpengl it also uses gl prefix

Why ?

its simple, laziness

it may sound crazy but its actually more lazy to write glBegin than Begin and the reason being is that we coders love to copy paste code
and as you may have guessed it by now the vast majority of OpenGL code online is written in C/C++ which use the gl prefix.

So it makes more sense to keep the prefix and make copy pasting much easier and also porting C/C++ OpenGL code to Python than actually make it easier to write code from scratch.

It also works both ways , if your python code does not work there is no way to know for sure if they are the wrappers or the actual Opengl code so again you will have to port code from python to C because C/C++ has the best IDEs for debugging OpenGL code giving you all the precise errors you need plus, inspecting low level memory inside and outside the GPU etc.

By the way that bgl. is redundant if you do

from bgl imprort * 

then your code will only need to be like this


glBegin(bgl.GL_LINES)
glVertex2f(*v1)
glVertex2f(*v2) 
glEnd()

Why, the official ones, of course. What did you think I meant?

No I wanted to say in general “What official release”?
There are a lot of corrections in official openGL…All change each month…and The OpenGl of Blender is not as the official rules.

I explain my question bellow.