Texture Atlas creation

A forum for discussing development work on Ardor3D add-ons and extensions. Basically, anything that is Ardor3D that is not in the ardor3d-core module.
Forum rules
Developer discussions only. Newbie questions belong in the HELP! forum. This is a forum for discussing development work on Ardor3D add-ons and extensions. Basically, anything that is Ardor3D that is not in the ardor3d-core module.

Texture Atlas creation

Postby MrCoder » Sun Dec 26, 2010 6:32 am

Got some code in for automatic creation of texture atlases, iteration 1. Probably lots of room for improvements to be able to use it in more situations. Currently, you add meshes to the atlas creator and it packs its texture into atlases, assigns the mesh the atlas it was added too and also changes it's texture coordinates to map into the atlas. So, basic usage atm is:

Create a texture packer with an atlas size. When an atlas is full it will move on to a new one. Textures that are bigger than the set size will be skipped.
Code: Select all
final TexturePacker packer = new TexturePacker(256, 256);


Add meshes to the packer. (Many options here as well if you want it to use a different texture index source/target)
Code: Select all
packer.insert(mesh)


Finally create all the atlases and apply to all meshes involved
Code: Select all
packer.createAtlases();


See AtlasExample for an example of using this. One very nice thing except for any performance improvement is that it works very well in combination with the MeshCombiner. If an atlas pack is issued on meshes you want to combine, texturing will now work as well.
(See TestAtlasPacker for a swing test of the actual texture pack algorithm)
Destroy, Erase, Improve
User avatar
MrCoder
Site Admin
 
Posts: 755
Joined: Mon Nov 03, 2008 8:56 am
Location: Stockholm, Sweden

Re: Texture Atlas creation

Postby renanse » Sun Dec 26, 2010 11:58 am

Very cool! 8-)
Gratitude is a mark of a noble soul and a refined character.
User avatar
renanse
Site Admin
 
Posts: 2955
Joined: Tue Oct 28, 2008 6:49 pm
Location: Austin, TX

Re: Texture Atlas creation

Postby MrCoder » Sun Dec 26, 2010 3:47 pm

Thanks :)

Things I want(need) to add:

1. More safety checks, error handling
2. User definable border size
3. A way to use this on images in "offline" mode, which packs images from disk into atlases and spits out a file with offsets for each texture into the atlases. I would have a use for this for ArdorCraft for example, for easy atlas creation as part of the pipeline.
Destroy, Erase, Improve
User avatar
MrCoder
Site Admin
 
Posts: 755
Joined: Mon Nov 03, 2008 8:56 am
Location: Stockholm, Sweden

Re: Texture Atlas creation

Postby sorcerer » Thu Dec 30, 2010 8:58 am

Thanks, that will be of use, but for now with my own code I always get a

java.lang.IndexOutOfBoundsException: 8
at java.nio.DirectFloatBufferU.put(DirectFloatBufferU.java:286)
at com.ardor3d.extension.atlas.TexturePacker.insert(TexturePacker.java:80)

Because in my case in
Code: Select all
final FloatBuffer source = cachedParameter.getTextureCoords();
final FloatBuffer destination = parameterObject.getTextureCoords();
   for (int i = 0; i < source.limit(); i++) {
           destination.put(i, source.get(i));
   }

I happen to have destination.limit = 8 and source.limit = 24 !

For now I did not study texture coordinates a lot (just read what's on wikipedia), I hope I will be able to understand the matter !

Thanks again for sharing this texturePacker, really makes a great combo with meshcombiner !
User avatar
sorcerer
newcomer
 
Posts: 16
Joined: Mon Jan 05, 2009 12:46 pm

Re: Texture Atlas creation

Postby MrCoder » Thu Dec 30, 2010 3:03 pm

I bet there are more bugs in there than that! :) I'll try to fix this right away!
Destroy, Erase, Improve
User avatar
MrCoder
Site Admin
 
Posts: 755
Joined: Mon Nov 03, 2008 8:56 am
Location: Stockholm, Sweden

Re: Texture Atlas creation

Postby MrCoder » Thu Dec 30, 2010 3:19 pm

Try now. Keep the bugreports coming! thanks :)
Destroy, Erase, Improve
User avatar
MrCoder
Site Admin
 
Posts: 755
Joined: Mon Nov 03, 2008 8:56 am
Location: Stockholm, Sweden

Re: Texture Atlas creation

Postby sorcerer » Thu Dec 30, 2010 9:48 pm

It works now, thanks !

Just a small hint : I call the packer from an initialization method (not from render loop like in your example), so I had to do myNode.updateGeometricState(0) before packIntoAtlas , or else I get a nullpointer in TextureParameter constructor, at mesh.getWorldRenderState(StateType.Texture)).getTexture.

--> After writing this I tried with mesh.getLocalRenderState in TextureParameter constructor and it seems to work ...
User avatar
sorcerer
newcomer
 
Posts: 16
Joined: Mon Jan 05, 2009 12:46 pm

Re: Texture Atlas creation

Postby MrCoder » Fri Dec 31, 2010 2:57 am

Cool, I just want to make sure it works also for meshes that got their texture from a spatial parent higher up in the tree. I'll find a way to solve it when no update has been run. Thanks again for the feedback!
Destroy, Erase, Improve
User avatar
MrCoder
Site Admin
 
Posts: 755
Joined: Mon Nov 03, 2008 8:56 am
Location: Stockholm, Sweden

Re: Texture Atlas creation

Postby renanse » Fri Dec 31, 2010 10:52 am

You might do something like check isDirty and update the world renderstates if needed.
Gratitude is a mark of a noble soul and a refined character.
User avatar
renanse
Site Admin
 
Posts: 2955
Joined: Tue Oct 28, 2008 6:49 pm
Location: Austin, TX

Re: Texture Atlas creation

Postby MrCoder » Fri Dec 31, 2010 3:28 pm

good thinking!
Destroy, Erase, Improve
User avatar
MrCoder
Site Admin
 
Posts: 755
Joined: Mon Nov 03, 2008 8:56 am
Location: Stockholm, Sweden

Next

Return to Extensions Development Discussions

Who is online

Users browsing this forum: No registered users and 1 guest

cron