Problem with my normals

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

Rockoon
05-14-2006, 12:12 AM
Half of them seem to be pointing in the wrong direction.

Probably because not all of the triangles vertices are consistently ordered in a clockwise (or counterclockwise) direction.

There are two solutions to a planes normal...

Iceplug
05-14-2006, 06:17 AM
Note DirectX9 has a function that does the Cross product for you:
result = Vector3.Cross(v1, v2)

The (possibly) easiest solution would be to reorder the vertices when you do your tilemap.

Moohasha
05-14-2006, 11:41 AM
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.

Moohasha
05-14-2006, 11:56 AM
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. :p 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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum