#region File Description
//-----------------------------------------------------------------------------
// ModelViewerControl.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion
namespace WinFormsContentLoading
{
///
/// Example control inherits from GraphicsDeviceControl, and displays
/// a spinning 3D model. The main form class is responsible for loading
/// the model: this control just displays it.
///
public class ModelViewerControl : GraphicsDeviceControl
{
// Timer controls the rotation speed.
Stopwatch timer;
///
/// Initializes the control.
///
protected override void Initialize()
{
// Start the animation timer.
timer = Stopwatch.StartNew();
// Hook the idle event to constantly redraw our animation.
Application.Idle += delegate { Invalidate(); };
}
///
/// Draws the control.
///
protected override void Draw()
{
// Clear to the default control background color.
Color backColor = new Color(BackColor.R, BackColor.G, BackColor.B);
GraphicsDevice.Clear(backColor);
for (int x = 0; x < Globals.penStylesList.Count; x++)
{
if (Globals.penStylesList[x].nameID == Globals.thisPen.ToString())
{
// Compute camera matrices.
float rotation = (float)timer.Elapsed.TotalSeconds;
Vector3 eyePosition = Globals.penStylesList[x].modelCenter;
eyePosition.Z += Globals.penStylesList[x].modelRadius * 2;
eyePosition.Y += Globals.penStylesList[x].modelRadius;
float aspectRatio = GraphicsDevice.Viewport.AspectRatio;
float nearClip = Globals.penStylesList[x].modelRadius / 100;
float farClip = Globals.penStylesList[x].modelRadius * 100;
Matrix world = Matrix.CreateRotationY(rotation);
Matrix view = Matrix.CreateLookAt(eyePosition, Globals.penStylesList[x].modelCenter, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, aspectRatio,
nearClip, farClip);
// Draw the model.
foreach (ModelMesh mesh in Globals.penStylesList[x].Model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
// effect.EnableDefaultLighting();
effect.TextureEnabled = true;
effect.Texture = Globals.penStylesList[x].ModelTexture;
effect.World = Globals.penStylesList[x].boneTransforms[mesh.ParentBone.Index] * world;
effect.View = view;
effect.Projection = projection;
// effect.PreferPerPixelLighting = true;
// effect.SpecularPower = 16;
}
mesh.Draw();
}
}
}
}
}
}