PDA

View Full Version : Normals Calc



lsdi
02-19-2002, 08:46 PM
This way to calc normals is the correct way?





typedef struct fvect{
float x;
float y;
float z;
};

fvect cnormal(fvect vert_1,fvect vert_2)
{
/*
Produto vetorial: <v1,v2,v3> e <u1,u2,u3> = <a, b, c> onde:
a = (v2*u3)-(v3*u2)
b = -(v1*u3)+(u1*v3)
c = (v1*u2)-(v2*u1)

Normalização:
len=sqrt(nx*nx+ny*ny+nz*nz)
if (len>0) {
nx*=(1/len)
ny*=(1/len)
nz*=(1/len)
*/
fvect norm;
float len;
norm.x=(vert_1.y*vert_2.z)-(vert_1.z*vert_2.y);
norm.y=-(vert_1.x*vert_2.z)+(vert_2.x*vert_1.z);
norm.z=(vert_1.x*vert_2.y)-(vert_1.y*vert_2.x);
len=sqrt(norm.x*norm.x+norm.y*norm.y+norm.z+norm.z );
if(len>0) {
norm.x*=1/len;
norm.y*=1/len;
norm.z*=1/len;
}
return norm;
}

Bob
02-20-2002, 01:24 AM
Doesn't look too bad to me. But why do you change the sign of the Y component in the normal? The signs should be like in the X and Z component.

[This message has been edited by Bob (edited 02-20-2002).]

Gavin
02-20-2002, 01:38 AM
Just by the by....

x *= 1/y
is the same as
x /=y

not being picky just the way I do it and you type 2 less characters! :-)

satan
02-20-2002, 01:55 AM
Originally posted by Gavin:
Just by the by....

x *= 1/y
is the same as
x /=y

not being picky just the way I do it and you type 2 less characters! :-)

yeah and after u used x,y and z as variable names you go on and name your other vars a,b,c... so you need only one character for any variable and not those damn long character names http://www.opengl.org/discussion_boards/ubb/wink.gif
not too serious but this is just one thing why i hate c/c++ so much

but anybody shall use what (s)he feels comfy with

lsdi
02-20-2002, 09:25 AM
A division its a liltle slower than a multiplication. This is why im using *=1/len
instead of /= http://www.opengl.org/discussion_boards/ubb/smile.gif

chxfryer
02-20-2002, 09:48 AM
But you are still doing a division with
blah *= 1/otherblah
this would be actually slower because you are doing the division and then another operation multiplication. if you used
1/otherblah more than once, precalculate it then use the multiplication way.