MeshClass.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using System.Collections.Generic;
  5. using SlimDX;
  6. using SlimDX.D3DCompiler;
  7. using SlimDX.Direct3D11;
  8. using SlimDX.DXGI;
  9. using SlimDX.Windows;
  10. using Device = SlimDX.Direct3D11.Device;
  11. using Resource = SlimDX.Direct3D11.Resource;
  12. using Buffer = SlimDX.Direct3D11.Buffer;
  13. namespace EQ2ModelViewer
  14. {
  15. public class MeshClass
  16. {
  17. public struct EQ2Model
  18. {
  19. public float x, y, z;
  20. public float tu, tv;
  21. public float nx, ny, nz;
  22. }
  23. public struct EQ2Region
  24. {
  25. public float x, y, z;
  26. public float tu, tv, tz;
  27. }
  28. private Buffer m_VertexBuffer;
  29. private Buffer m_IndexBuffer;
  30. private int m_VertexCount;
  31. private int m_IndexCount;
  32. public EQ2Model[] m_model;
  33. TextureClass m_Texture;
  34. public void SetFaceCount(int count)
  35. {
  36. m_VertexCount = count;
  37. m_IndexCount = count;
  38. m_model = new EQ2Model[count];
  39. }
  40. public void AddData(int index, float x, float y, float z, float nx, float ny, float nz, float tu, float tv)
  41. {
  42. if (index > m_VertexCount)
  43. throw new IndexOutOfRangeException("MeshClass AddData Failed: index (" + index + ") is greater then m_VertexCount (" + m_VertexCount + ")");
  44. m_model[index].x = x;
  45. m_model[index].y = y;
  46. m_model[index].z = z;
  47. m_model[index].nx = nx;
  48. m_model[index].ny = ny;
  49. m_model[index].nz = nz;
  50. m_model[index].tu = tu;
  51. m_model[index].tv = tv;
  52. }
  53. public List<Vector3> GetVertices()
  54. {
  55. List<Vector3> ret = new List<Vector3>();
  56. foreach (EQ2Model m in m_model)
  57. {
  58. Vector3 newVec = new Vector3(m.x, m.y, m.z);
  59. ret.Add(newVec);
  60. }
  61. return ret;
  62. }
  63. public bool InitializeBuffers(Device device)
  64. {
  65. BufferDescription vertexBufferDesc = new BufferDescription();
  66. BufferDescription indexBufferDesc = new BufferDescription();
  67. DataStream vertices = new DataStream(System.Runtime.InteropServices.Marshal.SizeOf(typeof(EQ2Model)) * m_VertexCount, true, true);
  68. DataStream indices = new DataStream(sizeof(ulong) * m_IndexCount, true, true);
  69. for (int i = 0; i < m_VertexCount; i++)
  70. {
  71. vertices.Write(new Vector3(m_model[i].x, m_model[i].y, m_model[i].z));
  72. vertices.Write(new Vector2(m_model[i].tu, m_model[i].tv));
  73. vertices.Write(new Vector3(m_model[i].nx, m_model[i].ny, m_model[i].nz));
  74. indices.Write(i);
  75. }
  76. vertices.Position = 0;
  77. indices.Position = 0;
  78. vertexBufferDesc.Usage = ResourceUsage.Default;
  79. vertexBufferDesc.SizeInBytes = (int)vertices.Length;
  80. vertexBufferDesc.BindFlags = BindFlags.VertexBuffer;
  81. vertexBufferDesc.CpuAccessFlags = CpuAccessFlags.None;
  82. vertexBufferDesc.OptionFlags = ResourceOptionFlags.None;
  83. vertexBufferDesc.StructureByteStride = 0;
  84. m_VertexBuffer = new Buffer(device, vertices, vertexBufferDesc);
  85. vertices.Dispose();
  86. indexBufferDesc.Usage = ResourceUsage.Default;
  87. indexBufferDesc.SizeInBytes = (int)indices.Length;
  88. indexBufferDesc.BindFlags = BindFlags.IndexBuffer;
  89. indexBufferDesc.CpuAccessFlags = CpuAccessFlags.None;
  90. indexBufferDesc.OptionFlags = ResourceOptionFlags.None;
  91. indexBufferDesc.StructureByteStride = 0;
  92. m_IndexBuffer = new Buffer(device, indices, indexBufferDesc);
  93. indices.Dispose();
  94. return true;
  95. }
  96. public void RenderBuffers(DeviceContext context)
  97. {
  98. int stride = System.Runtime.InteropServices.Marshal.SizeOf(typeof(EQ2Model));
  99. int offset = 0;
  100. context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_VertexBuffer, stride, offset));
  101. context.InputAssembler.SetIndexBuffer(m_IndexBuffer, Format.R32_UInt, 0);
  102. context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
  103. }
  104. public void ShutDown()
  105. {
  106. ReleaseTexture();
  107. m_VertexBuffer.Dispose();
  108. m_IndexBuffer.Dispose();
  109. }
  110. public int GetIndexCount()
  111. {
  112. return m_IndexCount;
  113. }
  114. public ShaderResourceView GetTexture()
  115. {
  116. if (m_Texture == null)
  117. return null;
  118. return m_Texture.GetTexture();
  119. }
  120. private void ReleaseTexture()
  121. {
  122. if (m_Texture != null)
  123. m_Texture.ShutDown();
  124. }
  125. public bool LoadTexture(Device device, string filename)
  126. {
  127. m_Texture = new TextureClass();
  128. if (m_Texture == null)
  129. {
  130. Console.WriteLine("Model: Failed to create an instance of TextureClass()...WTF!");
  131. return false;
  132. }
  133. if (!m_Texture.Initialize(device, filename))
  134. {
  135. Console.WriteLine("Model: Failed to load the texture.");
  136. return false;
  137. }
  138. return true;
  139. }
  140. }
  141. }