Shader convenience module...

I'm always wary of adding non-standard functionality to PyOpenGL, but as I've been trying to write tutorials for how to use shaders I'm finding that it really is necessary to have a module that provides the appropriate "alternate" definitions and provides the basic compileShader/compileProgram functions (with error checking and log extraction).

So, to use a shader in PyOpenGL (in the 3.0.1 upcoming release):

from OpenGL.GL.shaders import *
...
self.shader = compileProgram(
compileShader(
'''void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}''',GL_VERTEX_SHADER),
compileShader('''void main() {
gl_FragColor = gl_Color;
}''',GL_FRAGMENT_SHADER),
)
...
glUseProgram(self.shader)

I'm sure I'll regret it as soon as the comments start, but oh well, the folly of youth.

Comments

  1. Lucian

    Lucian on 07/19/2009 6:44 a.m. #

    That looks very nice and clean to me.

    On a related note, I'm a bit miffed that you have to write various variants of pseudo-C to take advantage of shaders. I'd love something like Cython/Pyrex at least. Perhaps the PyPy project could do it more cleanly, with RPython.

  2. Richard Jones

    Richard Jones on 07/19/2009 7:21 p.m. #

    That looks like a very nice, clean API.

Comments are closed.

Pingbacks

Pingbacks are closed.