Download Source for Visual Studio 2012
WPF lacks built-in 3D shapes such as sphere, cylinder and cube. In result, those who intend to add such shapes to their 3D scene have no choice rather than generating their mesh through additional coding. As I've already googled and searched through CodeProject for a simple solution for generating a sphere in WPF, I didn't find a clear code that I could rely on for a serious project. Some of them too long others with limitations and ambiguity. The code that I submit here generates a triangle list and their normal vectors for a sphere with unit radius which can be then moved and resized by simple calculations. It’s written in VB.Net and can be easily converted to other .Net based languages.
Using the code
For better manipulation of
Trianlges and their
Normals, the following structure is developed.
Public Structure Triangle
Dim Point1 As Point3D
Dim Point2 As Point3D
Dim Point3 As Point3D
Dim Normal As Vector3D
The points in space should be located in such a way that they all have equal distance from center of sphere and also the enumerator of such points must be able to keep a record of neighboring points. This can be accomplished by converting the Cartesian coordinate to Spherical.
The following shows how the parameters r, θ and ɸ replace the Cartesian X, Y and Z.
As far as r is supposed to be unit it can be omitted. Figure below shows how the enumerator passes through points on sphere surface. It’s shown that ɸ enumerator can be used to find adjacent points on certain Z surface while θ enumerator iterates through all Z surfaces to generate the sphere surface points.
A closer look onto the sphere surface reveals how the triangles can be generated using the algorithm. As it’s shown each θ iteration represents a point which may be common in 8 triangles. In order to prevent generating additional triangles, the algorithm locates two triangles on ɸ iteration forward and θ level backward as shown below.
Now it's time to write the code.
Generating Sphere Points
function returns an
ArrayList that contains
Triangles. In this function, At first, the following code generates the points on surface of sphere and adds them to
PointList()() Array. The
Density parameters determines the number of ɸ for sphere.
For tita As Integer = 0 To Density
Dim vtita As Double = tita * (Math.PI / Density)
For nphi As Integer = -Density To Density
Dim vphi As Double = nphi * (Math.PI / Density)
PointList(tita)(nphi + Density).X = Math.Sin(vtita) * Math.Cos(vphi)
PointList(tita)(nphi + Density).Y = Math.Sin(vtita) * Math.Sin(vphi)
PointList(tita)(nphi + Density).Z = Math.Cos(vtita)
Then, iteration below connects the point lists according to described algorithm.
Triangle2 are respective triangles explained and illustrated already. As far as sphere is located in origin the
Normal vectors can be suggested easily as the location of points on sphere surface.
Dim TriangleList As New ArrayList
For n_tita As Integer = 1 To PointList.GetLength(0) - 1
For n_phi As Integer = 0 To PointList(n_tita).GetLength(0) - 2
Dim Triangle1, Triangle2 As Triangle
Triangle1.Point1 = PointList(n_tita)(n_phi)
Triangle1.Point2 = PointList(n_tita)(n_phi + 1)
Triangle1.Point3 = PointList(n_tita - 1)(n_phi)
Triangle1.Normal = New Vector3D(Triangle1.Point1.X, Triangle1.Point1.Y, Triangle1.Point1.Z)
Triangle2.Point1 = PointList(n_tita)(n_phi + 1)
Triangle2.Point2 = PointList(n_tita - 1)(n_phi + 1)
Triangle2.Point3 = PointList(n_tita - 1)(n_phi)
Triangle2.Normal = New Vector3D(Triangle1.Point1.X, Triangle1.Point1.Y, Triangle1.Point1.Z)
1) In order to enhance the application performance, the triangle list can be saved on hard drive and then
loaded in memory instead of calling the CreateTrianlgeListForSphere function each time a sphere is needed.
2) For bigger projects it’d better to use triangle strips rather than an
. That will reduce use of memory, CPU and GPU significantly.
3) Two loops above can be easily merged for best performance.
Adding a custom sphere requires calling CreateTriangleListForSphere and then resize and move the
Normal vector remains intact.