I have a cube rendering inside a Viewport3D
and i need to know a way to find out whether ALL of the cube is visible to the user.
Edit:Just to be clear,..I'm not talking about clipping because of the near/far plane distance here. I mean the cube is to tall or wide to fit in the cameras field of view.
Any help would be massively appreciated!
Thanks in advance.
I can't offer a solution but I can, perhaps, point you in the right direction.
What you need to get hold of is the extent of the 2D projection of the cube on the view plane. You can then do a simple check on the min and max X & Y values to see whether the whole of the cube is visible.
Adding a tolerance factor to the extent will take care of any rounding errors.
EDIT: I have just done a Google search for "2D projection WPF" and this link came up. It looks like it addresses what you want.
FURTHER EDIT: I've copied the relevant section of code from the above link here.
public static Rect Get2DBoundingBox(ModelVisual3D mv3d)
{
bool bOK;
Matrix3D m = MathUtils.TryWorldToViewportTransform(vpv, out bOK);
bool bFirst = true;
Rect r = new Rect();
if (mv3d.Content is GeometryModel3D)
{
GeometryModel3D gm3d = (GeometryModel3D) mv3d.Content;
if (gm3d.Geometry is MeshGeometry3D)
{
MeshGeometry3D mg3d = (MeshGeometry3D)gm3d.Geometry;
foreach (Point3D p3d in mg3d.Positions)
{
Point3D pb = m.Transform(p3d);
Point p2d = new Point(pb.X, pb.Y);
if (bFirst)
{
r = new Rect(p2d, new Size(1, 1));
bFirst = false;
}
else
{
r.Union(p2d);
}
}
}
}
return r;
}