GraphicClass.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using SlimDX;
  5. using SlimDX.D3DCompiler;
  6. using SlimDX.Direct3D11;
  7. using SlimDX.DXGI;
  8. using SlimDX.Windows;
  9. using Device = SlimDX.Direct3D11.Device;
  10. using Resource = SlimDX.Direct3D11.Resource;
  11. using Buffer = SlimDX.Direct3D11.Buffer;
  12. namespace EQ2ModelViewer
  13. {
  14. public class GraphicClass
  15. {
  16. private Device m_device;
  17. private DeviceContext m_context;
  18. private SwapChain m_swapChain;
  19. private RenderTargetView m_renderTarget;
  20. private Texture2D m_depthBuffer;
  21. private DepthStencilState m_depthStencilState;
  22. private DepthStencilState m_depthDisabledStencilState;
  23. private DepthStencilView m_depthStencilView;
  24. private RasterizerState m_rasterState;
  25. private BlendState m_alphaEnabledBlendState;
  26. private BlendState m_alphaDisableBlendState;
  27. private Matrix m_worldMatrix;
  28. private Matrix m_orthoMatrix;
  29. private Matrix m_projectionMatrix;
  30. public Device Device
  31. {
  32. get { return m_device; }
  33. }
  34. public DeviceContext Context
  35. {
  36. get { return m_context; }
  37. }
  38. public SwapChain SwapChain
  39. {
  40. get { return m_swapChain; }
  41. }
  42. public Matrix GetWorldMatrix()
  43. {
  44. return m_worldMatrix;
  45. }
  46. public Matrix GetOrthoMatrix()
  47. {
  48. return m_orthoMatrix;
  49. }
  50. public Matrix GetProjectionMatrix()
  51. {
  52. return m_projectionMatrix;
  53. }
  54. public bool Initialize(Panel pGraphics)
  55. {
  56. SwapChainDescription description = new SwapChainDescription();
  57. ModeDescription modedesc = new ModeDescription(pGraphics.ClientSize.Width, pGraphics.ClientSize.Height, new Rational(0, 1), Format.R8G8B8A8_UNorm);
  58. modedesc.ScanlineOrdering = DisplayModeScanlineOrdering.Unspecified;
  59. modedesc.Scaling = DisplayModeScaling.Unspecified;
  60. description.BufferCount = 1;
  61. description.IsWindowed = true;
  62. description.ModeDescription = modedesc;
  63. description.Usage = Usage.RenderTargetOutput;
  64. description.OutputHandle = pGraphics.Handle;
  65. description.SampleDescription = new SampleDescription(1, 0);
  66. description.Flags = 0;
  67. Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out m_device, out m_swapChain);
  68. m_context = m_device.ImmediateContext;
  69. using (var resource = Resource.FromSwapChain<Texture2D>(m_swapChain, 0))
  70. m_renderTarget = new RenderTargetView(m_device, resource);
  71. Texture2DDescription depthBufferDesc = new Texture2DDescription();
  72. depthBufferDesc.Width = pGraphics.ClientSize.Width;
  73. depthBufferDesc.Height = pGraphics.ClientSize.Height;
  74. depthBufferDesc.MipLevels = 1;
  75. depthBufferDesc.ArraySize = 1;
  76. depthBufferDesc.Format = Format.D24_UNorm_S8_UInt;
  77. depthBufferDesc.SampleDescription = new SampleDescription(1, 0);
  78. depthBufferDesc.Usage = ResourceUsage.Default;
  79. depthBufferDesc.BindFlags = BindFlags.DepthStencil;
  80. depthBufferDesc.CpuAccessFlags = CpuAccessFlags.None;
  81. depthBufferDesc.OptionFlags = ResourceOptionFlags.None;
  82. m_depthBuffer = new Texture2D(m_device, depthBufferDesc);
  83. DepthStencilStateDescription depthStencilStateDesc = new DepthStencilStateDescription();
  84. depthStencilStateDesc.IsDepthEnabled = true;
  85. depthStencilStateDesc.DepthWriteMask = DepthWriteMask.All;
  86. depthStencilStateDesc.DepthComparison = Comparison.Less;
  87. depthStencilStateDesc.IsStencilEnabled = true;
  88. depthStencilStateDesc.StencilReadMask = 0xFF;
  89. depthStencilStateDesc.StencilWriteMask = 0xFF;
  90. DepthStencilOperationDescription frontface = new DepthStencilOperationDescription();
  91. frontface.FailOperation = StencilOperation.Keep;
  92. frontface.DepthFailOperation = StencilOperation.Increment;
  93. frontface.PassOperation = StencilOperation.Keep;
  94. frontface.Comparison = Comparison.Always;
  95. depthStencilStateDesc.FrontFace = frontface;
  96. DepthStencilOperationDescription backface = new DepthStencilOperationDescription();
  97. backface.FailOperation = StencilOperation.Keep;
  98. backface.DepthFailOperation = StencilOperation.Decrement;
  99. backface.PassOperation = StencilOperation.Keep;
  100. backface.Comparison = Comparison.Always;
  101. depthStencilStateDesc.BackFace = backface;
  102. m_depthStencilState = DepthStencilState.FromDescription(m_device, depthStencilStateDesc);
  103. m_context.OutputMerger.DepthStencilState = m_depthStencilState;
  104. m_context.OutputMerger.DepthStencilReference = 1;
  105. DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription();
  106. depthStencilViewDesc.Format = Format.D24_UNorm_S8_UInt;
  107. depthStencilViewDesc.Dimension = DepthStencilViewDimension.Texture2D;
  108. depthStencilViewDesc.MipSlice = 0;
  109. m_depthStencilView = new DepthStencilView(m_device, m_depthBuffer, depthStencilViewDesc);
  110. m_context.OutputMerger.SetTargets(m_depthStencilView, m_renderTarget);
  111. RasterizerStateDescription rasterDesc = new RasterizerStateDescription();
  112. rasterDesc.IsAntialiasedLineEnabled = false;
  113. rasterDesc.CullMode = CullMode.Back;
  114. rasterDesc.DepthBias = 0;
  115. rasterDesc.DepthBiasClamp = 0.0f;
  116. rasterDesc.IsDepthClipEnabled = true;
  117. rasterDesc.FillMode = FillMode.Solid;
  118. rasterDesc.IsFrontCounterclockwise = false;
  119. rasterDesc.IsMultisampleEnabled = false;
  120. rasterDesc.IsScissorEnabled = false;
  121. rasterDesc.SlopeScaledDepthBias = 0.0f;
  122. m_rasterState = RasterizerState.FromDescription(m_device, rasterDesc);
  123. m_context.Rasterizer.State = m_rasterState;
  124. var viewport = new Viewport(0.0f, 0.0f, pGraphics.ClientSize.Width, pGraphics.ClientSize.Height, 0.0f, 1.0f);
  125. m_context.Rasterizer.SetViewports(viewport);
  126. using (var factory = m_swapChain.GetParent<Factory>())
  127. factory.SetWindowAssociation(pGraphics.Handle, WindowAssociationFlags.IgnoreAltEnter);
  128. // World matrix
  129. m_worldMatrix = Matrix.Identity;
  130. // Projection matrix
  131. float fieldOfView = (float)Math.PI / 4.0f;
  132. float screenAspect = (float)viewport.Width / viewport.Height;
  133. m_projectionMatrix = Matrix.PerspectiveFovLH(fieldOfView, screenAspect, 0.1f, 1000.0f);
  134. // Ortho matrix
  135. m_orthoMatrix = Matrix.OrthoLH(viewport.Width, viewport.Height, 0.1f, 1000.0f);
  136. DepthStencilStateDescription depthDisabledDesc = new DepthStencilStateDescription();
  137. depthDisabledDesc.IsDepthEnabled = false;
  138. depthDisabledDesc.DepthWriteMask = DepthWriteMask.All;
  139. depthDisabledDesc.DepthComparison = Comparison.Less;
  140. depthDisabledDesc.IsStencilEnabled = true;
  141. depthDisabledDesc.StencilReadMask = 0xFF;
  142. depthDisabledDesc.StencilWriteMask = 0xFF;
  143. depthDisabledDesc.FrontFace = frontface;
  144. depthDisabledDesc.BackFace = backface;
  145. m_depthDisabledStencilState = DepthStencilState.FromDescription(m_device, depthDisabledDesc);
  146. BlendStateDescription blendDesc = new BlendStateDescription();
  147. blendDesc.RenderTargets[0].BlendEnable = true;
  148. blendDesc.RenderTargets[0].SourceBlend = BlendOption.One;
  149. blendDesc.RenderTargets[0].DestinationBlend = BlendOption.InverseSourceAlpha;
  150. blendDesc.RenderTargets[0].BlendOperation = BlendOperation.Add;
  151. blendDesc.RenderTargets[0].SourceBlendAlpha = BlendOption.One;
  152. blendDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
  153. blendDesc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add;
  154. blendDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
  155. m_alphaEnabledBlendState = BlendState.FromDescription(m_device, blendDesc);
  156. blendDesc.RenderTargets[0].BlendEnable = false;
  157. m_alphaDisableBlendState = BlendState.FromDescription(m_device, blendDesc);
  158. /*
  159. BlendStateDescription blendDesc2 = new BlendStateDescription();
  160. blendDesc2.RenderTargets[0].BlendEnable = false;
  161. blendDesc2.RenderTargets[0].SourceBlend = BlendOption.One;
  162. blendDesc2.RenderTargets[0].DestinationBlend = BlendOption.InverseSourceAlpha;
  163. blendDesc2.RenderTargets[0].BlendOperation = BlendOperation.Add;
  164. blendDesc2.RenderTargets[0].SourceBlendAlpha = BlendOption.One;
  165. blendDesc2.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
  166. blendDesc2.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add;
  167. blendDesc2.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
  168. m_alphaDisableBlendState = BlendState.FromDescription(m_device, blendDesc2);
  169. */
  170. return true;
  171. }
  172. public void ShutDown()
  173. {
  174. if (m_alphaEnabledBlendState != null)
  175. m_alphaEnabledBlendState.Dispose();
  176. if (m_alphaDisableBlendState != null)
  177. m_alphaDisableBlendState.Dispose();
  178. if (m_rasterState != null)
  179. m_rasterState.Dispose();
  180. if (m_depthStencilView != null)
  181. m_depthStencilView.Dispose();
  182. if (m_depthDisabledStencilState != null)
  183. m_depthDisabledStencilState.Dispose();
  184. if (m_depthStencilState != null)
  185. m_depthStencilState.Dispose();
  186. if (m_depthBuffer != null)
  187. m_depthBuffer.Dispose();
  188. if (m_renderTarget != null)
  189. m_renderTarget.Dispose();
  190. if (m_swapChain != null)
  191. m_swapChain.Dispose();
  192. if (m_device != null)
  193. m_device.Dispose();
  194. }
  195. public void BeginScene()
  196. {
  197. m_context.ClearRenderTargetView(m_renderTarget, new Color4(0.0f, 0.0f, 0.0f));
  198. m_context.ClearDepthStencilView(m_depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);
  199. }
  200. public void EndScene()
  201. {
  202. m_swapChain.Present(0, PresentFlags.None);
  203. }
  204. public void TurnZBufferOn()
  205. {
  206. m_context.OutputMerger.DepthStencilState = m_depthStencilState;
  207. m_context.OutputMerger.DepthStencilReference = 1;
  208. }
  209. public void TurnZBufferOff()
  210. {
  211. m_context.OutputMerger.DepthStencilState = m_depthDisabledStencilState;
  212. m_context.OutputMerger.DepthStencilReference = 1;
  213. }
  214. public void TurnOnAlphaBlending()
  215. {
  216. m_context.OutputMerger.BlendState = m_alphaEnabledBlendState;
  217. m_context.OutputMerger.BlendFactor = new Color4(0.0f, 0.0f, 0.0f, 0.0f);
  218. }
  219. public void TurnOffAlphaBlending()
  220. {
  221. m_context.OutputMerger.BlendState = m_alphaDisableBlendState;
  222. m_context.OutputMerger.BlendFactor = new Color4(0.0f, 0.0f, 0.0f, 0.0f);
  223. }
  224. }
  225. }