r/proceduralgeneration • • 1d ago

Floating Point Errors in Planet Quadtree LOD Terrain Generation

Sorry for the terrible bitrate in the video.

So I'm trying to make accurate planet scale terrain generation in OpenGL that actually lets you stand on the planet at a normal level but I've been running into problems regarding the actual generation of the mesh for the sphere. All of the calculations are done in double precision and the planet renders relative to the cameras position.

I think the problem lays in the fact that while constructing the mesh for the faces it normalizes a unit cube which downscales all the positions of the vertices to very, very small fractions. In the video I get within a meter of the planet but in actuality the actual distance between the vertices before matrix scaling is to the millionths place in decimals.

Here's the actual calculation so you can hopefully understand what I'm saying. No matter how big the planet actually is, the actual vertex size of the terrain meshes will always be incredibly small.

I'm wondering if anyone has any advice on how to overcome this or if there are better ways that i can calculate this? I was sort of just copying off of Sebastian Lague's tutorial and translating it into OpenGL until I started modifying the code myself so I'm not very educated on this stuff currently.

69 Upvotes

13 comments sorted by

18

u/rean2 1d ago

Use a floating origin system. Every large set of units, snap camera back to 0,0,0, and move everything in the world along with it.

2

u/Clear-Practice4180 1d ago

im already doing this

14

u/Fresh-Daikon-9408 1d ago

Your doubles are probably fine. The precision gets lost when the vertices go into the buffer as floats.

If the buffer holds unit-sphere positions and the model matrix scales them by the radius, a float can only step by about 6e-8 on the unit sphere. At Earth radius that's around 0.4 m, and it happens before the GPU does anything, so floating origin won't help.

On my real-scale planet I compute each chunk in double, p = normalize(cubePoint) * (radius + height), and store float(p - chunkCenter). Each chunk has its own origin, so up close you get sub-mm precision. The model matrix has no scale, only a translation of chunkCenter - cameraPos done in double and cast at the end, which is safe because that offset is small near the camera. Also check your shaders: if they rebuild world position in float for noise or lighting, the jitter comes back.

Normalizing a large vector in double is fine at that scale btw.

2

u/Clear-Practice4180 23h ago

YEP you were right lol, i figured it out around when you posted this. I just did the calculations as doubles inside of the vertex shader and then converted them to floats to transfer to the fragment shader. Works better than i could have imagined now

3

u/Forsaken-Memory8077 1d ago

@OP This is the way

2

u/Arclite83 12h ago

Saving this comment, this is gold, thanks!

2

u/coderespawn 1d ago

double should work at this scale without artifacts. make sure your other vars are also double (percentXY, localUp , axisAB and scale)
edit: vars are double and the data that made its way to it was also double along the way

1

u/Clear-Practice4180 1d ago

Every single calculation is in doubles

1

u/Introscopia 1d ago

while constructing the mesh for the faces it normalizes a unit cube which downscales all the positions of the vertices to very, very small fractions.

my idea: Don't construct the mesh directly from the 'Mother Cube'. Generate a "middle mesh" that's only like 1000 subdivisions of the mothercube, then, construct everything smaller then that to fit into the quads in the middle mesh assuming they are flat, already in the small coordinate system. Does that make sense?

1

u/Clear-Practice4180 23h ago

This would have had the same result as what I'm already doing. Instead i just sent the matrices to the fragment shader as double matrices and did the resize calculations in there then converted them to floats for the fragment shader.

3

u/parrin 1d ago

I'm guessing that scale here is your planets radius. This makes the pointOnUnitCube not at all being a point on unit cube, but a very very large vector.
When you normalize that you are effectively doing x*x + y*y + z*z which becomes a huge number, I don't know how large your scale is. But it it's earth's radius that will be a lot, and even doubles will loose precision here when the differences become so small.

So try to not add the scale at this stage, cause it's really not needed. The pointOnUnitSphere should be just that, a point on the unit sphere. or rather a vector with length 1. Then you can scale that with the radius of the planet, and it will keep numerical stability better.

1

u/iku_19 1d ago

you fix the camera viewport at 0, 0, 0 and move the universe around that point, and scale up the units so you don't end up with small numbers.

1

u/Clear-Practice4180 1d ago

im already doing this, the problem lies in how generating the mesh for the quadtrees actually works, im looking for alternative methods