2

My goal was to color the vertexes according to their order

EDIT: long time goal: access to preceding and following vertexes to simulate gravity behavior

i've used following code

#version 120
#extension GL_EXT_geometry_shader4 : enable
void main( void ) {
    for( int i = 0 ; i < gl_VerticesIn ; i++ ) {
        gl_FrontColor = vec4(float(i)/float(gl_VerticesIn),0.0,0.0,1.0);
        gl_Position = gl_PositionIn[i];
        EmitVertex();
    }
}

but all vertexes are drawn black, it seem that i is always evaluated as 0, am i missing something or doing it wrong?

EDIT: figured the meta-problem out: how to feed all me model geometry into single geometry shader call, so the mainloop iterates over all the vertexes, not for every triangle.

Valerij
  • 27,090
  • 1
  • 26
  • 42
  • 1
    Are you sure you configured the geometry shader to take in triangles (or whatever you expect, except points). The only case when `gl_VerticesIn` should be 1 is when you configured the geometry shader to take points as input. – Christian Rau Sep 04 '11 at 22:19
  • well i actually use `GL_POINTS` as input type, now i understand that gl_VerticesIn indicates the index of a vertex within a geometry object (eg. trinagle) and not absolute scene index. But how can i let the geometry shader iterate over all my vertexes (64 in my case) – Valerij Sep 05 '11 at 08:27

1 Answers1

2

You don't let a single geometry shader invocation iterate over all your vertexes, it is called for every original primitive (point, line, triangle, ...).

The solution is much easier: In the vertex shader (that is actually called for every vertex) you can read the special variable gl_VertexID, which contains the vertex's index. That index is either just a counter incremented for every vertex (if using glDrawArrays) and reset by every draw call, or the index from the index array (if using glDrawElements).

EDIT: Regarding the long time goal. Not directly but you might use a texture buffer for that. This basically enables you to get direct linear array-access to a buffer object (in your case the vertex buffer) which you can then just index with this vertex index. But there might also be other ways to accomplish that, which may suffice for another question.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • and it is possible (and when how) to access an other vertex using this index? (see long time goal) – Valerij Sep 05 '11 at 13:27
  • actually thought about a 1d texture for this. actually funny to pass one vertex and an texture to pipeline and get an entire solar system rendering back `:)` – Valerij Sep 05 '11 at 13:48