GLSL Noob Question

Hi all,

I have been messing about with a geometry shader in GLSL and something strange is going on.

Basically, I was playing about with texturing the a model. Normally (just a vert and frag shader), its just a case of in the vert shader…

out vec2 TextCoords

then of course set TextCoords = to the buffer from the VAO.

then take them into the frag shader…

in vec2 TextCoords;

Then use sampler2D and the glsl texture method.

In actual fact this in itself is quite strange, as the buffer in the VAO holds all the texture coordinates, not simply a vec2 as declared, to me this should if anything give me the first pair of texture coordinates. I am presuming something is going on in the background.

So, to the geometry shader, this is where things get strange. When I add in a geometry shader that is exactly what happens, I only get a single coordinate, and therefore a single colour from the texture. This is fixed using interface blocks, the code then becomes,
in the vert…

out VS_OUT{
vec2 texCoords;
} vs_out;

in the geom…

in VS_OUT {
   vec2 texCoords;
} gs_in[];

in the frag…

in vec2 TexCoords;

This is really quite baffling for someone who is new to GLSL, in fact the only on that makes sense to me from a programming perspective is…

in VS_OUT {
   vec2 texCoords;
} gs_in[];

as after all I am saving the buffer from my VAO into an array of vec2. So why don’t I need to use arrays all the time, and why do I need to use them when I add in the geom shader?

Thanks for any help.

The vertex shader is invoked for each vertex. It gets one value from each enabled attribute array as input. Its outputs form a single vertex. If a geometry shader is present, it is invoked for each primitive. Its inputs are all of the vertices for the primitive, so for a triangle the inputs are arrays of length 3, with one entry for each vertex.

1 Like

Ah right,

OK thanks, if I understand then. Once the vertex shader is done, it passes the data to the geometry shader, and the output for the texture coordinates are aggregated in to the interface block and we are now working with an array? I have the GLSL cookbook but I dont recall it going into this kind of stuff, can you recommend any good books?

Anything relevant should be in the OpenGL Programming Guide. The ultimate reference is the specification, but that’s not particularly easy to read, especially for the interactions between the client and shaders (because the specification is split into the API specification and the GLSL specification).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.