1
\$\begingroup\$

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

\$\endgroup\$
  • \$\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 Feb 20 '17 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 Feb 20 '17 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 Feb 20 '17 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\$ – ratchet freak Feb 21 '17 at 9:05
2
\$\begingroup\$

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

gl_PrimitiveID

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

| improve this answer | |
\$\endgroup\$
  • \$\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 Aug 6 '18 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.