VertexPositionTexture_Texture2D.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace OpenVIII
  6. {
  7. public class VertexPositionTexture_Texture2D : IDisposable
  8. {
  9. #region Fields
  10. public TextureHandler Texture;
  11. public VertexPositionTexture[] VPT;
  12. private AlphaTestEffect ate;
  13. /// <summary>
  14. /// Buffer that holds the VPT after transforming the location.
  15. /// </summary>
  16. private VertexPositionTexture[] TransformedVPT;
  17. #endregion Fields
  18. #region Constructors
  19. public VertexPositionTexture_Texture2D(VertexPositionTexture[] vPT, TextureHandler texture)
  20. {
  21. VPT = vPT;
  22. TransformedVPT = (VertexPositionTexture[])VPT.Clone();
  23. Texture = texture;
  24. ate = new AlphaTestEffect(Memory.Graphics.GraphicsDevice)
  25. {
  26. Texture = (Texture2D)Texture
  27. };
  28. }
  29. #endregion Constructors
  30. #region Destructors
  31. ~VertexPositionTexture_Texture2D()
  32. {
  33. Dispose();
  34. }
  35. #endregion Destructors
  36. #region Methods
  37. public static VertexPositionTexture_Texture2D operator +(VertexPositionTexture_Texture2D left, VertexPositionTexture_Texture2D right)
  38. {
  39. if (left.Texture == right.Texture)
  40. {
  41. //could be optimized.
  42. var tmp = new List<VertexPositionTexture>(left.VPT.Length + right.VPT.Length);
  43. tmp.AddRange(left.VPT);
  44. tmp.AddRange(right.VPT);
  45. return new VertexPositionTexture_Texture2D(tmp.ToArray(), left.Texture);
  46. }
  47. else throw new Exception("Textures must match or else won't work");
  48. }
  49. public void Dispose() => ((IDisposable)ate).Dispose();
  50. public void UpdateForBattle(Vector3 pos) =>
  51. //if used beyond battle might need to update this to use different matrices
  52. //Viewport vp = Memory.graphics.GraphicsDevice.Viewport;
  53. //var _1 = vp.Unproject(new Vector3(vp.Width / 2f, vp.Height / 2f, 0f), Module_battle_debug.ProjectionMatrix, Module_battle_debug.ViewMatrix, Module_battle_debug.WorldMatrix);
  54. //var _2 = vp.Unproject(new Vector3(vp.Width / 2f, vp.Height / 2f, 1f), Module_battle_debug.ProjectionMatrix, Module_battle_debug.ViewMatrix, Module_battle_debug.WorldMatrix);
  55. //var centerscreen = Vector3.Normalize(_2 - _1);
  56. //Matrix t = Matrix.CreateTranslation(pos);
  57. //Matrix lookat = Matrix.CreateLookAt(pos, _1, Vector3.Up);
  58. //float yaw = (float)Math.Atan2(lookat.M13, lookat.M33);
  59. //float pitch = (float)Math.Asin(-lookat.M23);
  60. //float roll = (float)Math.Atan2(lookat.M21, lookat.M22);
  61. //Quaternion q = Quaternion.CreateFromYawPitchRoll(yaw, pitch, 0f);
  62. //The above code does the same thing as CreateBillboard but if you need more control might be worth having.
  63. Update(ModuleBattleDebug.CreateBillboard(pos));
  64. public void Update(Matrix bb)
  65. {
  66. for (var i = 0; i < VPT.Length; i++)
  67. {
  68. TransformedVPT[i].Position = Vector3.Transform(VPT[i].Position, bb);
  69. //TransformedVPT[i].Position = Vector3.Transform(VPT[i].Position, q);
  70. //TransformedVPT[i].Position = Vector3.Transform(TransformedVPT[i].Position, t);
  71. }
  72. }
  73. public void DrawForBattle()
  74. {
  75. ate.World = ModuleBattleDebug.WorldMatrix;
  76. ate.View = ModuleBattleDebug.ViewMatrix;
  77. ate.Projection = ModuleBattleDebug.ProjectionMatrix;
  78. Draw();
  79. }
  80. public void Draw()
  81. {
  82. Memory.Graphics.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  83. Memory.Graphics.GraphicsDevice.BlendState = BlendState.AlphaBlend;
  84. Memory.Graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  85. Memory.Graphics.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
  86. ModuleBattleDebug.Effect.TextureEnabled = true;
  87. foreach (var pass in ate.CurrentTechnique.Passes)
  88. {
  89. pass.Apply();
  90. Memory.Graphics.GraphicsDevice.DrawUserPrimitives(primitiveType: PrimitiveType.TriangleList,
  91. vertexData: TransformedVPT, vertexOffset: 0, primitiveCount: VPT.Length / 3);
  92. }
  93. }
  94. #endregion Methods
  95. }
  96. }