3

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.

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • Why are you using floating-point constants for an unsigned integer vec3? Shouldn't your GLSL compiler be complaining about that at the very least? (e.g. No overload of `uvec3 (...)` that takes 2 floating-point components and one integer). And for that matter, are you honestly drawing into an integer color buffer? Or is this something you tried to do as a hack to make `gl_PrimitiveID` work? – Andon M. Coleman Jan 05 '14 at 03:57
  • Sorry, my bad, gl_PrimitiveID works fine, the problem is with my framebuffer. – user1784182 Jan 05 '14 at 06:06

1 Answers1

4

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.

Michael IV
  • 11,016
  • 12
  • 92
  • 223