public void IntersectFaces(Ray ray)

{

float t, lt;

lt = float.PositiveInfinity;

Vector3 hitloc;

for(int i=0;i<_Model.Length/3;i++)

{

//_Model holds my vertices.

bool intersects = FindRayTriangleIntersection(ray,10,ref _Model[i*3].Position , ref _Model[i * 3+1].Position , ref _Model[i * 3+2].Position ,out hitloc,out t);

if(intersects&&t<lt) //only select the face that is closest.

{ //t is the distance to this intersection

lt = t; //lt holds the last distance measured.

CurrentFace = i;

}

}

}

private bool FindRayTriangleIntersection(Ray ray, float maximumLength, ref Vector3 a, ref Vector3 b, ref Vector3 c, out Vector3 hitLocation, out float t)

{

hitLocation = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);

Vector3 rayDirection = ray.Direction;

Vector3 rayOrigin = ray.Position;

t = float.NegativeInfinity;

Vector3 vector = b - a;

Vector3 vector2 = c - a;

Vector3 vector4 = Vector3.Cross(rayDirection, vector2);

float num = Vector3.Dot(vector, vector4);

if ((num > -1E-07f) && (num < 1E-07f))

{

return false;

}

float num2 = 1f / num;

Vector3 vector3 = rayOrigin - a;

float num3 = Vector3.Dot(vector3, vector4) * num2;

if ((num3 < -0.01f) || (num3 > 1.01f))

{

return false;

}

Vector3 vector5 = Vector3.Cross(vector3, vector);

float num4 = Vector3.Dot(rayDirection, vector5) * num2;

if ((num4 < -0.01f) || ((num3 + num4) > 1.01f))

{

return false;

}

t = Vector3.Dot(vector2, vector5) * num2;

if ((t > maximumLength) || (t < 0f))

{

t = float.NegativeInfinity;

return false;

}

hitLocation = rayOrigin + (t * rayDirection);

return true;

}