Sign in to follow this  

How is this usually done?

This topic is 1763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi.

 

So I was wondering if there is a better way to deal with the multiple buffers/arrays/etc when I'm loading a heightmap (or other resource) because I'm producing a lot of duplicate data in the process.

 

Currently I point to the image and read it. From that I have a BufferedImage (I'm using Java) that holds all the heights.

 

That BufferedImage isn't directly usable for what I'm trying to do so I construct a vertex array out of its pixels, which is a plain array of floats (ie, XYZ-XYZ-XYZ and so on).

 

Now I read another image to get color data for each pixel (no texturing yet), again, I need to extract the data from that BufferedImage so I construct a new plain array of floats with a color per vertex (RGB-RGB-RGB and so on).

 

After that I construct an array of normals out of the vertex array I made before.

 

There I have all the data in 3 different arrays. I want an interleaved array to pass to OpenGL so I create a new array interleaving the data of the three float arrays.

 

Since OpenGL and Java don't get along that well, I also need to construct a FloatBuffer out of that interleaved array so I can pass the data to OpenGL.

 

Moreover, since I only retain the interleaved array, that's the one I'm using to get height information of the terrain to calculate the camera's height in the world. That means lots of offset this and offset that to get around the color and normal data in the array.

 

Summing up, I have massive amounts of duplicate data (two redundant BufferedImages and 3 float arrays, plus the needed FloatBuffer) while I'm loading it to only retain the interleaved array and the FloatBuffer.

 

Am I missing something important here? The garbage collector should get rid of the redundant stuff since I don't reference it anymore but it still quite a jump of used memory to load a single asset. It probably can get out of hand loading many things.

Edited by TheChubu

Share this post


Link to post
Share on other sites

 I would re-write to just generate the final array. Load both the heightmap and colormap images, then generate your interleaved array, normals and all.

 

 Or, you're probably going to want to have access to certain data, like the heightmap and normals, for physics purposes (assuming you're going beyond simply rendering a terrain). In that case, organize your data so that the heightmap data is easily accessible, and then generate the interleaved array. At any rate, only generate the data you will need beyond load time. Minimize the redundant data. It won't really make a difference, but it will make your code more compact, which in larger projects becomes a good thing. Doing more work with less code is what separates the newbies from the gurus.

Share this post


Link to post
Share on other sites

Hm, I'll refactor the methods to work inside the interleaved array with an offset and a stride then. That ought to get rid of most of the redundant data. Otherwise I'll try to use TWL's PNG decoder instead of BufferedImages if it works better.

 

I'm not too worried about the code but the whole idea of (for example) using 2Gb of ram while loading assets only to keep half of that or less once the GC kicks in.

 

For now I get the heights of the map by working inside the interleaved array. There is a DataMeshTerrain class that has a "getHeightAt" method that does the work around the array indices of the mesh (among other things). I don't know if its the most "elegant" way but it works for now.

 

Anyway, I'll still have to refactor everything again if I decide to implement a half edge structure someday biggrin.png

 

EDIT: Talking about the array. Turns up that working directly with it (interleaved or not) is kinda a mess for calculating normals. I have to write lots of boilerplate code since each corner of the map is a special case and each side of the map is a special case. Ends up beign 4 loops, one for each side, 4 directly calculated normals, one for each corner, and a single loop for the rest of the interior vertices. Its pretty fast but its like 150 or 200 lines long.

 

For doing it in a more "elegant" way I need to store some kind of adjacency data per vertex right? (so it ends up in a more simple "for each adjacent vertex do this")

Edited by TheChubu

Share this post


Link to post
Share on other sites

This topic is 1763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this