I am having trouble with lightening calculation, I have traced the problems to the normal calculation but I can't find any errors with it.

I have polygons defined as either triangles or quads, their vertices are placed in three vectors called p0, p1, p2 and p3.

I have checked to see that the polygons are defined in CCW order and they are.

Any help would be much appreciated.

Here are the lines concerning the normal calculation:

// compute normals, poly vertices defined in CCW order

// n = (p2-p1) x (p0-p1)

a[0] = p2[0] - p1[0];

a[1] = p2[1] - p1[1];

a[2] = p2[2] - p1[2];

b[0] = p0[0] - p1[0];

b[1] = p0[1] - p1[1];

b[2] = p0[2] - p1[2];

n[0] = a[1]*b[2] - a[2]*b[1];

n[1] = a[2]*b[0] - a[0]*b[2];

n[2] = a[0]*b[1] - a[1]*b[0];

// normalize

// n = (nx / sqrt(|n|), ny / sqrt(|n|), nz / sqrt(|n|))

nabs = sqrt(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);

n[0] /= nl; n[1] /= nl; n[2] /= nl;

// Mattias