Moohasha
05-13-2006, 04:58 PM
I've created a terrain that is a just grid of points (equally spaced in the x and z directions, y determines height), connected by a line list (to avoid blending of light over sharp edges). To calculate the surface normal for each triangle I pass all three vertices (A, B, and C) to a function, form two vectors (vectors AB and AC) take the cross product, and return the unit vector with the same direction as the result. This seems to be working fine in most cases, but I'm getting some undesired results. It's hard to explain, so please view the attached screenshot to see the problem. Every other triangle has a messed up normal in certain directions, but not others. Here is the function I'm using to create a normal, which I then assign to all three vertices of a surface:
Private Function GetNormal(ByVal A As Vector3, ByVal B As Vector3, ByVal C As Vector3) As Vector3
Dim v1 As New Vector3(B.X - A.X, C.Y - A.Y, C.Z - A.Z)
Dim v2 As New Vector3(C.X - A.X, C.Y - A.Y, C.Z - A.Z)
Dim result As Vector3
result.X = (v1.Y * v2.Z) - (v1.Z * v2.Y)
result.Y = (v1.Z * v2.X) - (v1.X * v2.Z)
result.Z = (v1.X * v2.Y) - (v1.Y * v2.X)
result.Normalize()
Return result
End Function
And to answer the obvious question, I believe I am passing the vertices to the function in the correct order, because all the normals are correct when the surface is flat (see screen shot). Any ideas what's causing the problem??
Private Function GetNormal(ByVal A As Vector3, ByVal B As Vector3, ByVal C As Vector3) As Vector3
Dim v1 As New Vector3(B.X - A.X, C.Y - A.Y, C.Z - A.Z)
Dim v2 As New Vector3(C.X - A.X, C.Y - A.Y, C.Z - A.Z)
Dim result As Vector3
result.X = (v1.Y * v2.Z) - (v1.Z * v2.Y)
result.Y = (v1.Z * v2.X) - (v1.X * v2.Z)
result.Z = (v1.X * v2.Y) - (v1.Y * v2.X)
result.Normalize()
Return result
End Function
And to answer the obvious question, I believe I am passing the vertices to the function in the correct order, because all the normals are correct when the surface is flat (see screen shot). Any ideas what's causing the problem??