2

I want to render a set of textured quads in OpenGL, but I was wondering if there was any way to pass in a unique integer id with each quad to the shader pipeline?

The id needs to be available to the fragment shader so that it can be rendered to a texture, separately from the rest of the image, because I want to reference the texture later to find out what id is at what location on the screen.

genpfault
  • 51,148
  • 11
  • 85
  • 139
webwraith
  • 23
  • 5
  • 1
    How many quads are you talking here? There is `gl_PrimitiveID` available in the fragment stage, but if you draw more than 256 quads (or 128 if each one is broken into two triangles) you will overflow a general (8-bit) single-channel image format. So then you would have to encode your ID across multiple channels; not terribly difficult, but you might be better off using a single-channel 16-/32-bit integer texture at that point. – Andon M. Coleman Jun 20 '14 at 19:45
  • I'll probably have quite a few, as I'll be using the system for an isometric game, and intend to use it for movement, attacking enemies, picking up items, moving through doors, etc. I think I'd prefer the option of a 32-bit integer texture, to reduce the likelihood of having any clashes, especially as I want to use the high-order byte as a flag for the type of object at that point – webwraith Jun 21 '14 at 01:26

1 Answers1

2

Tack on another vertex attribute for the ID and pass that through to your fragment shader.

You can set up Multiple Render Targets (MRT) to render your IDs out to another framebuffer attachment.

Community
  • 1
  • 1
genpfault
  • 51,148
  • 11
  • 85
  • 139