Is there a better way to do this?
I'm drawing 2D, concave, sometimes multicontoured, sometimes self intersecting polygons with OpenGL. Here is a sample:
Right now, I take the points which if connected would result in the polygon's outline. Then I put these into the GLUTesselator where triangles come out. I then make texture coordinates and texture the polygon.
The absolute slowest component is the tessellation / triangulation. 开发者_如何学GoGiven that I just need to draw these as you see, what are faster alternatives to triangulating? Could I possibly find a painting algorithm that would set the pixel appropriately?
Thanks
First a simpler problem: suppose you just wanted to use one color. You could start with the list of outlines, then scan the whole window by row and column: whenever you crossed a boundary, you would increment or decrement (depending on the sense of the crossing) a counter how_many_outlines_am_I_inside
. When it's zero, paint the pixel white, otherwise paint it green (and I guess black for the boundary itself). This will handle self-intersecting curves correctly.
Now for the shades. Judging by your example, shading is uniform vertically, but changes horizontally, scaled to the full width of the outline. So instead of a simple counter, you'll need a stack (I recommend stl::list) of outlines, so that you can keep track of which outline (of the ones you're inside) is uppermost, so that you can calculate what fraction of the distance you've covered from xmin to xmax. Finally for the translucency (as with the star partly visible through the rectangle): you'll have to decide on the rules yourself, I can't infer them by eyeball, but the stack should make it easy to implement.
Want some help with the C++ code?
精彩评论