FrustumClass.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 FrustumClass
  15. {
  16. private Plane[] m_Planes = new Plane[6];
  17. public void ConstructFrustum(float screenDepth, Matrix projectionMatrix, Matrix viewMatrix)
  18. {
  19. float zMin;
  20. float r;
  21. Matrix matrix;
  22. zMin = -projectionMatrix.M43 / projectionMatrix.M33;
  23. r = screenDepth / (screenDepth - zMin);
  24. projectionMatrix.M33 = r;
  25. projectionMatrix.M43 = -r * zMin;
  26. matrix = Matrix.Multiply(viewMatrix, projectionMatrix);
  27. float a = matrix.M14 + matrix.M13;
  28. float b = matrix.M24 + matrix.M23;
  29. float c = matrix.M34 + matrix.M33;
  30. float d = matrix.M44 + matrix.M43;
  31. m_Planes[0] = new Plane(a, b, c, d);
  32. m_Planes[0].Normalize();
  33. a = matrix.M14 - matrix.M13;
  34. b = matrix.M24 - matrix.M23;
  35. c = matrix.M34 - matrix.M33;
  36. d = matrix.M44 - matrix.M43;
  37. m_Planes[1] = new Plane(a, b, c, d);
  38. m_Planes[1].Normalize();
  39. a = matrix.M14 + matrix.M11;
  40. b = matrix.M24 + matrix.M21;
  41. c = matrix.M34 + matrix.M31;
  42. d = matrix.M44 + matrix.M41;
  43. m_Planes[2] = new Plane(a, b, c, d);
  44. m_Planes[2].Normalize();
  45. a = matrix.M14 - matrix.M11;
  46. b = matrix.M24 - matrix.M21;
  47. c = matrix.M34 - matrix.M31;
  48. d = matrix.M44 - matrix.M41;
  49. m_Planes[3] = new Plane(a, b, c, d);
  50. m_Planes[3].Normalize();
  51. a = matrix.M14 - matrix.M12;
  52. b = matrix.M24 - matrix.M22;
  53. c = matrix.M34 - matrix.M32;
  54. d = matrix.M44 - matrix.M42;
  55. m_Planes[4] = new Plane(a, b, c, d);
  56. m_Planes[4].Normalize();
  57. a = matrix.M14 + matrix.M12;
  58. b = matrix.M24 + matrix.M22;
  59. c = matrix.M34 + matrix.M32;
  60. d = matrix.M44 + matrix.M42;
  61. m_Planes[5] = new Plane(a, b, c, d);
  62. m_Planes[5].Normalize();
  63. }
  64. public bool CheckPoint(float x, float y, float z)
  65. {
  66. for (int i = 0; i < 6; i++)
  67. {
  68. if (Plane.DotCoordinate(m_Planes[i], new Vector3(x, y, z)) < 0.0f)
  69. return false;
  70. }
  71. return true;
  72. }
  73. public bool CheckCube(float xCenter, float yCenter, float zCenter, float radius)
  74. {
  75. for (int i = 0; i < 6; i++)
  76. {
  77. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - radius), (yCenter - radius), (zCenter - radius))) >= 0.0f)
  78. continue;
  79. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + radius), (yCenter - radius), (zCenter - radius))) >= 0.0f)
  80. continue;
  81. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - radius), (yCenter + radius), (zCenter - radius))) >= 0.0f)
  82. continue;
  83. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + radius), (yCenter + radius), (zCenter - radius))) >= 0.0f)
  84. continue;
  85. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - radius), (yCenter - radius), (zCenter + radius))) >= 0.0f)
  86. continue;
  87. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + radius), (yCenter - radius), (zCenter + radius))) >= 0.0f)
  88. continue;
  89. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - radius), (yCenter + radius), (zCenter + radius))) >= 0.0f)
  90. continue;
  91. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + radius), (yCenter + radius), (zCenter + radius))) >= 0.0f)
  92. continue;
  93. return false;
  94. }
  95. return true;
  96. }
  97. public bool CheckSphere(float xCenter, float yCenter, float zCenter, float radius)
  98. {
  99. for (int i = 0; i < 6; i++)
  100. {
  101. if (Plane.DotCoordinate(m_Planes[i], new Vector3(xCenter, yCenter, zCenter)) < -radius)
  102. return false;
  103. }
  104. return true;
  105. }
  106. public bool CheckRectangle(float xCenter, float yCenter, float zCenter, float xSize, float ySize, float zSize)
  107. {
  108. for (int i = 0; i < 6; i++)
  109. {
  110. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - xSize), (yCenter - ySize), (zCenter - zSize))) >= 0.0f)
  111. continue;
  112. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + xSize), (yCenter - ySize), (zCenter - zSize))) >= 0.0f)
  113. continue;
  114. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - xSize), (yCenter + ySize), (zCenter - zSize))) >= 0.0f)
  115. continue;
  116. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - xSize), (yCenter - ySize), (zCenter + zSize))) >= 0.0f)
  117. continue;
  118. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + xSize), (yCenter + ySize), (zCenter - zSize))) >= 0.0f)
  119. continue;
  120. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + xSize), (yCenter - ySize), (zCenter + zSize))) >= 0.0f)
  121. continue;
  122. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter - xSize), (yCenter + ySize), (zCenter + zSize))) >= 0.0f)
  123. continue;
  124. if (Plane.DotCoordinate(m_Planes[i], new Vector3((xCenter + xSize), (yCenter + ySize), (zCenter + zSize))) >= 0.0f)
  125. continue;
  126. return false;
  127. }
  128. return true;
  129. }
  130. }
  131. }