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:

Code:

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??

__________________ "The whole trouble comes from the fact that there is so much tinkering with software. It is not made in a clean fabrication process, which it should be. What we need, is software engineering." — F.L. Bauer, 1968

I'm pretty sure I ordered all the vertices in the same direction. When I enable culling they either all render or all don't. And besides, it's only half when the "hill" or angle is in a certain direction. None of the surfaces on the right side facing left are lit wrong, only ones facing right.
I've tried passing them to my GetNormals() function in different orders, but that either resulted in no effect (generating the same normal from different vectors), or getting the normal pointing in the opposite direction.
I thought DX9 might have something to calculate the cross product for you. I'll try using that.

__________________ "The whole trouble comes from the fact that there is so much tinkering with software. It is not made in a clean fabrication process, which it should be. What we need, is software engineering." — F.L. Bauer, 1968

Once again, it's another PEBKAC error. (Problem Exists Between Keyboard And Chair). If you notice my GetNormal() function, I mixed up B and C when calculating v1. I didn't notice this until I started to replace those two lines with a Vector3.Subctract() function. Fixing that took care of everything. Thanks for the help guys.

__________________ "The whole trouble comes from the fact that there is so much tinkering with software. It is not made in a clean fabrication process, which it should be. What we need, is software engineering." — F.L. Bauer, 1968

The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET. subscribe