GLSL Geometry shader and generic vertex attributes
Asked Answered
F

2

7

So I've been trying for a while now, to pass a vertex attribute array into the geometry shader. It is an array of float (where the attribute per vertex is just a float value)

Now, when I put this in the geometry shader:

attribute float nodesizes;

The shader compiler complains:

OpenGL requires geometry inputs to be arrays

How do I exactly pass it along?

Also, here's my code for putting the vertex attrib:

glBindAttribLocation(programid, 1, "nodesizes");
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, array);
glEnableVertexAttribArray(1);

Am I doing something wrong?

Felder answered 30/6, 2011 at 13:26 Comment(2)
Quite unrelated comment: Keep in mind that you have to call glBindAttribLocation before linking the program to have an effect, as your code suggests something different.Divulsion
Ah, that is something that will be noted. thanksFelder
D
9

The geometry shader doesn't get attributes. The vertex shader gets attributes and puts out varyings (speaking in the old syntax). These can then be read in the geometry shader, but as an array, as one geometry shader invocation follows multiple vertex shader invocations. Something like this:

vertex shader:

attribute float nodesize;
varying float vNodesize;

void main() {
    ...
    vNodesize = nodesize;
    ...
}

geometry shader:

varying float vNodesize[];

void main() {
    ...vNodesize[i]...
}

The names are arbitrary, but of course the names of the varyings have to match in both shaders. I hope you didn't just mess up the terms vertex shader and geometry shader.

Divulsion answered 30/6, 2011 at 14:21 Comment(9)
I see, but the question is, what will be the "i"? I only have one vertex shader, so will i be 0?Felder
@Felder If the i has no meaning (or is always 0), then you should rethink if you really need a geometry shader. Inform yourself what a geometry shader really does, it is only used in rather special cases. Maybe you messed up the terms vertex shader and geometry shader.Divulsion
I've been using the geometry shader to emit a cube from a single vertex. But the input from a single pass of the geometry shader is still only a vertex, which means i is always 0.. am I right?Felder
@Felder Ok, then the geometry shader is a good idea and i is indeed just 0 (so you don't need an "i"). But you still need an array for the varying, as the geometry shader syntax demands it.Divulsion
@Felder It may be, that when you are using the old syntax, you need to give the varying array a specific size (1 in your case) and cannot just use [], but I'm not sure about that, as I have not much experience with geometry shaders.Divulsion
@Felder So did my answer work or have you found another solution? In the former case, acceptance and up-vote would be appreciated. In the latter, I would like to hear your solution.Divulsion
It technically should work, but for some reason the geometry shader is complaining. I've resorted to using texture coordinates to pass the attributes, for now.Felder
@Felder What does it complain about now?Divulsion
I had the same problem. Using varying in float vNodesize; (notice the in) solved it!Sims
I
-1

Which open gl version are you using?

From the Open GL language specification (4.10.6)

A single invocation of the geometry shader executable on the geometry processor will operate on a declared input primitive with a fixed number of vertices. This single invocation can emit a variable number of vertices that are assembled into primitives of a declared output primitive type and passed to subsequent pipeline stages.

This means that you need to specify on witch kind of primitive (point, line, trianle, quad) the geometry assumes.

If I understand you correctly you want the geom shader to emit a cube for each vertex. So you should set the geometry input type to point.

As a reslult the geom shader code should start like this:

varying float vNodesize[];

void main() {
    ...vNodesize[i]...
}

Where i = 0 for points =1 for lines and so on.

Iridectomy answered 30/6, 2011 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.