I want to know the triangle ID of each rendered pixel is coming from. Is there a way to do this in OpenGL?

  • \$\begingroup\$ Yeah, it's called picking. Are you using modern GL or legacy GL? How big are your IDs? What have you tried? \$\endgroup\$
    – MickLH
    Commented Feb 20, 2017 at 21:31
  • \$\begingroup\$ I'm using modern opengl, my IDs can get up to millions. I'm in the process of trying gl_primitiveID. \$\endgroup\$
    – user109352
    Commented Feb 20, 2017 at 21:34
  • \$\begingroup\$ Ok just bind an integer texture to an FBO, disable all the fancy stuff like blending and shading, and output the primitive ID to the color buffer that has the integer texture bound. I'm too lazy to write a full answer right now. \$\endgroup\$
    – MickLH
    Commented Feb 20, 2017 at 21:36
  • \$\begingroup\$ create a mapping from triangle ID to color, there are 16 million unique colors to pick from in a 24 bit RGB texture format. \$\endgroup\$ Commented Feb 21, 2017 at 9:05

1 Answer 1


If you are using glsl version 410 or higher, you can use


to get a triangle id per pixel on fragment shader. Hope this helps.

  • \$\begingroup\$ Glad to hear that you found a solution! Don't forget to mark it as accepted - it helps others who may run into the same or similar problems. \$\endgroup\$
    – Pikalek
    Commented Aug 6, 2018 at 23:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .