PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentSystem/Rendering/CameraData.cs

#
C# | 190 lines | 105 code | 16 blank | 69 comment | 8 complexity | 7584ccd056ebfc2d92d946ea5aaf3b08 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.IO;
  3. using Delta.Utilities;
  4. using Delta.Utilities.Datatypes;
  5. using Delta.Utilities.Helpers;
  6. namespace Delta.ContentSystem.Rendering
  7. {
  8. /// <summary>
  9. /// Helper class to store camera data and save it out as content to use used
  10. /// in levels or scenes (or just in your game code).
  11. /// </summary>
  12. public class CameraData : Content, ISaveLoadBinary
  13. {
  14. #region Constants
  15. /// <summary>
  16. /// Version number for this CameraData. If this goes above 1, we need
  17. /// to support loading older versions as well. Saving is always going
  18. /// to be the latest version (this one).
  19. /// </summary>
  20. private const int VersionNumber = 1;
  21. #endregion
  22. #region Get (Static)
  23. /// <summary>
  24. /// Get and load content based on the content name. This method makes sure
  25. /// we do not load the same content twice (the constructor is protected).
  26. /// </summary>
  27. /// <param name="contentName">Content name we want to load, this is
  28. /// passed onto the Content System, which will do the actual loading with
  29. /// help of the Load method in this class.</param>
  30. /// <returns>The loaded Content object, always unique for the same
  31. /// name, this helps comparing data.</returns>
  32. public static CameraData Get(string contentName)
  33. {
  34. return Get<CameraData>(contentName, ContentType.Camera);
  35. }
  36. #endregion
  37. #region FieldOfView (Public)
  38. /// <summary>
  39. /// Field of view, usually around 60.
  40. /// </summary>
  41. public float FieldOfView;
  42. #endregion
  43. #region NearPlane (Public)
  44. /// <summary>
  45. /// Near plane, when does the camera start in meters from the camera.
  46. /// </summary>
  47. public float NearPlane;
  48. #endregion
  49. #region FarPlane (Public)
  50. /// <summary>
  51. /// Far plane, how far can we look in meters.
  52. /// </summary>
  53. public float FarPlane;
  54. #endregion
  55. #region Path (Public)
  56. /// <summary>
  57. /// Optional precomputed camera path.
  58. /// </summary>
  59. public new Matrix[] Path;
  60. #endregion
  61. #region Constructors
  62. /// <summary>
  63. /// Create camera data
  64. /// </summary>
  65. public CameraData()
  66. : base(EmptyName, ContentType.Camera)
  67. {
  68. Path = new Matrix[0];
  69. }
  70. /// <summary>
  71. /// Create camera data by loading it from content, use Get to call this.
  72. /// Use the static Get method to call this.
  73. /// </summary>
  74. /// <param name="contentName">Name of the content.</param>
  75. protected CameraData(string contentName)
  76. : base(contentName, ContentType.Camera)
  77. {
  78. }
  79. #endregion
  80. #region ISaveLoadBinary Members
  81. /// <summary>
  82. /// Load camera data (positions, matrices, etc.) from a stream.
  83. /// </summary>
  84. /// <param name="reader">Reader</param>
  85. public void Load(BinaryReader reader)
  86. {
  87. // We currently only support our version, if more versions are added,
  88. // we need to do different loading code depending on the version here.
  89. int version = reader.ReadInt32();
  90. if (version != VersionNumber)
  91. {
  92. Log.InvalidVersionWarning(GetType().Name + ": " + Name, version,
  93. VersionNumber);
  94. return;
  95. }
  96. // the field of view
  97. FieldOfView = reader.ReadSingle();
  98. // the near
  99. NearPlane = reader.ReadSingle();
  100. // and far plane
  101. FarPlane = reader.ReadSingle();
  102. // and finally the camera animation path
  103. int numberOfPathFrames = reader.ReadInt32();
  104. Path = new Matrix[numberOfPathFrames];
  105. for (int index = 0; index < numberOfPathFrames; index++)
  106. {
  107. Path[index].Load(reader);
  108. }
  109. }
  110. /// <summary>
  111. /// Save camera data (positions, matrices, etc.) to a stream.
  112. /// </summary>
  113. /// <param name="writer">Writer</param>
  114. public void Save(BinaryWriter writer)
  115. {
  116. writer.Write(VersionNumber);
  117. // the field of view
  118. writer.Write(FieldOfView);
  119. // the near
  120. writer.Write(NearPlane);
  121. // and far plane
  122. writer.Write(FarPlane);
  123. // and finally the camera animation path
  124. writer.Write(Path.Length);
  125. for (int index = 0; index < Path.Length; index++)
  126. {
  127. Path[index].Save(writer);
  128. }
  129. }
  130. #endregion
  131. #region Methods (Private)
  132. #region Load
  133. /// <summary>
  134. /// Native load method, will just load the data.
  135. /// </summary>
  136. /// <param name="alreadyLoadedNativeData">
  137. /// The first instance that has already loaded the required content data
  138. /// of this content class or just 'null' if there is none loaded yet (or
  139. /// anymore).
  140. /// </param>
  141. protected override void Load(Content alreadyLoadedNativeData)
  142. {
  143. try
  144. {
  145. if (alreadyLoadedNativeData != null)
  146. {
  147. // Just clone all data
  148. CameraData otherData = alreadyLoadedNativeData as CameraData;
  149. FieldOfView = otherData.FieldOfView;
  150. NearPlane = otherData.NearPlane;
  151. FarPlane = otherData.FarPlane;
  152. Path = otherData.Path;
  153. // This object cannot be used for cloning now, but the caller (Mesh)
  154. // can set itself as the NativeClassObject and allow native cloning.
  155. return;
  156. }
  157. if (String.IsNullOrEmpty(RelativeFilePath) == false)
  158. {
  159. // Load via the ISaveLoadBinary interface methods below.
  160. // Cloning should not really happen for shaders anyway.
  161. FileHelper.Load(RelativeFilePath, this);
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. Log.Warning("Failed to load mesh animation data from file '" +
  167. RelativeFilePath + "': " + ex);
  168. FailedToLoad = true;
  169. }
  170. }
  171. #endregion
  172. #endregion
  173. }
  174. }