This is the code for IsTriangleVisible:
Code:
private static bool IsTriangleVisible(Triangle<Vertex> check, SharpCamera camera = null)
{
if (camera != null)
Camera = camera;
Vector3 face = Vector3.Transform(AbsoluteVectors.Look, Camera.GetRotation);
double FOVCOS = Math.Cos((double)FieldOfView);
float Distance;
Distance = Vector3.Distance(check.MiddlePoint, Camera.GetPosition);
if (Distance > MaxDistance || Distance < MinDistance)
return false;
if (Vector3.Dot(face, Vector3.Normalize(check.MiddlePoint - Camera.GetPosition)) >= FOVCOS)
return false;
return true;
}
Basically first eliminate them if they are too far, then check if inside FOV. I have no idea how to check for obstruction, if I could there would be no need for the rest of the code after this.
For the sort, I chek distance between two Triangle middle points:
Code:
private static int Closer(Triangle<Vertex> one, Triangle<Vertex> two)
{
float plusone = Vector3.Distance(two.MiddlePoint, Camera.GetPosition);
float minusone = Vector3.Distance(one.MiddlePoint, Camera.GetPosition);
if (plusone > minusone)
return 1;
if (minusone > plusone)
return -1;
return 0;
}
Middle point is simply a Vector3 with
Code:
X=(Corners[0].X+Corners[1].X+Corners[2].X)/3
etc.
SharpCamera is a class I made that lets you rotate around relative axis and get precise view matrices without gimbal locks or quaternion related problems.
I'm quite new in graphics programming, so maybe I don't know some of the language features I should know >.<. If that's the case please do give me a hint and what kind of class I should use.