09-24-2010, 05:59 PM
I'm having an issue with the ReadingComplete function. If I load a very small file i have no troubles or if i my modelLoaded function after the model has loaded everything works great. However when i use the ReadingComplete function it seem to run the function before everything has loaded. Have I done something wrong?
MainWindow.xaml.cs
MainWindow.xaml
thanks,
Bryan
MainWindow.xaml.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
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 System.IO;
using System.Windows.Media.Media3D;
using System.Windows.Threading;
using Ab3d;
namespace Simple_3ds_loading
{
public partial class MainWindow : Window
{
// spin timer
private DispatcherTimer dTimer;
private int count;
Ab3d.Reader3ds newReader3ds;
Model3DGroup modelGroup;
//transforms
Transform3DGroup myTransform3DGroup = new Transform3DGroup();
ScaleTransform3D myScaleTransform3D = new ScaleTransform3D();
RotateTransform3D myRotateTransform3D = new RotateTransform3D();
AxisAngleRotation3D myAxisAngleRotation3d = new AxisAngleRotation3D();
//camra and light
PerspectiveCamera myPCamera = new PerspectiveCamera();
DirectionalLight myDirectionalLight = new DirectionalLight();
public MainWindow()
{
InitializeComponent();
}
private void myPageLoaded(object sender, EventArgs e)
{
loadModel();
}
void loadModel()
{
//bring up the choose file dialog
Microsoft.Win32.OpenFileDialog loadWho = new Microsoft.Win32.OpenFileDialog();
loadWho.Filter = "3-D files (.3ds)|*.3ds";
//loadWho.FilterIndex = 0;
Nullable<bool> result = loadWho.ShowDialog(this);
if (result == true)
{
newReader3ds = new Ab3d.Reader3ds();
newReader3ds.ThrowMissingTextureException = true;
newReader3ds.ReadingComplete += new EventHandler(newReader3ds_ReadingComplete);
Console.WriteLine("--");
Console.WriteLine(loadWho.FileName);
try
{
modelGroup = newReader3ds.ReadFile(loadWho.FileName, Viewport1);
}
catch (Reader3ds.MissingTextureException f)
{
MessageBox.Show("Missing texture: " + f.MissingTextureFileName);
newReader3ds.ThrowMissingTextureException = false;
modelGroup = newReader3ds.ReadFile(loadWho.FileName, Viewport1);
}
}
else
{
//must load a file
loadModel();
}
}
void OnClick1(object sender, RoutedEventArgs e)
{
Console.WriteLine("pressed");
modelLoaded();
}
private void newReader3ds_ReadingComplete(Object sender, EventArgs e)
{
Console.WriteLine("we think the model is finished loading");
//modelLoaded();
}
private void modelLoaded()
{
//Set camera values.
myPCamera = (Viewport1.Camera as PerspectiveCamera);
//myPCamera.FarPlaneDistance = 5000;
//myPCamera.NearPlaneDistance = 1;
myPCamera.FieldOfView = 60;
myPCamera.Position = new Point3D(0, 60, 300);
myPCamera.LookDirection = new Vector3D(0, -.2, -1);
myPCamera.UpDirection = new Vector3D(0, 1, 0);
// Directional light values.
myDirectionalLight.Color = Colors.White;
myDirectionalLight.Direction = new Vector3D(0, -1, 0);
modelGroup.Children.Add(myDirectionalLight);
//get bounds
Rect3D objectBounds = modelGroup.Bounds;
//deside how big to make it.
double resizePer = (300 / objectBounds.SizeX);
double resizePerY = (300 / objectBounds.SizeY);
double resizePerZ = (300 / objectBounds.SizeZ);
if (resizePerY < resizePer)
{
resizePer = resizePerY;
}
if (resizePerZ < resizePer)
{
resizePer = resizePerZ;
}
//set the size
myScaleTransform3D.ScaleX = resizePer;
myScaleTransform3D.ScaleY = resizePer;
myScaleTransform3D.ScaleZ = resizePer;
myTransform3DGroup.Children.Add(myScaleTransform3D);
// Create a transformation that i can use to do some rotating.
myAxisAngleRotation3d.Axis = new Vector3D(0, 3, 0);
myAxisAngleRotation3d.Angle = 0;
myRotateTransform3D.Rotation = myAxisAngleRotation3d;
myTransform3DGroup.Children.Add(myRotateTransform3D);
modelGroup.Transform = myTransform3DGroup;
//start the timer to give us some spinning...for fun.
count = 100;
dTimer = new DispatcherTimer();
dTimer.Interval = new TimeSpan(100000);
dTimer.Tick += new EventHandler(dTimer_Tick);
dTimer.Start();
}
void dTimer_Tick(object sender, EventArgs e)
{
count++;
if (count > 660)
{
count = 100;
}
if (count > 300)
{
//and this makes it spin.
myAxisAngleRotation3d.Angle = (count-300)*-1;
}
}
}
}
MainWindow.xaml
Code:
<Window x:Class="Simple_3ds_loading.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800"
Loaded="myPageLoaded">
<Grid>
<Viewport3D Name="Viewport1" ClipToBounds="True"></Viewport3D>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="691,526,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="OnClick1" ClickMode="Press"/>
</Grid>
</Window>
thanks,
Bryan