GLSL - fragment shader - each triangle with distinct color

1.1k views Asked by At

I want to write a shader program which will render each triangle of the mesh in distinct color (so I could pick one triangle). I tried to use gl_PrimitiveID, but it always returns 0, for example:

#version 330

out uvec3 FragColor;

void main()
{
    FragColor = uvec3(0.0, 0.0, gl_PrimitiveID);
}

This shader always renders black color.

1

There are 1 answers

0
Michael IV On

To achieve what you want in a simplest way I would pass additional vertex array as color attribute containing colors per triangle into vertex shader.Then pass it as varying output into your fragment shader.That's it.This way you can specify exactly colors for each vertex.