00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 using System;
00018 using System.Collections.Generic;
00019 using System.Text;
00020 using Microsoft.DirectX;
00021
00022 namespace DXGfxLib
00023 {
00024 public class Frustrum
00025 {
00029 public Matrix matView;
00030
00034 public Matrix matProj;
00035
00039 Microsoft.DirectX.Plane leftPlane;
00043 Microsoft.DirectX.Plane rightPlane;
00047 Microsoft.DirectX.Plane topPlane;
00051 Microsoft.DirectX.Plane bottomPlane;
00055 Microsoft.DirectX.Plane nearPlane;
00059 Microsoft.DirectX.Plane farPlane;
00060
00064 public Microsoft.DirectX.Plane LeftPlane
00065 {
00066 get
00067 {
00068 return leftPlane;
00069 }
00070 set
00071 {
00072 leftPlane = value;
00073 }
00074 }
00075
00079 public Microsoft.DirectX.Plane RightPlane
00080 {
00081 get
00082 {
00083 return rightPlane;
00084 }
00085 set
00086 {
00087 rightPlane = value;
00088 }
00089 }
00090
00094 public Microsoft.DirectX.Plane TopPlane
00095 {
00096 get
00097 {
00098 return topPlane;
00099 }
00100 set
00101 {
00102 topPlane = value;
00103 }
00104 }
00105
00109 public Microsoft.DirectX.Plane BottomPlane
00110 {
00111 get
00112 {
00113 return bottomPlane;
00114 }
00115 set
00116 {
00117 bottomPlane = value;
00118 }
00119 }
00120
00124 public Microsoft.DirectX.Plane NearPlane
00125 {
00126 get
00127 {
00128 return nearPlane;
00129 }
00130 set
00131 {
00132 nearPlane = value;
00133 }
00134 }
00135
00139 public Microsoft.DirectX.Plane FarPlane
00140 {
00141 get
00142 {
00143 return farPlane;
00144 }
00145 set
00146 {
00147 farPlane = value;
00148 }
00149 }
00150
00157 public void ExtractFromMatrices(Matrix matView, Matrix matProj)
00158 {
00159 this.matView = matView;
00160 this.matProj = matProj;
00161 ExtractFromMatrix(Matrix.Multiply(matView, matProj));
00162 }
00163
00170 private void ExtractFromMatrix(Matrix mat)
00171 {
00172 leftPlane.A = mat.M14 + mat.M11;
00173 leftPlane.B = mat.M24 + mat.M21;
00174 leftPlane.C = mat.M34 + mat.M31;
00175 leftPlane.D = mat.M44 + mat.M41;
00176
00177 rightPlane.A = mat.M14 - mat.M11;
00178 rightPlane.B = mat.M24 - mat.M21;
00179 rightPlane.C = mat.M34 - mat.M31;
00180 rightPlane.D = mat.M44 - mat.M41;
00181
00182 topPlane.A = mat.M14 - mat.M12;
00183 topPlane.B = mat.M24 - mat.M22;
00184 topPlane.C = mat.M34 - mat.M32;
00185 topPlane.D = mat.M44 - mat.M42;
00186
00187 bottomPlane.A = mat.M14 + mat.M12;
00188 bottomPlane.B = mat.M24 + mat.M22;
00189 bottomPlane.C = mat.M34 + mat.M32;
00190 bottomPlane.D = mat.M44 + mat.M42;
00191
00192 nearPlane.A = mat.M13;
00193 nearPlane.B = mat.M23;
00194 nearPlane.C = mat.M33;
00195 nearPlane.D = mat.M43;
00196
00197 farPlane.A = mat.M14 - mat.M13;
00198 farPlane.B = mat.M24 - mat.M23;
00199 farPlane.C = mat.M34 - mat.M33;
00200 farPlane.D = mat.M44 - mat.M43;
00201
00202 leftPlane.Normalize();
00203 rightPlane.Normalize();
00204 topPlane.Normalize();
00205 bottomPlane.Normalize();
00206 nearPlane.Normalize();
00207 farPlane.Normalize();
00208 }
00209
00216 public bool IsInFrustrum(BBox bbox)
00217 {
00218 if ((MathUtil.GetPlaneRelativePosition(leftPlane, bbox) == planeRelativePosition.back)
00219 || (MathUtil.GetPlaneRelativePosition(rightPlane, bbox) == planeRelativePosition.back)
00220 || (MathUtil.GetPlaneRelativePosition(topPlane, bbox) == planeRelativePosition.back)
00221 || (MathUtil.GetPlaneRelativePosition(bottomPlane, bbox) == planeRelativePosition.back)
00222 || (MathUtil.GetPlaneRelativePosition(nearPlane, bbox) == planeRelativePosition.back)
00223 || (MathUtil.GetPlaneRelativePosition(farPlane, bbox) == planeRelativePosition.back))
00224 {
00225 return false;
00226 }
00227
00228 return true;
00229 }
00230 }
00231 }