Sharing my code on saving TerrainGrid Height-maps to PNG files

Hi,

Seen it mentioned elsewhere in this forum so I’d thought I’d share what I came up with.

It does use an external library though - pngj from http://code.google.com/p/pngj/



[snippet id=“73”]



I used PNGJ as it worked and was simple, I did have minor saving using java’s own internal library but the image was all banded and wrong, I think due to a mix up of big vs little endianess…



Anyways hope this will help some people.



On a side note: did I hear a rumour on TerrainGrid being removed at some point in the development? If so I’d better recode now as my game project is a very long term one and I don’t want to be changing it down the track…



Enjoy!



Radan

1 Like

Thanks for sharing. Note that there is a JmeSystem.writeImageFile() method that writes image data most efficiently per platform (as in it uses platform-specific calls to do that).

Cool! People have definitely requested having this, and now they do :slight_smile:

TerrainGrid will not be removed, however it might be suggested to not be used as soon as something comes in to replace it.

Hi,

Happy to share, I’ll take a look at JmeSystem.writeImageFile() and see if I can swap out the external library. Will post back if I do.



I have been toying with both TerrainGrid and a routine I wrote myself that uses a 3x3 grid of tiles keeping the ‘Player’ always in the center tile. I created it initially as I couldn’t make sense of TerrainGrid but swapped out later as I was’t getting the performance (fps) that terraingrid was giving me. (80 fps vs 300 initially but I’ve since increased my code to about 200 fps - this is just terrain no objects)



The advantage I had with my own TerrainTiler code was that it would save the terrain to a file (using BinaryExporter) for later quick loading so it wouldn’t need to generate a new terrainquad from scratch each time. Also if I edited the heights or splats I’d just re-save the quad to update.

I may still go back to my routine if I can work out some optimisations… One issue I have with TerrainGrid is memory usage, I’ve watched it consume GIGS of memory as I move around from cell to cell… I’m only using a 1024 size terraingrid with basic height-based texture mapping. Any ideas?



Thanks!



Radan.

The memory usage with TerrainGrid is one of its biggest drawbacks.

I was hoping it was me and bad coding :cry:

Maybe I’ll work some more in my routine and see if I can get it optimized, never know it may be your terraingrid replacement? :smiley:

I’m still very much in the experimenting on code part of my project so I may as well play around before I commit to one direction.

Thanks again for the input.

If you are tiling together terrainQuads, you can use the NeighbourFinder interface to make TerrainQuad able to find its neighbours so it can seam up its edges when the level of detail changes. TerrainGrid was implemented before this change.

You can look at TerrainTestTile.java for an example