Simple OpenGL helper library for testing code and writting examples with minimal fuss

pip install hgl==0.1.3


HGL - Helper GL

This is a simple library used for writing quick tests and examples programs while avoiding some of the boiler plate code you normally require. It has also been designed to be used nicely in emacs org-mode so you can do literate programming, quickly testing and annotate code as you go.

Currently only tested on linux, but will likely work on other platforms dependant on the context you use to render.

Current contexts supported are:

  • pySDL2 context
  • pygame-sdl2 context (its a pain to install no pip)
  • gtkglarea context
  • glut context


You install this using pip.

pip install hgl

Alternatively you can use the commend below to install directly from the downloaded source.

python3 setup.py develop


To work on this library run setup.py in develop mode to create a symlink to your checked out project.

python3 setup.py develop


To run the test suite you should be able to use pytest in the project root



Below are a few examples of using this library to knock up some quick examples.

Using different rendering contexts

Using gtkglarea context

from hgl.context.gtkglarea_context import context
return context(version=(4, 3)).save(filename='./docs/gtkglarea_context_example.png')

Using pygame context

from hgl.context.gtkglarea_context import context
return context(version=(4, 3)).save(filename='./docs/pyame_context_example.png')

Extending an existing context

If you want to modify the defaults you can just inherit the context and replace the methods.

from hgl.context.gtkglarea_context import context

class custom_context(context):
    default_fragment_shader = ["""
        #version 330
        out vec4 fragColor;
        void main()
            fragColor = vec4(1.0, 1.0, 0.0, 1.0);

my_ctx = custom_context(version=(4,3))
return my_ctx.save(filename='./docs/overriden_context.png')


import os
from OpenGL import GL
import numpy as np
from hgl.context.gtkglarea_context import context
from hgl.libs.textures import load_texture

from tests.helper import simple_texture_fs 
from tests.helper import simple_texture_vs 

class custom_context(context):
    vertex_list = np.array([
      0.6,  0.6, 0.0, 0.0, 1.0,
      -0.6,  0.6, 0.0, 1.0, 0.0,
      0.0, -0.6, 0.0, 0.0, 0.0],
    vertex_size = 5
    vertex_stride = 4 * vertex_size

    default_vertex_shader = simple_texture_vs
    default_fragment_shader = simple_texture_fs

    def update(self):
        self.texture_id = load_texture(filename='./tests/textures/testing.png')
        super(custom_context, self).update()

my_ctx = custom_context(version=(4,3))
return my_ctx.save(filename='./docs/texture_context.png')