```
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Bvl = Bevelstone.FlightSimulator;
using System.Windows.Media.Media3D;
namespace SampleProjectExplorer.Controls
{
/// <summary>
/// Interaction logic for FSModelPreviewControl.xaml
/// </summary>
public partial class FSModelPreviewControl : System.Windows.Controls.UserControl
{
public FSModelPreviewControl()
{
InitializeComponent();
this.MouseDown += new MouseButtonEventHandler(OnMouseDown);
this.MouseMove += new MouseEventHandler(OnMouseMove);
this.MouseUp += new MouseButtonEventHandler(OnMouseUp);
viewport.Children.Add(model);
Transform3DGroup transformGroup = new Transform3DGroup();
transformGroup.Children.Add(new RotateTransform3D(rotateVertical));
transformGroup.Children.Add(new RotateTransform3D(rotateHorizontal));
model.Transform = transformGroup;
}
ModelVisual3D model = new ModelVisual3D();
AxisAngleRotation3D rotateHorizontal = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 45);
AxisAngleRotation3D rotateVertical = new AxisAngleRotation3D(new Vector3D(1, 0, 0), 45);
Bvl.FlightSimModel fsModel = null;
public Bvl.FlightSimModel FSModel
{
get
{
return fsModel;
}
set
{
model.Children.Clear();
fsModel = value;
if (fsModel != null)
{
foreach (Bvl.TriangleSet ts in fsModel.LevelsOfDetail[0].TriangleSets)
{
MeshGeometry3D triangleMesh = new MeshGeometry3D();
foreach (Bvl.Triangle t in ts)
{
// Should reuse vertexes here - oh well, can't have it all in the first attempt :)
int index = triangleMesh.Positions.Count;
for (int i = 2; i >= 0; i--) // Need them in reverse order of what FS use... or maybe I just swap them when writing the BGL file :)
{
Bvl.Vertex v = t.Vertexes[i];
triangleMesh.Positions.Add(new Point3D(v.X, v.Y, v.Z));
triangleMesh.Normals.Add(new Vector3D(v.Nx, v.Ny, v.Nz));
triangleMesh.TriangleIndices.Add(triangleMesh.TriangleIndices.Count);
}
}
Color color = Color.FromScRgb(ts.Material.DiffuseA, ts.Material.DiffuseR, ts.Material.DiffuseG, ts.Material.DiffuseB);
Material material = new DiffuseMaterial(new SolidColorBrush(color));
GeometryModel3D triangleModel = new GeometryModel3D(triangleMesh, material);
ModelVisual3D subModel = new ModelVisual3D();
subModel.Content = triangleModel;
model.Children.Add(subModel);
}
}
}
}
bool capture = false;
Point mouseDownPos;
double startAngleX;
double startAngleY;
void OnMouseMove(object sender, MouseEventArgs e)
{
if (capture)
{
double dx = e.GetPosition(this).X - mouseDownPos.X;
dx = startAngleX + dx;
rotateHorizontal.Angle = dx;
double dy = e.GetPosition(this).Y - mouseDownPos.Y;
dy = startAngleY + dy;
if (dy > 90) dy = 90;
if (dy < -90) dy = -90;
rotateVertical.Angle = dy;
}
}
void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
capture = true;
mouseDownPos = e.GetPosition(this);
startAngleX = rotateHorizontal.Angle;
startAngleY = rotateVertical.Angle;
CaptureMouse();
}
}
void OnMouseUp(object sender, MouseButtonEventArgs e)
{
if (capture) ReleaseMouseCapture();
capture = false;
}
}
}
```