normal-calculation

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

// 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;

// where is nl coming from?
// shouldnt it be n[0] /= nabs; … ??

Originally posted by 147-2:
[b]
// 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;

// where is nl coming from?
// shouldnt it be n[0] /= nabs; … ??

[/b]

Ah, sorry… mixed versions of the code, nabs was named nl before, but it is named nabs in all places of the real code, so this isn’t the problem, I’ve tried with glEnable(GL_NORMALIZE); on, and the normals are still off.

Hmmm, maybe it helps if you specify the problem more precisely. Is the tria/poly not lit? is the reflection not correct?

My first guess would be, the vectors are not correctly defined. I think it should be anti clockwise. Use the right hand rule to check. But you said that you checked that already…

No clue so far
Regards
xDigital

Originally posted by xDigital:
[b]Hmmm, maybe it helps if you specify the problem more precisely. Is the tria/poly not lit? is the reflection not correct?

My first guess would be, the vectors are not correctly defined. I think it should be anti clockwise. Use the right hand rule to check. But you said that you checked that already…

No clue so far
Regards
xDigital[/b]

The reflection is not correct, drawing the normals yields that they are 90 degrees of, in some axis. The vertices are defined in CCW order, as said, I’ve verified this by drawing the back sides in wire frame.

The normal is in the same plane as the polygon, if the polygon is defined in a plane parallel to the x-y, y-z or x-y planes.

I can probably get a screenshot and post a link to it here if the description was fuzzy.

[This message has been edited by kinslayer (edited 02-24-2004).]

k, try this calculation:

typedef struct{
float x,y,z;
} VECTOR;

VECTOR Crossproduct(VECTOR v0,VECTOR v1){
VECTOR r;
r.x = v0.yv1.z-v0.zv1.y;
r.y = v0.zv1.x-v0.xv1.z;
r.z = v0.xv1.y-v0.yv1.x;
return r;
}

plus your normalization.

Regards
xDigital

Very much thanks, although it did not help. The cross product in my original version also does very well in the unit-test I wrote.

So, my cross product is right on, including the nomalization, but I still get this mysterious bug.

I guess I’m off to more debugging.

Originally posted by xDigital:
[b]k, try this calculation:

typedef struct{
float x,y,z;
} VECTOR;

VECTOR Crossproduct(VECTOR v0,VECTOR v1){
VECTOR r;
r.x = v0.yv1.z-v0.zv1.y;
r.y = v0.zv1.x-v0.xv1.z;
r.z = v0.xv1.y-v0.yv1.x;
return r;
}

plus your normalization.

Regards
xDigital[/b]