开发者

GLSL geometry shader: iterate over entire mesh

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 ma开发者_如何学Cin( 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜