PolygonSpriteBatch refactoring

Discussions for core devs and contributors. Read only for normal forum members to keep the noise low

PolygonSpriteBatch refactoring

Postby NateS » Wed Sep 11, 2013 1:14 am

I want to render some triangles. Obviously can't use SpriteBatch. I could write a TriangleBatch, but PolygonSpriteBatch should probably be able to render triangles. It doesn't use indices though. Why? I've refactored (not committed) EarClippingTriangulator to output triangle indices instead of vertices. This matches results from the new DelaunayTriangulator I committed a few days ago. I've changed PolygonRegion to have triangles and PolygonSprite and PolygonSpriteBatch to use them.

It works fine, except PolygonRegion can load vertices from a file. What is this "polygon shape file" file format? Eg, tree.psh in gdx-tests data. Did we invent this format? Do tools somewhere output this? Does/can the format contain triangles? Currently I just generate sequential indices if the PolygonRegion doesn't have them, but this is wasteful. Can we always have triangles?
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: PolygonSpriteBatch refactoring

Postby BurningHand » Wed Sep 11, 2013 2:23 am

I think very few people even know that PolygonSpriteBatch exists, let alone use it. I'd venture a guess that You could have your way with it and not really affect many folks.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: PolygonSpriteBatch refactoring

Postby NateS » Wed Sep 11, 2013 2:27 am

Sure, I'm not too shy to refactor, but I need to understand it so I can sodomize it properly. :twisted:
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: PolygonSpriteBatch refactoring

Postby BurningHand » Wed Sep 11, 2013 2:39 am

Hmm, I just noticed that you got a capital Y. That's special.

Anyway, I think bach is probably the best one to answer questions about the decisions behind this class - I believe he made it for a particular case.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: PolygonSpriteBatch refactoring

Postby NateS » Wed Sep 11, 2013 3:33 am

Capital Y?
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: PolygonSpriteBatch refactoring

Postby BurningHand » Wed Sep 11, 2013 3:55 am

BurningHand wrote:I'd venture a guess that You could have your way with it...


A capital Y. :)
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: PolygonSpriteBatch refactoring

Postby bach » Wed Sep 11, 2013 7:30 am

Oh the mysteries of libgdx code :D

I can't remember the reason why we didn't use indices. So if you have a working solution there .. excellent!

Regarding the file-format. The point of introducing PolygonSpriteBatch was to reduce fill-rate. So take an image and only draw the visible pixels (or as close to that as possible). To define the drawable region one could use this: https://code.google.com/p/libgdx-polygoneditor/ which would output that psh format (and yes we "invented" it). From what I remember (without reading into the code), the format just contained vertices of a polygon, which then would be triangulated at load. I assume that's also the reason we didn't use indices, because the Triangulator wouldn't spit them out.

I don't really see any better way to define the drawable regions except via a visual editor?

The PolygonSpriteBatch was written with a very specific goal in mind, so yea it's definitely not made for a wide range of applications. If we can bend it to do more, I'm more than happy :)

I guess at some point we might have to reconsider the naming of the thing. Maybe we rename the SpriteBatch to a more general TriangleSpriteBatch? And we just have some utility classes that can be used with it (PolygonSprite/Region?)

For what it's worth, I had proposed to Mario before to completely drop the thing out of core. It think it's a bit bloat-y, being more or less a castrated version of SpriteBatch. I don't think it's being used much anyway, so if we want to do some radical refactoring I'd welcome it.

Bach
bach
 
Posts: 713
Joined: Mon Mar 07, 2011 1:50 am

Re: PolygonSpriteBatch refactoring

Postby NateS » Wed Sep 11, 2013 2:35 pm

Okie, cool. I committed my refuctoring, see Polygon* classes:
https://github.com/libgdx/libgdx/tree/m ... aphics/g2d

The PSH format doesn't have triangles, so I ended up loading the "shape" (polygon hull) from it and then triangulating at load time. Probably better to store the triangles in the file, but I'll let the polygon tool maintainers do that. Also this way existing PSH files still work.

I separated loading PSH files into PolygonRegionLoader. It uses EarClippingTriangulator, which has been rewritten to be more efficient and reuse its buffers, in addition to outputting triangles instead of vertices.

PolygonSpriteBatch has this method:
Code: Select all
draw(Texture texture,
float[] polygonVertices, int verticesOffset, int verticesCount,
short[] polygonTriangles, int trianglesOffset, int trianglesCount)

Which is pretty much my reason for doing all this. I can use this to draw batched triangles, yay!

PolygonSpriteBatch has to set the indices every frame. Bach vaguely remembers maybe this is not efficient on some Android devices. A polygon with n vertices has a triangulation of n-2 triangles. The tree.psh example has 28 vertices, which is 26 triangles, which is 5*28=140 floats for the vertices + 78 shorts for the indices, which is 716 bytes. If all vertices were sent instead, it would be 26*3=78 vertices which is 390 floats, which is 1560 bytes. It is less than half the data being sent to the GPU, though data is sent twice instead of just once. Hopefully since we are batching lots of data, having two buffer binds and memory transfers will not be an issue.

For the PolygonSpriteBatch methods that draw PolygonRegion and PolygonSprite, there are roughly 1/3 the number of vertices that need to be transformed on the CPU compared to before the refactoring.

tescott mentioned he uses PolygonSpriteBatch extensively, but I don't think the changes will be much effort for him. Hopefully it works faster. I'll ask him if he sees a difference, since he has real world usage.
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: PolygonSpriteBatch refactoring

Postby NateS » Wed Sep 11, 2013 7:00 pm

I started a public thread with info on how the new stuff works so others can discuss:
viewtopic.php?f=11&t=10779
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am


Return to Libgdx Development

Who is online

Users browsing this forum: No registered users and 1 guest