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
// ===================================================================================
//  Copyright (c) Microsoft Corporation.  All rights reserved.
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
//  OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
//  LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//  FITNESS FOR A PARTICULAR PURPOSE.
// ===================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace SolarWind
{
    public class Camera
    {
        public Matrix ViewTransform { get; set; } // view space transform

        Matrix projection = Matrix.Identity; // projection transform to convert 3D space to 2D screen space

        Vector3 cameraPosition; // the camera's position
        Vector3 cameraTarget; // the place the camera is looking
        Matrix orbitRotation; // the rotation around world origin
        
        public Camera()
        {
            cameraPosition = new Vector3(0, 0, 15.0f);
            orbitRotation = Matrix.Identity; // no rotation
            cameraTarget = Vector3.Zero; // world origin

            // initialize view transform
            ViewTransform = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
        }

        /// <summary>
        /// Aspect ratio of the viewport for this camera
        /// </summary>
        public float AspectRatio
        {
            set
            {
                // update the screen space transform every time the aspect ratio changes
                projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, value, 1.0f, 100.0f);
            }
        }

        /// <summary>
        /// Projection transform to convert 3D space to 2D screen space
        /// </summary>
        public Matrix ProjectionTransform
        {
            get
            {
                return projection;
            }
        }

        /// <summary>
        /// Position in world coordinates
        /// </summary>
        public Vector3 Position
        {
            set
            {
                cameraPosition = value;
                UpdateView();
            }
            get
            {
                return cameraPosition;
            }
        }

        /// <summary>
        /// Rotation around the world origin (orbit)
        /// </summary>
        public Matrix OrbitRotation
        {
            set
            {
                orbitRotation = value;
                UpdateView();
            }
            get
            {
                return orbitRotation;
            }
        }

        private void UpdateView()
        {
            Matrix world = Matrix.CreateTranslation(cameraPosition) * orbitRotation;
            ViewTransform = Matrix.CreateLookAt(world.Translation, cameraTarget, Vector3.Up);
        }
    }
}