Solar Wind 3D Sample

This sample for Silverilght 5 uses the new 3D features to draw the Earth with day and night transitions, atmosphere layers, and population density overlays. It demonstrates advanced concepts like mipmaps, texture blending, multiple drawing passes, sampler states...

C# (10.4 MB)
 
 
 
 
 
4.4 Star
(8)
9,127 times
Add to favorites
9/12/2011
E-mail Twitter del.icio.us Digg Facebook
#region File Description
//-----------------------------------------------------------------------------
// SpherePrimitive.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Graphics;

#endregion

namespace Primitives3D
{
    /// <summary>
    /// Geometric primitive class for drawing spheres.
    /// </summary>
    public class SpherePrimitive : GeometricPrimitive
    {
        /// <summary>
        /// Constructs a new sphere primitive, using default settings.
        /// </summary>
        public SpherePrimitive()
            : this(1, 16)
        {
        }

        Vector3 normal = new Vector3();
        Vector2 uv = new Vector2();

        /// <summary>
        /// Constructs a new sphere primitive,
        /// with the specified size and tessellation level.
        /// </summary>
        public SpherePrimitive(float diameter, int tessellation)
        {
            if (tessellation < 3)
                throw new ArgumentOutOfRangeException("tessellation");

            float radius = diameter / 2;

            int latitudeBands = tessellation;
            int longitudeBands = tessellation * 2;

            for (int latNumber = 0; latNumber <= latitudeBands; latNumber++)
            {
                float theta = latNumber * (float)Math.PI / latitudeBands;
                float sinTheta = (float)Math.Sin(theta);
                float cosTheta = (float)Math.Cos(theta);

                for (int longNumber = 0; longNumber <= longitudeBands; longNumber++)
                {
                    float phi = longNumber * 2.0f * (float)Math.PI / longitudeBands;
                    float sinPhi = (float)Math.Sin(phi);
                    float cosPhi = (float)Math.Cos(phi);

                    normal.X = cosPhi * sinTheta;
                    normal.Y = cosTheta;
                    normal.Z = sinPhi * sinTheta;
                    uv.X = 1.0f - ((float)longNumber / (float)longitudeBands);
                    uv.Y = ((float)latNumber / (float)latitudeBands);
                    
                    AddVertex(normal * radius, normal, uv);
                }
            }

            for (int latNumber = 0; latNumber < latitudeBands; latNumber++)
            {
                for (int longNumber = 0; longNumber < longitudeBands; longNumber++)
                {
                    int first = (latNumber * (longitudeBands + 1)) + longNumber;
                    int second = first + longitudeBands + 1;
                    
                    AddIndex(first);
                    AddIndex(second);
                    AddIndex(first + 1);

                    AddIndex(second);
                    AddIndex(second + 1);
                    AddIndex(first + 1);
                }
            }

            InitializePrimitive(GraphicsDeviceManager.Current.GraphicsDevice);
        }
    }
}