Vertex Attributes using vertex_list and GLSL

116 views
Skip to first unread message

Adam Griffiths

unread,
Sep 12, 2012, 3:59:12 AM9/12/12
to pyglet...@googlegroups.com
I'm trying to pass vertex attributes to a GLSL vertex shader.

I've created the following vertex_list:
self.vertex_list = pyglet.graphics.vertex_list(
            num_verts,
            ('v3f/static', verts),
            ('t2f/static', tcs),
            ('0g1f', attribs),
            )

I've also put this in my GLSL vertex shader:

attribute float index;


But I'm unsure how to bind the attribute array to the GLSL input?

The GL spec says I should call glGetAttribLocationARB which receives the attribute name.
Should I call this, then use the returned value as the first value of the attribute?
Ie. "Xg1f" instead of "0g1f"


I'm using the GLSL shader wrapper from here incase that matters:


Cheers,
Adam

Adam Griffiths

unread,
Sep 13, 2012, 1:51:02 AM9/13/12
to pyglet...@googlegroups.com
Ok, I've answered my own question.

The first step is to create and link the shader.
Using the existing wrapper this is simple:

self.shader = Shader(
            vert = Data.vert_shader,
            frag = Data.frag_shader
            )

Then we query OGL for an attribute index

self.attrib = glGetAttribLocation(
            self.shader.handle,
            "index"
            )

Then when creating the vertex list, create a string that uses the queried for attribute value

        # set the attribute index
        attrib_type = "%ig1f" % (self.attrib)

        self.vertex_list = pyglet.graphics.vertex_list(
            <snip>
            (attrib_type, attribs),
            )

In the vertex shader you need an attribute with the name you queried for.
In this case:

attribute float index;


You can test it by this as a varying value to the fragment shader and rendering it as the colour:

vertex shader:
varying vec4 debug;

void main()
{
    <snip>
    debug = vec4( index, index, index, 1.0 );
}

fragment shader:
varying vec4 debug;

void main()
{
    // divide by the number of vertices
    gl_FragColor = debug / vec4( 1000, 1000, 1000, 1.0 );
}

BAM!

Hope that helps someone in the future.


Cheers,
Adam
Reply all
Reply to author
Forward
0 new messages