MeshClass.cs 5.7 KB

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