Loading...
[-]

Bing Maps 3D – Clipping shapes with an imposter Earth

The final source code for these Bing Maps 3D posts can be found here 3DViewerBingMaps.zip. You will also need to install Bing Maps 3D from Microsoft.

This is the 3rd part of my posts on Bing Maps 3D. The 3 parts are:

- Adding Arbitrary Shapes. Creating a Cube mesh and a Sphere mesh.
- Rendering shapes past the Far Clip. How to render shapes whose coordinates are outside the View Port.
- Clipping shapes with an imposter Earth.

In the previous Bing Maps 3D post we had overcome the problem of 3D objects being clipped by the far plane on the horizon, but now we had the problem that they weren’t being clipped by the earth as seen for the yellow cube in the picture below:

Untitled7

The solution to this is to create another sphere to imitate the earth, but smaller and closer to the camera. The sphere should be scaled down by the same factor that the cube is scaled down when it is brought closer to the camera. The sphere rendered for the Earth in Bing Maps 3D isn’t an exact sphere, it is a little bit squashed, so to make sure we get the same shape we measure the earth from within Bing Maps 3D and apply those measurements to our sphere. Here’s where we use the SetToReplaceEarth() method that I included in the Sphere Mesh code in the first post which looks like this:

/// <summary>
/// Scales and positions this sphere such that it replaces the earth
/// </summary>
public void SetToReplaceEarth() {
  Microsoft.MapPoint.Rendering3D.Cameras.GeodeticViewpoint view = new Microsoft.MapPoint.Rendering3D.Cameras.GeodeticViewpoint();
  Vector3D[] vecs = new Vector3D[6];
  view.Position.Location = LatLonAlt.CreateUsingDegrees(0, 0, 0);
  vecs[0] = view.Position.Vector;

  view.Position.Location = LatLonAlt.CreateUsingDegrees(0,180, 0);
  vecs[1] = view.Position.Vector;

  view.Position.Location = LatLonAlt.CreateUsingDegrees(-90, 0, 0);
  vecs[2] = view.Position.Vector;

  view.Position.Location = LatLonAlt.CreateUsingDegrees(90, 0, 0);
  vecs[3] = view.Position.Vector;

  view.Position.Location = LatLonAlt.CreateUsingDegrees(0, 90, 0);
  vecs[4] = view.Position.Vector;

  view.Position.Location = LatLonAlt.CreateUsingDegrees(0, -90, 0);
  vecs[5] = view.Position.Vector;

  Vector3D average = new Vector3D(0, 0, 0);
  Vector3D min = new Vector3D(double.MaxValue, double.MaxValue, double.MaxValue);
  Vector3D max = new Vector3D(double.MinValue, double.MinValue, double.MinValue);

  for (int i = 0; i < vecs.Length; ++i) {
    average.Add(vecs[i]);
    min = min.Min(vecs[i]);
    max = max.Max(vecs[i]);
  }
  average.MultiplyBy(1.0 / 6.0);

  this.Position = average;

  Vector3D diff = max.Subtract(min);
  diff.MultiplyBy(0.5 / radius);
  this.Scale = diff;

}

We put this sphere into the scene at 0,0,0, and to make it visible I will give it a very transparent yellow color. Here’s the result:

Untitled9

So it’s working now right? Well not quite, you see I’ve drawn the sphere above to have 20 stacks, 20 slices, so it’s not a very high resolution mesh, so when you get lower the clip is quite a bit under the surface. In the image below the yellow line should stop at the same place as the red cube does:

Untitled10

Easy fix right? Lets change the imitation Earth to a 200×200 mesh:

Untitled8

What happened? Well the old DirectX cards could only handle a 16 bit Index Buffer , which is what we set up our sphere mesh as when we declared it like this:

public class SphereMesh : MeshGraphicsObject<Vertex.PositionNormal, ushort>{
}

Fortunately for me my video card can handle a 32 bit index buffer, so I change the code throughout to have uint instead of ushort and now it works fine and the error in the clip at low altitude is improved:

Untitled11

This wraps up my 3 part series on adding 3D Meshes to Bing Maps 3D. Enjoy playing with the sample application attached at the top of all 3 articles.

One Trackback

  1. [...] – Adding Arbitrary Shapes. Creating a Cube mesh and a Sphere mesh. – Rendering shapes past the Far Clip. How to render shapes whose coordinates are outside the View Port. – Clipping shapes with an imposter Earth. [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*