PDA

View Full Version : Normals...



lsdi
04-22-2002, 09:43 AM
Its my calc. correct?




struct quad_vect{
double x;
double y;
double z;
};

quad_vect normalize(quad_vect vert)
{
float len;
quad_vect norm;
len=sqrt(pow(vert.x,2)+pow(vert.y,2)+pow(vert.z,2) );
norm=vert;
if(len>0) {
norm.x/=len;
norm.y/=len;
norm.z/=len;
}
return norm;
}

quad_vect normal(quad_vect vert_1,quad_vect vert_2)
{
quad_vect norm;
norm.x=(vert_1.y*vert_2.z)-(vert_2.y*vert_1.z);
norm.y=(vert_1.z*vert_2.x)-(vert_2.z*vert_1.x);
norm.z=(vert_1.x*vert_2.y)-(vert_2.x*vert_1.y);
return normalize(norm);
}

Tolga Dalman
04-22-2002, 10:52 AM
hi,

hm, to me it looks correct. what exactly is the problem?

Nutty
04-22-2002, 10:59 AM
Dont use "pow", thats looks obscenely slow..

use (vert.x*vert.x) etc.. etc..

Dont pass structures by value, use a reference or a ptr. Same for returning.

Calc len as follows.

len = 1.0f / sqrtf(x*x + y*y + z*z);

then use Mul instead of divide, it's faster.

vert.x *= len;
etc..

Nutty