Click here to Skip to main content
15,880,972 members
Articles / Desktop Programming / WPF

Triangle List Generator for Sphere

Rate me:
Please Sign up or sign in to vote.
4.61/5 (15 votes)
20 Dec 2013CPOL2 min read 27.4K   457   15   7
This article demonstrates a simple and fast method for generating a list of regular triangles that form a unit sphere surface
..

Download Source for Visual Studio 2012

Image 1 

Introduction  

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. 

Image 2

Using the code 

Triangle Structure 

For better manipulation of Trianlges and their Normals, the following structure is developed.

VB.NET
Public Structure Triangle
   Dim Point1 As Point3D
   Dim Point2 As Point3D
   Dim Point3 As Point3D
   Dim Normal As Vector3D
End Structure

Algorithm  

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.   

 Image 3 

The following shows how the parameters r, θ and ɸ replace the Cartesian X, Y and Z.   

Image 4

Image 5

Image 6

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. 

Image 7

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.  

Image 8

Now it's time to write the code.  

Generating Sphere Points  

The CreateTriangleListForSphere 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. 

VB.NET
    
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)
            Next
Next

Generating Triangles 

Then, iteration below connects the point lists according to described algorithm. Triangle1 and 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)


               TriangleList.Add(Triangle1)
               TriangleList.Add(Triangle2)


           Next
       Next

Performance 

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.<o:p>

2) For bigger projects it’d better to use triangle strips rather than an ArrayList of T<code>riangles. That will reduce use of memory, CPU and GPU significantly.

3) Two loops above can be easily merged for best performance.   

<o:p>

Application 

Adding a custom sphere requires calling CreateTriangleListForSphere and then resize and move the Triangle points: Point1, Point2 and Point3. The Normal vector remains intact. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
United Kingdom United Kingdom
I'm a Full-stack Software Developer with over a decade of experience at industrial and research projects having my main focus on Microsoft .Net stack.

Comments and Discussions

 
QuestionThanks Pin
Member 1124602820-Nov-14 4:00
Member 1124602820-Nov-14 4:00 
AnswerRe: Thanks Pin
Taylor Kobani11-Jan-15 23:55
Taylor Kobani11-Jan-15 23:55 
GeneralInteresting Pin
Meshack Musundi4-Jan-14 4:31
professionalMeshack Musundi4-Jan-14 4:31 
QuestionHexagon Pin
bling23-Dec-13 7:35
bling23-Dec-13 7:35 
AnswerRe: Hexagon Pin
The_Inventor23-Dec-13 16:05
The_Inventor23-Dec-13 16:05 
GeneralRe: Hexagon Pin
bling24-Dec-13 5:09
bling24-Dec-13 5:09 
GeneralRe: Hexagon Pin
The_Inventor24-Dec-13 13:53
The_Inventor24-Dec-13 13:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.