[SOLVED] [Math/Geometry/Mesh-Generation] Planetary Surface

Hello everyone!

I need a little bit of help with something.
Problem is solved. Look at the bottom of this post.


The Problem:
I am working on a 3D Planetary-Engine in Java (yes, you read right, ‘3D Planets in Java’), and I am stuck at correctly generating the surface of the planet, or to be more exact, the Mesh of the Terrain-Patches the surface consists of.


What I am doing right now:
Right now, I am using the CubeToSphere system in combination with a Quad-Tree.
That is the part that works like a charm. I cna create new node’s in the QuadTree, and remove them if no longer needed.
Dynamically, at runtime, and it could happen at ANY moment in time as the ‘camera’ (Usually the Player) moves around.

The problem I am having is to give each Node in the QuadTree a section of Terrain.
These Terrain ‘Patches’ have a size of 32x32 3D-Points, which get later transformed onto the surface of the planetary sphere, including a height-offset from a height-map which is also procedurally generated using a simple fractal algorythm (a weird mix of Diamond-Square and Simplex-Noise).

So far so good, but how do I go about actually generating the points in between the 4 given edge-points which make up the borders of the Node?
I basically have to generate a Mesh of Points in between 4 arbitrary points in 3D-Space.

And:
I have no idea how to do this, as I am somehow having problems thinking up any good way to do it, nor do I seem to find any good resources on doing this per Google.
If you happen to know any good resources on this: Link(s) please!

Additional Notes:

  • If needed, I can add a few more variables to the QuadTree-Node’s, if that helps.
  • I don’t to create any objects except Points in the code (for obvious reasons -> Camera + Arbitrary Movement + Dynamic QuadTree = Memory Garbage en’ masse).
  • I would like to NOT use recursion in the generation (if there is no other way, I will use recursion in the code).
  • I CAN and I AM already using Multi-Threading in the application. But concurrency should not be that big of a problem.

[s]That should be enough of a description.

Help please?[/s]

The solution is as follows.

Code Info:

  • I didn’t optimize anything.
  • I didn’t use any external libraries.
  • Result gets stored in ‘vertices’ in the order X,Y,Z,X,Y,Z, …
  • Hardcoded to generate a field of 32x32 points.
  • Its VERY fast just like this already.

The Code to do the thing I wan’t:



		float[] A = new float[]{ -2.0f, +1.75f, 0.0f };
		float[] B = new float[]{ +1.0f, +1.0f, 0.0f };
		float[] C = new float[]{ +1.5f, -1.25f, 0.0f };
		float[] D = new float[]{ -1.0f, -1.5f, 0.0f };
		
		
		float[] vertices = new float[32 * 32 * 3];
		int shift = 32 * 31;
		
		// PASS ONE, Top and Bottom Rows!
		float lerp, div;
		for(int i = 0, io = 0, io2 = 0; i < 32; i++)
		{
			div = i;
			io = i*3;
			io2 = (i+shift)*3;
			lerp = div / 31f;
			
			// A[0], B[0]
			vertices[io++] = A[0] + (lerp * (B[0] - A[0]));
			vertices[io++] = A[1] + (lerp * (B[1] - A[1]));
			vertices[io] = A[2] + (lerp * (B[2] - A[2]));
			
			vertices[io2++] = D[0] + (lerp * (C[0] - D[0]));
			vertices[io2++] = D[1] + (lerp * (C[1] - D[1]));
			vertices[io2] = D[2] + (lerp * (C[2] - D[2]));
		}
		
		// PASS TWO, All dem rest!
		for(int ix = 0, ixo, ixo2; ix < 32; ix++)
		{
			
			// Get the Top and Bottom points.
			ixo = ix * 3;
			ixo2 = (ix+shift) * 3;
			float tx = vertices[ixo++];
			float ty = vertices[ixo++];
			float tz = vertices[ixo];
			
			float bx = vertices[ixo2++];
			float by = vertices[ixo2++];
			float bz = vertices[ixo2];
			
			for(int iy = 1, index; iy <= 31; iy++)
			{
				index = (ix + (iy*32)) * 3;
				div = iy;
				lerp = div / 31f;
				
				vertices[index++] = tx + (lerp * (bx - tx));
				vertices[index++] = ty + (lerp * (by - ty));
				vertices[index] = tz + (lerp * (bz - tz));
			}
		}

Thanks for the help.

  • Longor1996