TD GLSL, 'gl_VertexID' odd behavior

Hello,

I’m a beginner that doesn’t understand GLSL so well.

I’ve tried to draw the basic (probably famous) ‘colorful triangle’ OpenGL example in a GLSL TOP

//vertex shader
out vec4 vs_color;

void main(void)
{
const vec4 vertices[3] = vec4[3](vec4(0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4(0.25, 0.25, 0.5, 1.0));
const vec4 colors[3] = vec4[3](vec4(1.0, 0.0, 0.0, 1.0),
vec4(0.0, 1.0, 0.0, 1.0),
vec4(0.0, 0.0, 1.0, 1.0));

int vertID = gl_VertexID;

vertID = vertID;

gl_Position = vertices[vertID] ;

vs_color = colors[gl_VertexID];

}

result:
triangleFailed.png

but there’s a glitch, it seems like the original triangle is rotated and duplicated.
I guess the ‘gl_VertexID’ goes beyond 3 in this case, unlike non-TouchDesigner GLSL versions.
I applied %3 to the ‘gl_VertexID’ and now it looks good (the picture below).

vertID = vertID; -> vertID = vertID %3;

newresult:
triangle.png

It would be great if anybody can explain what causes this glitch. Thanks.

The GLSL TOP draws quad, which is why you are seeing your vertex shader run 4 times and creating 4 vertices

1 Like