CpuAnimatedVertexBufferReader.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #region License
  2. // Copyright 2011-2016 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using Microsoft.Xna.Framework;
  18. using Microsoft.Xna.Framework.Content;
  19. using Microsoft.Xna.Framework.Graphics;
  20. using Microsoft.Xna.Framework.Graphics.PackedVector;
  21. using nkast.Aether.Graphics;
  22. namespace nkast.Aether.Animation.Content
  23. {
  24. public class CpuAnimatedVertexBufferReader : ContentTypeReader<CpuAnimatedVertexBuffer>
  25. {
  26. protected override CpuAnimatedVertexBuffer Read(ContentReader input, CpuAnimatedVertexBuffer buffer)
  27. {
  28. IGraphicsDeviceService graphicsDeviceService = (IGraphicsDeviceService)input.ContentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService));
  29. GraphicsDevice device = graphicsDeviceService.GraphicsDevice;
  30. // read standard VertexBuffer
  31. VertexDeclaration declaration = input.ReadRawObject<VertexDeclaration>();
  32. int vertexCount = (int)input.ReadUInt32();
  33. // int dataSize = vertexCount * declaration.VertexStride;
  34. //byte[] data = new byte[dataSize];
  35. //input.Read(data, 0, dataSize);
  36. //read data
  37. VertexElement[] channels = declaration.GetVertexElements();
  38. var cpuVertices = new VertexIndicesWeightsPositionNormal[vertexCount];
  39. var gpuVertices = new VertexPositionNormalTexture[vertexCount];
  40. for (int i = 0; i < vertexCount; i++)
  41. {
  42. foreach (VertexElement channel in channels)
  43. {
  44. switch (channel.VertexElementUsage)
  45. {
  46. case VertexElementUsage.Position:
  47. System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector3);
  48. Vector3 pos = input.ReadVector3();
  49. if (channel.UsageIndex == 0)
  50. {
  51. cpuVertices[i].Position = pos;
  52. gpuVertices[i].Position = pos;
  53. }
  54. break;
  55. case VertexElementUsage.Normal:
  56. System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector3);
  57. Vector3 nor = input.ReadVector3();
  58. if (channel.UsageIndex == 0)
  59. {
  60. cpuVertices[i].Normal = nor;
  61. gpuVertices[i].Normal = nor;
  62. }
  63. break;
  64. case VertexElementUsage.TextureCoordinate:
  65. System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector2);
  66. Vector2 tex = input.ReadVector2();
  67. if (channel.UsageIndex == 0)
  68. {
  69. gpuVertices[i].TextureCoordinate = tex;
  70. }
  71. break;
  72. case VertexElementUsage.BlendWeight:
  73. System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Vector4);
  74. Vector4 wei = input.ReadVector4();
  75. if (channel.UsageIndex == 0)
  76. {
  77. cpuVertices[i].BlendWeights = wei;
  78. }
  79. break;
  80. case VertexElementUsage.BlendIndices:
  81. System.Diagnostics.Debug.Assert(channel.VertexElementFormat == VertexElementFormat.Byte4);
  82. byte i0 = input.ReadByte();
  83. byte i1 = input.ReadByte();
  84. byte i2 = input.ReadByte();
  85. byte i3 = input.ReadByte();
  86. if (channel.UsageIndex == 0)
  87. {
  88. cpuVertices[i].BlendIndex0 = i0;
  89. cpuVertices[i].BlendIndex1 = i1;
  90. cpuVertices[i].BlendIndex2 = i2;
  91. cpuVertices[i].BlendIndex3 = i3;
  92. }
  93. break;
  94. default:
  95. throw new Exception();
  96. }
  97. }
  98. }
  99. // read extras
  100. bool IsWriteOnly = input.ReadBoolean();
  101. if (buffer == null)
  102. {
  103. BufferUsage usage = (IsWriteOnly) ? BufferUsage.WriteOnly : BufferUsage.None;
  104. buffer = new CpuAnimatedVertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, vertexCount, usage);
  105. }
  106. buffer.SetData(gpuVertices, 0, vertexCount);
  107. buffer.SetGpuVertices(gpuVertices);
  108. buffer.SetCpuVertices(cpuVertices);
  109. return buffer;
  110. }
  111. }
  112. }