Skip to main content

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

Project description

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

* Installation
You install this using pip.
#+BEGIN_SRC bash
pip install hgl
#+END_SRC

Alternatively you can use the commend below to install directly from the downloaded source.
#+BEGIN_SRC bash
python3 setup.py develop
#+END_SRC

* Development

To work on this library run setup.py in develop mode to create a symlink to your checked out project.
#+BEGIN_SRC bash
python3 setup.py develop
#+END_SRC

* Testing
To run the test suite you should be able to use pytest in the project root
#+BEGIN_SRC bash
pytest
#+END_SRC

#+RESULTS:
| ============================= | test | session | starts | ============================== | | | |
| platform | linux2 | -- | Python | 2.7.13, | pytest-3.0.6, | py-1.4.32, | pluggy-0.4.0 |
| rootdir: | /home/oly/repos/hgl, | inifile: | | | | | |
| plugins: | mock-1.3.0, | cov-2.4.0, | Inomaly-0.1.0 | | | | |
| collected | 56 | items | | | | | |
| | | | | | | | |
| tests/test_calculate/test_calculations_angles.py | ... | | | | | | |
| tests/test_calculate/test_calculations_boxes.py | ... | | | | | | |
| tests/test_calculate/test_calculations_circles.py | .... | | | | | | |
| tests/test_calculate/test_calculations_curves.py | 0 | | | | | | |
| tests/test_calculate/test_calculations_grid.py | ... | | | | | | |
| tests/test_calculate/test_calculations_intersection.py | .. | | | | | | |
| tests/test_calculate/test_calculations_misc.py | .. | | | | | | |
| tests/test_calculate/test_calculations_origin.py | .. | | | | | | |
| tests/test_extend_context/test_change_shaders.py | ............ | | | | | | |
| tests/test_extend_context/test_change_vertex_array.py | ............ | | | | | | |
| tests/test_gl_contexts/test_context.py | ............ | | | | | | |
| | | | | | | | |
| ========================== | 56 | passed | in | 29.46 | seconds | ========================== | |

* Examples
Below are a few examples of using this library to knock up some quick examples.
** Using different rendering contexts
*** Using gtkglarea context
#+BEGIN_SRC python :results file
from hgl.context.gtkglarea_context import context
return context(version=(4, 3)).save(filename='./docs/gtkglarea_context_example.png')
#+END_SRC

#+RESULTS:
[[file:./docs/gtkglarea_context_example.png]]
*** Using glut context
#+BEGIN_SRC python :results file
from hgl.context.glut_context import context
return context(version=(4, 3)).save(filename='./docs/glut_context_example.png')
#+END_SRC

#+RESULTS:
[[file:./docs/glut_context_example.png]]

*** Using pygame context
#+BEGIN_SRC python :results file
from hgl.context.gtkglarea_context import context
return context(version=(4, 3)).save(filename='./docs/pyame_context_example.png')
#+END_SRC

#+RESULTS:
[[file:./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.
#+BEGIN_SRC python :results file
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')
#+END_SRC

#+RESULTS:
[[file:./docs/overriden_context.png]]

** Texturing
#+BEGIN_SRC python :results file
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],
dtype=np.float32)
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')
#+END_SRC

#+RESULTS:
[[file:./docs/texture_context.png]]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hgl-0.1.2.tar.gz (59.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page