Jump to content

  • Log In with Google      Sign In   
  • Create Account


How is this usually done?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 TheChubu   Crossbones+   -  Reputation: 3952

Like
0Likes
Like

Posted 19 March 2013 - 05:20 AM

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, 19 March 2013 - 08:31 AM.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


Sponsor:

#2 radioteeth   Prime Members   -  Reputation: 957

Like
1Likes
Like

Posted 19 March 2013 - 10:22 AM

 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.



#3 TheChubu   Crossbones+   -  Reputation: 3952

Like
0Likes
Like

Posted 20 March 2013 - 07:13 AM

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, 20 March 2013 - 07:19 AM.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS