MeshSerializer.cs :  » Game » RealmForge » Axiom » Serialization » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » Axiom » Serialization » MeshSerializer.cs
using System;
using System.Collections;
using System.IO;
using Axiom.Core;

namespace Axiom.Serialization{
  /// <summary>
  ///    Class for serialising mesh data to/from an OGRE .mesh file.
  /// </summary>
  /// <remarks>
  ///    This class allows exporters to write OGRE .mesh files easily, and allows the
  ///    OGRE engine to import .mesh files into instatiated OGRE Meshes.
  ///    <p/>
  ///    It's important to realize that this exporter uses OGRE terminology. In this context,
  ///    'Mesh' means a top-level mesh structure which can actually contain many SubMeshes, each
  ///    of which has only one Material. Modelling packages may refer to these differently, for
  ///    example in Milkshape, it says 'Model' instead of 'Mesh' and 'Mesh' instead of 'SubMesh', 
  ///    but the theory is the same.
  /// </remarks>
  public sealed class MeshSerializer : Serializer {
    #region Fields

    /// <summary>
    ///    Lookup table holding the various mesh serializer versions.
    /// </summary>
    private Hashtable implementations = new Hashtable();

    /// <summary>
    ///    Current version string.
    /// </summary>
    private static string currentVersion = "[MeshSerializer_v1.30]";

    #endregion Fields

    #region Constructor

    /// <summary>
    ///    Default constructor.
    /// </summary>
    public MeshSerializer() {
      // add the supported .mesh versions
      implementations.Add("[MeshSerializer_v1.10]", new MeshSerializerImplv11());
      implementations.Add("[MeshSerializer_v1.20]", new MeshSerializerImplv12());
      implementations.Add(currentVersion, new MeshSerializerImpl());
    }

    #endregion Constructor

    #region Methods

    /// <summary>
    ///    Exports a mesh to the file specified.
    /// </summary>
    /// <param name="mesh">Reference to the mesh to export.</param>
    /// <param name="fileName">The destination filename.</param>
    public void ExportMesh(Mesh mesh, string fileName) {
      throw new NotImplementedException("Mesh exporting is currently not implemented.");
    }

    /// <summary>
    ///    Imports mesh data from a .mesh file.
    /// </summary>
    /// <param name="stream">The stream holding the .mesh data. Must be initialised (pos at the start of the buffer).</param>
    /// <param name="mesh">Reference to the Mesh object which will receive the data. Should be blank already.</param>
    public void ImportMesh(Stream stream, Mesh mesh) {
      BinaryReader reader = new BinaryReader(stream);

      // read the header ID
      ushort headerID = ReadUShort(reader);

      if(headerID != (ushort)MeshChunkID.Header) {
        throw new AxiomException("File header not found.");
      }

      // read version
      string fileVersion = ReadString(reader);

      // set jump back to the start of the reader
      Seek(reader, 0, SeekOrigin.Begin);

      // barf if there specified version is not supported
      if(!implementations.ContainsKey(fileVersion)) {
        throw new AxiomException("Cannot find serializer implementation for version '{0}'.", fileVersion);
      }

            LogManager.Instance.Write("Mesh: Loading '{0}'...", mesh.Name);

            // call implementation
      MeshSerializerImpl serializer = (MeshSerializerImpl)implementations[fileVersion];
      serializer.ImportMesh(stream, mesh);

      // warn on old version of mesh
      if(fileVersion != currentVersion) {
        LogManager.Instance.Write("WARNING: {0} is an older format ({1}); you should upgrade it as soon as possible using the OgreMeshUpdate tool.", mesh.Name, fileVersion);
      }
    }

    #endregion Methods
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.