Advertisement
Guest User

CModel

a guest
Feb 15th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7.  
  8. namespace XNA_Model
  9. {
  10.  
  11. public interface IRenderable
  12. {
  13. void Draw(Matrix View, Matrix Projection, Vector3 CameraPosition);
  14. }
  15.  
  16. public class CModel : IRenderable
  17. {
  18. private GraphicsDevice m_graphicsDevice;
  19.  
  20. public Model Model { get; private set; }
  21. public Vector3 Position { get; set; }
  22. public Vector3 Rotation { get; set; }
  23. public Vector3 Scale { get; set; }
  24.  
  25. private Matrix[] m_transforms;
  26. private BoundingSphere m_boundingSphere;
  27.  
  28. public BoundingSphere BoundingSphere
  29. {
  30. get { return m_boundingSphere.Transform(Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position)); }
  31. }
  32.  
  33. public CModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice)
  34. {
  35. this.m_graphicsDevice = graphicsDevice;
  36. this.Model = Model;
  37. m_transforms = new Matrix[Model.Bones.Count];
  38. Model.CopyAbsoluteBoneTransformsTo(m_transforms);
  39.  
  40. BuildBoundingSphere();
  41. GenerateTags();
  42.  
  43. this.Position = Position;
  44. this.Rotation = Rotation;
  45. this.Scale = Scale;
  46. }
  47.  
  48. private void BuildBoundingSphere()
  49. {
  50. BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0);
  51. foreach (ModelMesh mesh in Model.Meshes)
  52. {
  53. BoundingSphere transformed = mesh.BoundingSphere.Transform(
  54. m_transforms[mesh.ParentBone.Index]);
  55.  
  56. sphere = BoundingSphere.CreateMerged(sphere, transformed);
  57. }
  58. this.m_boundingSphere = sphere;
  59. }
  60. private void GenerateTags()
  61. {
  62. foreach (ModelMesh mesh in Model.Meshes)
  63. foreach (ModelMeshPart part in mesh.MeshParts)
  64. if (part.Effect is BasicEffect)
  65. {
  66. BasicEffect effect = (BasicEffect)part.Effect;
  67. MeshTag tag = new MeshTag(effect.DiffuseColor, effect.Texture, effect.SpecularPower);
  68. part.Tag = tag;
  69. }
  70. }
  71.  
  72. public void Draw(Matrix View, Matrix Projection, Vector3 CameraPosition)
  73. {
  74. Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position);
  75. foreach (ModelMesh mesh in Model.Meshes)
  76. {
  77. Matrix localWorld = m_transforms[mesh.ParentBone.Index] * baseWorld;
  78. foreach (ModelMeshPart meshPart in mesh.MeshParts)
  79. {
  80. Effect effect = meshPart.Effect;
  81. if (effect is BasicEffect)
  82. {
  83. ((BasicEffect)effect).World = localWorld;
  84. ((BasicEffect)effect).View = View;
  85. ((BasicEffect)effect).Projection = Projection;
  86. ((BasicEffect)effect).EnableDefaultLighting();
  87. }
  88. else
  89. {
  90. SetEffectParameter(effect, "World", localWorld);
  91. SetEffectParameter(effect, "View", View);
  92. SetEffectParameter(effect, "Projection", Projection);
  93. SetEffectParameter(effect, "CameraPosition", CameraPosition);
  94. ((MeshTag)meshPart.Tag).Material.SetEffectParameters(effect);
  95. }
  96. }
  97.  
  98. mesh.Draw();
  99. }
  100. }
  101. void SetEffectParameter(Effect effect, string paraName, object val)
  102. {
  103. if (effect.Parameters[paraName] == null)
  104. return;
  105. if (val is Vector3)
  106. effect.Parameters[paraName].SetValue((Vector3)val);
  107. else if (val is bool)
  108. effect.Parameters[paraName].SetValue((bool)val);
  109. else if (val is Matrix)
  110. effect.Parameters[paraName].SetValue((Matrix)val);
  111. else if (val is Texture2D)
  112. effect.Parameters[paraName].SetValue((Texture2D)val);
  113. }
  114.  
  115. public void SetModelEffect(Effect effect, bool CopyEffect)
  116. {
  117. foreach (ModelMesh mesh in Model.Meshes)
  118. SetMeshEffect(mesh.Name, effect, CopyEffect);
  119. }
  120. public void SetModelMaterial(Material material)
  121. {
  122. foreach (ModelMesh mesh in Model.Meshes)
  123. SetMeshMaterial(mesh.Name, material);
  124. }
  125. public void SetMeshEffect(string MeshName, Effect effect, bool CopyEffect)
  126. {
  127. foreach (ModelMesh mesh in Model.Meshes)
  128. {
  129. if (mesh.Name != MeshName)
  130. continue;
  131. foreach (ModelMeshPart part in mesh.MeshParts)
  132. {
  133. Effect toSet = effect;
  134. if (CopyEffect)
  135. toSet = effect.Clone();
  136. MeshTag tag = ((MeshTag)part.Tag);
  137. if (tag.Texture != null)
  138. {
  139. SetEffectParameter(toSet, "BasicTexture", tag.Texture);
  140. SetEffectParameter(toSet, "TextureEnabled", true);
  141. }
  142. else
  143. SetEffectParameter(toSet, "TextureEnabled", false);
  144. SetEffectParameter(toSet, "DiffuseColor", tag.Color);
  145. SetEffectParameter(toSet, "SpecularPower", tag.SpecularPower);
  146. part.Effect = toSet;
  147. }
  148. }
  149. }
  150. public void SetMeshMaterial(string MeshName, Material material)
  151. {
  152. foreach (ModelMesh mesh in Model.Meshes)
  153. {
  154. if (mesh.Name != MeshName)
  155. continue;
  156. foreach (ModelMeshPart meshPart in mesh.MeshParts)
  157. ((MeshTag)meshPart.Tag).Material = material;
  158. }
  159. }
  160. public void VomitBoneNames()
  161. {
  162. foreach (ModelMesh mesh in Model.Meshes)
  163. {
  164. Console.WriteLine(mesh.Name);
  165. }
  166. }
  167. public void CacheEffects()
  168. {
  169. foreach (ModelMesh mesh in Model.Meshes)
  170. foreach (ModelMeshPart part in mesh.MeshParts)
  171. ((MeshTag)part.Tag).CachedEffect = part.Effect;
  172. }
  173. public void RestoreEffects()
  174. {
  175. foreach (ModelMesh mesh in Model.Meshes)
  176. foreach (ModelMeshPart part in mesh.MeshParts)
  177. if (((MeshTag)part.Tag).CachedEffect != null)
  178. part.Effect = ((MeshTag)part.Tag).CachedEffect;
  179. }
  180. }
  181. public class MeshTag
  182. {
  183. public Vector3 Color;
  184. public Texture2D Texture;
  185. public float SpecularPower;
  186. public Effect CachedEffect = null;
  187. public Material Material = new Material();
  188.  
  189. public MeshTag(Vector3 Color, Texture2D Texture, float SpecularPower)
  190. {
  191. this.Color = Color;
  192. this.Texture = Texture;
  193. this.SpecularPower = SpecularPower;
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement