Home > xna > Generating spherical world from heightmapped terrain

# Generating spherical world from heightmapped terrain

December 18Hits:10

I am using a standard heightmapped procedural terrain in my project. However, I want the terrain to appear spherical when the user zooms out. This is an attempt to simulate the appearance of a spherical planet. Here is my current algorithm:

````        //get the direction from the "planet" center to this vert         Vector3 sphereCentertoVertPosition = vert.Position - SphereCenter;         sphereCentertoVertPosition.Normalize();          //our rotation axis is the cross between the direction from this vert to the planet center and the root (center of the terrain) to the planet center         RotationAxis = Vector3.Cross(sphereCentertoVertPosition, sphereCenterToRootPosition);          //the amount we rotate is based on the distance of this vert from the center of the terrain         Vector3 fromCenter = vert.Position - Root.Position;         float amount = (fromCenter.Length() / ((myTextureWidth / Scale) / 2)) * (float)Math.PI;          Quaternion rot = Quaternion.CreateFromAxisAngle(RotationAxis, amount);          Vector3.Transform(ref vert.Position, ref rot, out vert.Position); `
```

My main concern is that the rotation axis is not correct. Theoretically, should it be the cross between the vert-to-planet-center and the terrain-center-to-planet-center?

Hmm, I'm not so sure about rotating the vertices. Perhaps this is not the answer you're looking for, but I can suggest an alternate method.

I've done something similar with maps in my game. I wrote a shader to wrap terrain into a sphere, but it can easily be done outside the graphics card too. It's as simple as mapping the Cartesian coordinates to spherical coordinates. The code would look something like:

``````public static Vector3f MapToSphere(Vector3f coords, WorldMap MAP) {
float pi = 3.14159265f;
float thetaDelta = ((2 * pi) / (MAP.XSize - 1));
float phiDelta = ((pi) / (MAP.ZSize - .5f));
float RadiusBase = ((MAP.XSize) / pi / 2f);
float theta = (coords.z * thetaDelta);
float phi = (coords.x * phiDelta);

//Limit the map to half a sphere
if(theta > pi) {theta = theta - (pi);}

if(theta < 0.0)    {theta = theta + (pi);}

if (phi > 2*pi) {phi = phi - (2*pi);}

if (phi < 0.0) {phi = phi + (2*pi);}

Vector3f coords2 = new Vector3f();
coords2.x = (float) (((RadiusBase) * Math.sin(theta) * Math.cos(phi)) + MAP.XSize / 2f);
coords2.y = (float) ((RadiusBase) * Math.sin(theta) * Math.sin(phi));
coords2.z = (float) (((RadiusBase) * Math.cos(theta)) + MAP.ZSize / 2f);
return coords2;
}
```
```

This maybe "too spherical" for you. If so, you can try padding the edges of your map so that the portion turned into a sphere only represents a portion of the surface of the sphere.

Below is an example of the spherical mapping results.

A "flat" map:

And a spherical world, created by wrapping the vertices into a sphere:

Note that there will be a large amount of distortion at the poles if you don't follow the suggestion above to limit the surface area of the sphere this map is stretched over.

## Related Articles

• ### Generating spherical world from heightmapped terrainDecember 18

I am using a standard heightmapped procedural terrain in my project. However, I want the terrain to appear spherical when the user zooms out. This is an attempt to simulate the appearance of a spherical planet. Here is my current algorithm: //get the

• ### Procedurally generate rivers for 2-d heightmap terrainOctober 22

I created an algorithm to procedurally generate 2d worlds based on a heightmap grid. (I used a 2d simplex noise algorithm mapped on the inverse of the distance of each pixel from the center, to "center" the island and keep it from running off th

• ### Dealing with edges on spherical heightmapped terrainDecember 30

This is the problem I'm having: where the height has been applied to the vertex, the edge of each face has been pushed apart. I was wondering how I can best deal with this issue? I read elsewhere that calculating the mean between the two adjacent edg

• ### Removing seams between procedurally-generated spherical terrain tilesNovember 30

I'm working on procedural planet generation project in Unity. To create a sphere, I use the following technique: Create six planes arranged as faces of a cube Normalize all vertices' positions to create a unit sphere Multiply each vertices' positions

• ### Voxel heightmap terrain editorMay 13

I've recently been experimenting with a simple Voxel-based 3d engine (think Minecraft) which uses heightmaps to define terrain in the following format: http://en.wikipedia.org/wiki/Heightmap Does anyone know of the best software for producing these k

• ### Heightmap terrain pickingJanuary 17

I've implemented an OpenGL based terrain unsing a tesselation-shader for dividing each 'terrain-cell' into the desired tiles. The heightmap is uploaded to the GPU and applied on the shader. When it comes to picking, I don't really know what to do. Te

• ### Generate texture for a heightmapApril 20

I've recently been trying to blend multiple textures based on the height at different points in a heightmap. However i've been getting poor results. I decided to backtrack and just attempt to recreate one single texture from an SDL_Surface (i'm using

• ### Spherical procedural terrain shader based on slopeJanuary 3

I've created a spherical terrain object out of 6 sphere projected (normalised) planes, each plane has been heightmapped post to being normalised. I'm looking to create a CG shader which will firstly calculate the slope of the heightmapped terrain, I

• ### Generate planet like heightmapsDecember 13

I would like to procedurally generate planets, but I don't know how to wrap a heightmap around a sphere. I already know how to generate plane heightmaps, but not how to generate spherical ones. Can someone push me in the right direction? ------------

• ### Sending changes to a terrain heightmap over UDPDecember 28

This is a more conceptual, thinking-out-loud question than a technical one. I have a 3D heightmapped terrain as part of a multiplayer RTS that I would like to terraform over a network. The terraforming will be done by units within the gameworld; the

• ### Heightmap, Voxel, Polygon (geometry) terrainsAugust 4

In relation to Heightmap, Voxel and Polygon (geometry) terrains: What are the main differences between all these three? Can you form a "smooth" terrain with Voxels, I mean, can you for example get a smooth mountain with Voxels, or Voxels are lim

• ### How is the terrain generated in Commandos and Commandos game clones/look-alikes?September 2

The Commandos series of games and its similar western counterpart, Desperados, use a mix of 2D and 3D elements to achieve a very pleasing and immersive atmosphere. Apart from the concept that alone made the series a best-seller, the graphics eye-cand

• ### Generating realistic terrain data - Part 2May 9

I've made a "heightmap" terrain generator similar to my previous one, except this one has some improvements, and some new features. The way this one works is similar to my last one, but slightly different, so I'll run through it again. First, a

• ### Correct way to "randomly" generate flowing terrainMarch 6

I'm creating a simple top down RTS game. I plan on it "randomly" generating maps on the fly when I need to. I plan on it all working in 'passes': Fill the terrain in with all grass Go back and add some random spurts of gravel Round out the grave

• ### Procedural Planets, Heightmaps and TexturesNovember 2

I am currently working on an OpenGL procedural planet generator. I hope to use it for a space RPG, that will not allow players to go down to the surface of a planet so I have ignored anything ROAM related. At the moment I am drawing a cube with VBOs

• ### Realistic planetary terrain generation with weightsJune 20

I need terrain generation for a planet. The planet will be divided up into several hundred hexes, and I need it to be realistic and based on weights. I have dabbled in terrain generation before, but nothing like this. So I figure it would be a good i

• ### Tips for density terrain function (using Marching cube) October 19

Possible Duplicate: How are voxel terrain engines made? I'm making a game like minecraft (although a different idea) but I need a random world generator for a 1024 block wide and 256 block tall map. Basically so far I have a multidimensional array fo

• ### What is "terrain mesh generation" exactly?September 16

I'm new to computer graphics and need to know and understand what the "terrain mesh generation" is? I read about "terrain", "mesh", "terrain generation" but can't find an article named "terrain mesh generation&

• ### Simplex noise heightmapJune 19

I'm currently working on a game with an "infinite" programmatically generated terrain. The terrain is being generated by creating a heightmap using simplex noise. I've followed a tutorial and ended up with the following code (it's vb.net by the

• ### Are there open access terrain tiles with a permissive license like OSM?March 29

Are there open access terrain pyramidal map tiles with a permissive usage license like OpenStreetMap's? I'm building aviation-related maps with Polymaps and need a good raster basemap. The Google terrain tiles are just perfect for my purposes: restra