Normals Calc

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 = (v2u3)-(v3u2)
b = -(v1u3)+(u1v3)
c = (v1u2)-(v2u1)

Normalização:
len=sqrt(nxnx+nyny+nznz)
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.zvert_2.y);
norm.y=-(vert_1.x
vert_2.z)+(vert_2.xvert_1.z);
norm.z=(vert_1.x
vert_2.y)-(vert_1.yvert_2.x);
len=sqrt(norm.x
norm.x+norm.ynorm.y+norm.z+norm.z);
if(len>0) {
norm.x
=1/len;
norm.y*=1/len;
norm.z*=1/len;
}
return norm;
}

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

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! :slight_smile:

Originally posted by Gavin:
[b]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! :-)[/b]

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

A division its a liltle slower than a multiplication. This is why im using *=1/len
instead of /=

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.