BitmapClass.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using SlimDX;
  4. using SlimDX.D3DCompiler;
  5. using SlimDX.Direct3D11;
  6. using SlimDX.DXGI;
  7. using SlimDX.Windows;
  8. using Device = SlimDX.Direct3D11.Device;
  9. using Resource = SlimDX.Direct3D11.Resource;
  10. using Buffer = SlimDX.Direct3D11.Buffer;
  11. namespace EQ2ModelViewer {
  12. public class BitmapClass {
  13. public struct VertexType {
  14. public Vector3 position;
  15. public Vector2 texture;
  16. };
  17. Buffer m_vertexBuffer;
  18. Buffer m_indexBuffer;
  19. TextureClass m_texture;
  20. int m_vertexCount;
  21. int m_indexCount;
  22. int m_screenWidth;
  23. int m_screenHeight;
  24. int m_bmpWidth;
  25. int m_bmpHeight;
  26. int m_prevPosX;
  27. int m_prevPosY;
  28. TextureShaderClass m_textureShader;
  29. private Matrix m_BaseViewMatrix;
  30. public bool Initialize(Device device, int screenWidth, int screenHeight, string filename, int bmpWidth, int bmpHeight, Matrix baseViewMatrix) {
  31. // Store screen size
  32. m_screenWidth = screenWidth;
  33. m_screenHeight = screenHeight;
  34. // Store the size in pixels that this bitmap should be rendered at
  35. m_bmpWidth = bmpWidth;
  36. m_bmpHeight = bmpHeight;
  37. // Init previous rendering position to -1
  38. m_prevPosX = -1;
  39. m_prevPosY = -1;
  40. m_BaseViewMatrix = baseViewMatrix;
  41. // Init the vertex and index buffers
  42. if (!InitializeBuffers(device))
  43. return false;
  44. if (!LoadTexture(device, filename))
  45. return false;
  46. m_textureShader = new TextureShaderClass();
  47. if (!m_textureShader.Initialize(device))
  48. return false;
  49. return true;
  50. }
  51. public void ShutDown() {
  52. if (m_indexBuffer != null)
  53. m_indexBuffer.Dispose();
  54. if (m_vertexBuffer != null)
  55. m_vertexBuffer.Dispose();
  56. if (m_texture != null)
  57. m_texture.ShutDown();
  58. if (m_textureShader != null)
  59. m_textureShader.ShutDown();
  60. }
  61. public bool Render(GraphicClass Graphics, int posX, int posY) {
  62. if (!UpdateBuffers(Graphics.Context, posX, posY))
  63. return false;
  64. RenderBuffers(Graphics);
  65. return true;
  66. }
  67. public int GetIndexCount() {
  68. return m_indexCount;
  69. }
  70. public ShaderResourceView GetTexture() {
  71. return m_texture.GetTexture();
  72. }
  73. private bool InitializeBuffers(Device device) {
  74. BufferDescription vertexBufferDesc;
  75. BufferDescription indexBufferDesc;
  76. DataStream vertexData;
  77. DataStream indexData;
  78. m_vertexCount = 6;
  79. m_indexCount = m_vertexCount;
  80. vertexData = new DataStream(m_vertexCount * System.Runtime.InteropServices.Marshal.SizeOf(typeof(VertexType)), true, true);
  81. indexData = new DataStream(m_indexCount * sizeof(uint), true, true);
  82. for (uint i = 0; i < m_indexCount; i++)
  83. indexData.Write(i);
  84. indexData.Position = 0;
  85. vertexBufferDesc = new BufferDescription(System.Runtime.InteropServices.Marshal.SizeOf(typeof(VertexType)) * m_vertexCount, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);
  86. m_vertexBuffer = new Buffer(device, vertexData, vertexBufferDesc);
  87. indexBufferDesc = new BufferDescription(System.Runtime.InteropServices.Marshal.SizeOf(typeof(UInt32)) * m_indexCount, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
  88. m_indexBuffer = new Buffer(device, indexData, indexBufferDesc);
  89. return true;
  90. }
  91. private bool UpdateBuffers(DeviceContext context, int posX, int posY) {
  92. float left;
  93. float right;
  94. float top;
  95. float bottom;
  96. VertexType[] vertices;
  97. DataBox mappedResource;
  98. if (posX == m_prevPosX && posY == m_prevPosY)
  99. return true;
  100. m_prevPosX = posX;
  101. m_prevPosY = posY;
  102. // Calculate the screen coordinate of the left side of the bitmap
  103. left = (float)((m_screenWidth / 2) * -1) + (float)posX;
  104. right = left + (float)m_bmpWidth;
  105. // Calculate the screen coordinates of the top of the bitmap
  106. top = (float)(m_screenHeight / 2) - (float)posY;
  107. bottom = top - (float)m_bmpHeight;
  108. vertices = new VertexType[m_vertexCount];
  109. vertices[0].position = new Vector3(left, top, 0.0f);
  110. vertices[0].texture = new Vector2(0.0f, 0.0f);
  111. vertices[1].position = new Vector3(right, bottom, 0.0f);
  112. vertices[1].texture = new Vector2(1.0f, 1.0f);
  113. vertices[2].position = new Vector3(left, bottom, 0.0f);
  114. vertices[2].texture = new Vector2(0.0f, 1.0f);
  115. vertices[3].position = new Vector3(left, top, 0.0f);
  116. vertices[3].texture = new Vector2(0.0f, 0.0f);
  117. vertices[4].position = new Vector3(right, top, 0.0f);
  118. vertices[4].texture = new Vector2(1.0f, 0.0f);
  119. vertices[5].position = new Vector3(right, bottom, 0.0f);
  120. vertices[5].texture = new Vector2(1.0f, 1.0f);
  121. mappedResource = context.MapSubresource(m_vertexBuffer, MapMode.WriteDiscard, SlimDX.Direct3D11.MapFlags.None);
  122. VertexType[] data = vertices;
  123. mappedResource.Data.WriteRange<VertexType>(data);
  124. context.UnmapSubresource(m_vertexBuffer, 0);
  125. return true;
  126. }
  127. private void RenderBuffers(GraphicClass Graphics) {
  128. int stride;
  129. int offset;
  130. stride = System.Runtime.InteropServices.Marshal.SizeOf(typeof(VertexType));
  131. offset = 0;
  132. Graphics.Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_vertexBuffer, stride, offset));
  133. Graphics.Context.InputAssembler.SetIndexBuffer(m_indexBuffer, Format.R32_UInt, offset);
  134. Graphics.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
  135. m_textureShader.Render(Graphics.Context, m_indexCount, Graphics.GetWorldMatrix(), m_BaseViewMatrix, Graphics.GetOrthoMatrix(), GetTexture());
  136. }
  137. private bool LoadTexture(Device device, string filename) {
  138. m_texture = new TextureClass();
  139. if (!m_texture.Initialize(device, filename))
  140. return false;
  141. return true;
  142. }
  143. }
  144. }