glShaderSource and ctypes

405 views
Skip to first unread message

Alex

unread,
Feb 24, 2008, 5:21:54 PM2/24/08
to pyglet-users
Hello!

After learning Python for a while, I started experimenting with pyglet
and converting some projects from jogl.

And now I get some errors with the code for shaders. It seems it has
something to do with ctypes, but I haven't looked into them yet. And
they look pretty complicated.

Code:

vertex = glCreateShader(GL_VERTEX_SHADER)
fragment = glCreateShader(GL_FRAGMENT_SHADER)

vsource = 'void main()\n{\ngl_FrontColor = vec4(1.0, 1.0, 1.0,
1.0);\ngl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n}'
glShaderSource(vertex, 1, vsource, 0)
glCompileShader(vertex)

fsource = 'void main()\n{\ngl_FragColor = gl_Color;\n}'
glShaderSource(fragment, 1, fsource, 0)
glCompileShader(fragment)

self.program = glCreateProgram()
glAttachShader(self.program, vertex)
glAttachShader(self.program, fragment)
glLinkProgram(self.program)
glValidateProgram(self.program)

error:

glShaderSource(vertex, 1, vsource, 0)
ctypes.ArgumentError: argument 3: <type 'exceptions.TypeError'>:
expected LP_LP_c_char instance instead of str

definition:

glShaderSource = _link_function('glShaderSource', None, [GLuint,
GLsizei, POINTER(POINTER(GLchar)), POINTER(GLint)], 'VERSION_2_0')


How can I convert a str to LP_LP_c_char/POINTER(POINTER(GLchar))? (It
seems more like a ctypes question. :-( )

PS:

More advanced examples under http://pyglet.googlecode.com/svn/trunk/examples/
would be great!
(Shader, VBO, ...)

Richard Jones

unread,
Feb 24, 2008, 5:26:11 PM2/24/08
to pyglet...@googlegroups.com
On Mon, Feb 25, 2008 at 9:21 AM, Alex <Orch...@gmail.com> wrote:
More advanced examples under http://pyglet.googlecode.com/svn/trunk/examples/
would be great!
(Shader, VBO, ...)
 
Have a look under experimental :)


     Richard

Drew Smathers

unread,
Feb 24, 2008, 5:26:22 PM2/24/08
to pyglet...@googlegroups.com

You need to convert the string to the protype C type, like:

def _shaderSource(text, shader_type=GL_VERTEX_SHADER):
buff = c.create_string_buffer(text)
c_text = c.cast(c.pointer(c.pointer(buff)), c.POINTER(c.POINTER(GLchar)))
shader = glCreateShader(shader_type)
glShaderSource(shader, 1, c_text, None)
return shader


--
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
d.p.s

Drew Smathers

unread,
Feb 24, 2008, 5:27:58 PM2/24/08
to pyglet...@googlegroups.com
On Sun, Feb 24, 2008 at 5:26 PM, Drew Smathers <drew.s...@gmail.com> wrote:
>
> You need to convert the string to the protype C type, like:
>

Oops :-),

s/protype/proper type

Drew Smathers

unread,
Feb 24, 2008, 5:31:51 PM2/24/08
to pyglet...@googlegroups.com
On Sun, Feb 24, 2008 at 5:26 PM, Drew Smathers <drew.s...@gmail.com> wrote:
> def _shaderSource(text, shader_type=GL_VERTEX_SHADER):
> buff = c.create_string_buffer(text)
> c_text = c.cast(c.pointer(c.pointer(buff)), c.POINTER(c.POINTER(GLchar)))
> shader = glCreateShader(shader_type)
> glShaderSource(shader, 1, c_text, None)
> return shader
>
>

Also, Just to clarify, the variable c in the above example is the
module ctypes. So this comes from:

import ctypes as c

Alex

unread,
Feb 24, 2008, 5:44:26 PM2/24/08
to pyglet-users
Thanks! It works!

This was more complex than i thought! I will look into ctypes next
week.

John Lehmann

unread,
Feb 26, 2008, 9:11:07 AM2/26/08
to pyglet...@googlegroups.com
Just in case you missed the "look under experimental" comment earlier, in /experimental you will find shader.py which provides a class that encapsulates a lot of this for you, with some examples - pinch.py being my favourite.

Creating a shader can be as simple as:

pinch_f = '''
uniform sampler2D tex;
...

void main() {
   vec2 h = vec2(1.0/size.x, 0.0);
   ...
   gl_FragColor = texture2D(tex, pos + v);
}
'''
pinch = shader.ShaderProgram()
pinch.setShader(shader.FragmentShader('pinch_f', pinch_f))
pinch.install()
pinch.uset2F('size', float(kitten.width), float(kitten.height))


(Of course everything stops being simple after that and I can't for the life of me work out how to make two textures available to the shader, but I haven't quite given up yet, and the realtime shader running against the webcam in my monitor is a fun way of programming and being vain at the same time).

John

Reply all
Reply to author
Forward
0 new messages