How do I render thick 2D lines as polygons?

前端 未结 9 1166
心在旅途
心在旅途 2020-12-12 18:17

I have a path made up of a list of 2D points. I want to turn these into a strip of triangles in order to render a textured line with a specified thickness (and other such th

相关标签:
9条回答
  • 2020-12-12 18:48

    I just found this amazing work:

    http://www.codeproject.com/Articles/226569/Drawing-polylines-by-tessellation

    It seems to do exactly what you want, and its licence allows to use it even in commercial applications. Plus, the author did a truly great job to detail his method. I'll probably give it a shot at some point to replace my own not-nearly-as-perfect implementation.

    0 讨论(0)
  • 2020-12-12 18:49

    See if Delaunay triangulation can help.

    0 讨论(0)
  • 2020-12-12 18:51

    From your image it looks like that you are drawing box around line segments with FILL on and using orange color. Doing so is going to create bad overdraws for sure. So first thing to do would be not render black border and fill color can be opaque.

    Why can't you use GL_LINES primitive to do what you intent to do? You can specify width, filtering, smoothness, texture anything. You can render all vertices using glDrawArrays(). I know this is not something you have in mind but as you are focusing on 2D drawing, this might be easier approach. (search for Textured lines etc.)

    0 讨论(0)
  • 2020-12-12 18:54

    I think I'd reach for a tessellation algorithm. It's true that in most case where these are used the aim is to reduce the number of vertexes to optimise rendering, but in your case you could parameterise to retain all the detail - and the possibility of optimising may come in useful.

    There are numerous tessellation algorithms and code around on the web - I wrapped up a pure C on in a DLL a few years back for use with a Delphi landscape renderer, and they are not an uncommon subject for advanced graphics coding tutorials and the like.

    0 讨论(0)
  • 2020-12-12 18:55

    I ended up having to get my hands dirty and write a small ribbonizer to solve a similar problem.

    For me the issue was that I wanted fat lines in OpenGL that did not have the kinds of artifacts that I was seeing with OpenGL on the iPhone. After looking at various solutions; bezier curves and the like - I decided it was probably easiest to just make my own. There are a couple of different approaches.

    One approach is to find the angle of intersection between two segments and then move along that intersection line a certain distance away from the surface and treat that as a ribbon vertex. I tried that and it did not look intuitive; the ribbon width would vary.

    Another approach is to actually compute a normal to the surface of the line segments and use that to compute the ideal ribbon edge for that segment and to do actual intersection tests between ribbon segments. This worked well except that for sharp corners the ribbon line segment intersections were too far away ( if the inter-segment angle approached 180' ).

    I worked around the sharp angle issue with two approaches. The Paul Bourke line intersection algorithm ( which I used in an unoptimized way ) suggested detecting if the intersection was inside of the segments. Since both segments are identical I only needed to test one of the segments for intersection. I could then arbitrate how to resolve this; either by fudging a best point between the two ends or by putting on an end cap - both approaches look good - the end cap approach may throw off the polygon front/back facing ordering for opengl.

    See http://paulbourke.net/geometry/lineline2d/

    See my source code here : https://gist.github.com/1474156

    0 讨论(0)
  • 2020-12-12 18:57

    In my case I could afford to overdraw. I just drow circles with radius = width/2 centered on each of the polyline's vertices.

    Artifacts are masked this way, and it is very easy to implement, if you can live with "rounded" corners and some overdrawing.

    0 讨论(0)
提交回复
热议问题