# Convert flat terrain to spherical

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

## Recommended Posts

I've currently got a subset of Geo-Clipmapping working reasonably well with O'Neill's Atmospheric Scattering on both the skydome and the ground. However currently the terrain is simply a flat square grid, and I would like to change it so that it is spherical like the Earth is. It's not a full sphere but only a partial plane (i.e. 10000KMx10000KM) Is there an 'easy-ish' way to go about this? Doing this in the vertex shader would like right for the drawing side of things but everything else would need to be transformed likewise to match and it doesn't intuitively feel like it would work. Formski

##### Share on other sites
I did a simple hack that worked somewhat okay, although as one approaches the poles stuff gets wacky. Maybe that's why god invented the north/southpole? ;)

Write a function that generates the points of a sphere (simple sin/cos)

for each point:
Create a vector from current point to the sphere center, and make it a length of the corresponding height taken from the heightmap.

//end for

Draw the sphere.

Easy-peasey!

/Robert

[Edited by - Rasmadrak on September 17, 2007 5:08:08 AM]

##### Share on other sites
Expressing your (x, y) as spherical polar coordinates will generate a sphere, but be warned: the result will have singularities at the poles (I guess that's where the terminology came from in the first place). That is, coordinate space will no longer be in a linear correlation with phase space.

The extreme example is that, travelling at constant speed, it will take as long to encircle the world around the equator as it will to travel the 'zero' distance going east or west around a pole. Unless you do some appropriate scaling, things will tend to get very squashed as you move away from the equator. Your texture-map will also be heavily distorted.

Unfortunately, there is nothing you can do to entirely eliminate this problem, as the plane is inherently topologically incompatible with the sphere.

What exactly were you planning on doing with this sphere? If it's anything beyond the very simple, then you may need to rethink the problem, and start anew with an 'isosphere' (subdivided icosahedron).

##### Share on other sites
Thanks for the suggestions.

What I'm aiming for is a flight sim over a portion of the Earth's surface. I'm not looking at mapping the whole of the Earth's surface, only one of the interesting parts of it.

Reason it's come up is that I implemented Sean O'Neill's atmospheric scattering, and at a point the terrain is out of position for the equations and it starts to break.

Formski

##### Share on other sites
Assuming you have full access to the geometry and your only worry is the shape of the terrain this might help:
note: I'm assuming y is the up axis/height.

Get a position a long way below the terrain but that's xz coords are the center of it (probably something like (0.0f, -100000.0f, 0.0f)) I'll call it sphereCenter.

For each vertex in the terrain:
-Create a vector with the xz coords of the vertex and 0 for the the y (I'll call it zeroHeightLocation).
-Get the direction to zeroHeightLocation: normalize(zeroHeightLocation - sphereCenter) (I'll call that vertDirection)
-Scale vertDirection by (the y value of sphereCenter + the y value of the vertex) (I'll call that vertOffset)
-vertOffset + sphereCenter will be the new vertex position

One thing to note is that due to the large numbers that you may need to use the floating point inprecision may cause slight distortions of the geometry but since you say this is for a flight sim it shouldn't be an issue as it wouldn't be noticable from a plane (even flying relatively low to the ground).

##### Share on other sites
The problem with creating the patch and _then_ deforming it to fit the sphere is that (assuming you are using different texturing for the patch and the underlying planet surface) the texturing will not match.
In my planet renderer (which also used the O'Neill AtScat!) I created the patch on the surface of the planet itself. The initial patch this gives you is good, but as soon as an update occurs along one of the edges it starts to distort. This is fine as long as your patch texture creation algorithm is designed to take the positional information from the patch in relation to the planet surface, and not to calculate it procedurally. The next problem you run into is the adding of more detailed patches (i.e. higher LODs). To get this to work with the distorted initial patch just means that the positions of the new vertices to be inserted should be calculated from the previous patch rather than procedurally generated from the algorithm.
The benefits of doing things this way is that the patch can happily move anywhere on the planets surface. I never got as far as stitching the patch onto the planet surface, but its not really a big deal as the patch is so huge you can only see the edge of it from space!
The source code for my engine is still up here: www.starfiregame.com
It isn't the most up to date code (from before I added AtScat), its pretty unstable aswell, but it works!
There are, I would imagine, better ways of doing this, and if you know them please share!

1. 1
Rutin
48
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011717
• ### Who's Online (See full list)

There are no registered users currently online

×