View Full Version : normal-calculation

kinslayer

02-22-2004, 08:43 AM

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

147-2

02-22-2004, 09:33 PM

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

kinslayer

02-23-2004, 06:23 AM

Originally posted by 147-2:

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

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.

xDigital

02-23-2004, 08:42 AM

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

kinslayer

02-24-2004, 09:27 AM

Originally posted by xDigital:

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

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).]

xDigital

02-24-2004, 09:45 PM

k, try this calculation:

typedef struct{

float x,y,z;

} VECTOR;

VECTOR Crossproduct(VECTOR v0,VECTOR v1){

VECTOR r;

r.x = v0.y*v1.z-v0.z*v1.y;

r.y = v0.z*v1.x-v0.x*v1.z;

r.z = v0.x*v1.y-v0.y*v1.x;

return r;

}

plus your normalization.

Regards

xDigital

kinslayer

02-25-2004, 12:45 AM

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. http://www.opengl.org/discussion_boards/ubb/frown.gif

Originally posted by xDigital:

k, try this calculation:

typedef struct{

float x,y,z;

} VECTOR;

VECTOR Crossproduct(VECTOR v0,VECTOR v1){

VECTOR r;

r.x = v0.y*v1.z-v0.z*v1.y;

r.y = v0.z*v1.x-v0.x*v1.z;

r.z = v0.x*v1.y-v0.y*v1.x;

return r;

}

plus your normalization.

Regards

xDigital

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.