0

Basic question: with OpenGL, can a shader program be made to use a single glDrawArrays call (with GL_POINTS) to draw a different point sprite at each vertex?

More info: I have an OpenGL desktop program (using SharpGL in a WPF application) that can display thousands of 2D tracks. In part, a track is a series of time stamped points and a portion of the points are displayed depending on a time span around a changeable CurrentTime. Other properties of each point determine the point's color. Each track binds it's vertex array, color array, and a time stamp array and calls a glDrawArrays to render its points. A shader program does the rest.

I've recently started using point sprites to give different types of tracks different symbols for their points. I'd like to give different points on a track different symbols depending on other attributes of each point. I'd like to do this with a single glDrawArrays call for each track. So the thought is that an array of sprites would do the trick (applying a different sprite to each vertex). Is this possible? Am I missing a better solution?

FTLPhysicsGuy
  • 1,035
  • 1
  • 11
  • 23

1 Answers1

2

Sure. OpenGL automatically generates texture coords from 0.0 to 1.0 across each sprite, but there's no reason your fragment shader can't change them. I'd put all the sprite images into one large texture atlas, and pass the attribute(s) that determine which image to use into the fragment shader.

In OpenGL 3.3 or better, there is a built-in gl_PrimitiveID variable you can use within the fragment shader, which is a counter incremented for each point, line, or triangle drawn. (It's a bit more complicated with geometry or tessellation shaders.) This might also be useful.

Hope this helps.

Hugh Fisher
  • 2,321
  • 13
  • 8
  • `gl_PrimitiveID` is actually defined for fragment shaders beginning with GL 3.2 (it went core when Geometry Shaders did). – Andon M. Coleman Jun 23 '14 at 01:11
  • Thx for the reply! The texture alias sounds promising. I'll check it out. +1 and I'll accept this answer once I've researched. – FTLPhysicsGuy Jun 23 '14 at 11:54
  • ooops... alias = atlas in previous comment. – FTLPhysicsGuy Jun 23 '14 at 12:19
  • This was the answer that put me on the right track. I ended up using a sprite sheet (yeah, still learning a lot about OpenGl). The answer given [to this question](http://stackoverflow.com/questions/9609423/applying-part-of-a-texture-sprite-sheet-texture-map-to-a-point-sprite-in-ios) also helped a lot. – FTLPhysicsGuy Jun 24 '14 at 00:57