Private Function LineHitsPlane(LineA As D3DVECTOR, LineB As D3DVECTOR, Plan As D3DPLANE, ByRef pvOut As D3DVECTOR) As Boolean
Dim vDir As D3DVECTOR
Dim Denominator As Single
Dim s As Single
D3DXVec3Subtract vDir, LineB, LineA
Denominator = Plan.a * vDir.X + Plan.b * vDir.Y + Plan.c * vDir.z
If Abs(Denominator) < 0.0001 Then
LineHitsPlane = False
Exit Function
End If
s = (-Plan.a * LineA.X - Plan.b * LineA.Y - Plan.c * LineA.z - Plan.d) / Denominator
If (s < 0#) Then
LineHitsPlane = False
Exit Function
End If
If (s > 1#) Then
LineHitsPlane = False
Exit Function
End If
D3DXVec3Scale pvOut, vDir, s
D3DXVec3Add pvOut, pvOut, LineA
LineHitsPlane = True
End Function
Private Function LineHitsTriangle(LineA As D3DVECTOR, LineB As D3DVECTOR, p1 As D3DVECTOR, p2 As D3DVECTOR, p3 As D3DVECTOR) As Boolean
Dim Plan As D3DPLANE
Dim vTemp As D3DVECTOR, vTemp2 As D3DVECTOR, vTemp3 As D3DVECTOR
Dim vInter As D3DVECTOR
D3DXPlaneFromPoints Plan, p1, p2, p3
If Not (LineHitsPlane(LineA, LineB, Plan, vInter)) Then
LineHitsTriangle = False
Exit Function
End If
vTemp3 = MakeVector(Plan.a, Plan.b, Plan.c)
D3DXVec3Subtract vTemp2, p1, p2
D3DXVec3Cross vTemp, vTemp2, vTemp3
D3DXPlaneFromPointNormal Plan, p1, vTemp
If D3DXPlaneDotCoord(Plan, vInter) < 0# Then
LineHitsTriangle = False
Exit Function
End If
D3DXVec3Subtract vTemp2, p2, p3
D3DXVec3Cross vTemp, vTemp2, vTemp3
D3DXPlaneFromPointNormal Plan, p2, vTemp
If D3DXPlaneDotCoord(Plan, vInter) < 0# Then
LineHitsTriangle = False
Exit Function
End If
D3DXVec3Subtract vTemp2, p3, p1
D3DXVec3Cross vTemp, vTemp2, vTemp3
D3DXPlaneFromPointNormal Plan, p3, vTemp
If D3DXPlaneDotCoord(Plan, vInter) < 0# Then
LineHitsTriangle = False
Exit Function
End If
LineHitsTriangle = True
End Function