views:

667

answers:

1

I have an application that draws 3-d map view marked up lines that show various features. I am porting the map over to an OpenGL-ES architecture, but am having a bit of trouble working out how to display dashed lines.

Doing a lot of googling, I've found many references to the idea that drawing dashed lines and polygons were removed from OpenGL-ES as they can be easily emulated using textures triangles. That's great, but I can't find anyone that actually does this emulation and/or has a description of the steps involved.

An example, of one problem I have encountered trying to prototype out this concept is perspective squeezes my lines to invisible as they go towards the horizon. Using LINE_STRIP, this doesn't happen, and the lines remain a constant width in the map.

Any advice on how to achieve dashed constant width lines in a perspective view would be much appreciated.

+1  A: 

I believe you can apply a texture to a line, not just a triangle. You'll need to set texture coordinates for each end of the line; the texture will be interpolated linearly along the line.

The effectiveness of this solution should be invariant of whether you use lines or line strips - line strips are just a way to create lines with fewer vertices.

There is one other problem: the tendency of the texture pattern to become compact as a line goes away from the camera. This happens because texture coordinate interpolation is perspective-correct even for lines (see section 3.5 of the GL spec).

There are two ways to get around this:

  1. If you can calculate a "q" coordinate for your texture that undoes the perspective, you can restore screen-space texturing. This technique is probably too performance-expensive.

  2. You can project the texture in eye space (e.g. glTexGen).

Texture coordinate generation is of course not available in GLES 1.1, but if you are using vertices by array, you can fake it by:

  • Setting your texture coordinate array to be your vertex coordinate array and
  • Using the texture matrix to "transform" the vertices.

The disadvantage of this technique is that the texture pattern will be in fixed screen space - that is, the texture won't run across the lines.

Ben Supnik
Thanks, I'll give this a go. I tried texturing the line previously, but didn't get it working - I might have given up to early.I would want the texture repeating so that I got a consistent dashes on the line no matter how long it grew.
Piklor
Without shaders I don't think you can do this without calculating the ST coords per frame.With shaders, you would need to:- Encode the direction of the line in model space on your mesh.- In the vertex shader, transform it into eye-space coordinates.- Generate the actual UV map in screen space (per pixel)- Rotate the sampling of the UV map by the transformed (and normalized) line direction vector.Suffice it to say, this is pretty non-trivial if you aren't very comfortable with matrix transforms and coordinate spaces. :-(
Ben Supnik
Also, you might want to look at this:http://homepage.mac.com/arekkusu/bugs/invariance/HWAA.htmlIt looks like a lot of consumer GPUs don't do stipple right under a variety of conditions. For horizontal and vertical stipple (one of the few cases they get right) simply projecting a checkerboard repeating texture is enough.
Ben Supnik
It appears as though I'm fighting against the grain on this one, and it's not feasible to to what I want in OpenGL-ES.The effect I was after is not worth the complication it seems to be requiring. Drawing textured triangles that fade into the horizon should be an acceptable alternative. Thanks for your help.
Piklor