PDA

View Full Version : What's wrong with my Normal calculating?



IndrekSnt
11-04-2003, 10:56 AM
I have a world that consists of many quads and polygons, it has also some lighting. In order OpenGL to lit everything correctly, it needs normals with glNormal3f(float X,float Y,float Z); Yes, I used it but some walls are lightened up, some not(some are black). http://www.opengl.org/discussion_boards/ubb/frown.gif.?.

Here are my functions:



VPoint cross(VPoint v1,VPoint v2){
VPoint normal;
normal.x=(v1.y*v2.z)-(v1.z*v2.y);
normal.y=(v1.z*v2.x)-(v1.x*v2.z);
normal.z=(v1.x*v2.y)-(v1.y*v2.x); return normal;}
//
float Magn(VPoint n){
float m;
m=(float)sqrt((n.x*n.x)+(n.y*n.y)+(n.z*n.z));
return m;}
//
VPoint Normalise(VPoint n){
float m=Magn(n);
n.x/=m; n.y/=m; n.z/=m;
return n;}
//
VPoint GetNormal(VPoint v[QUAD_P]){
VPoint v1=v[2]-v[0];
VPoint v2=v[1]-v[0];
VPoint n=cross(v1,v2);
n=Normalise(n); return n;}

And here is VPoint structure:



struct VPoint{
float x,y,z;
VPoint(){}
VPoint(float X,float Y,float Z){
x=X; y=Y; z=Z;}
VPoint operator+(VPoint v){
return VPoint(x+v.x, y+v.y, z+v.z);}
VPoint operator-(VPoint v){
return VPoint(x-v.x, y-v.y, z-v.z);}
VPoint operator*(float v){
return VPoint(x*v, y*v, z*v);}
VPoint operator/(float v){
return VPoint(x/v,y/v,z/v);}
};

I think the normal coordinates come wrong when I use GetNormal(); function, how to make it working?

Any suggestions are welcome...

chowe6685
11-04-2003, 11:10 AM
the normal function looks ok, are all of your quads wound the same way?

fuxifuxi
11-04-2003, 11:10 AM
Some of your normals point in the wrong direction. If you'll show them you'll see that they are pointing on the other side of the quad.

The solution is to switch the vector product for those quads (instead of vectProd(v1, v2) call vectProd(v2, v1)), but that is a lame solution. The real problem is that some of your quads are stored CCW and some CW.

IndrekSnt
11-04-2003, 11:22 AM
Well, the cross(v2,v1); changed it a little - now it's a little better. Some quads && polygons normals are still wrong. I use backface culling and I think the error shouldn't be in map.

This here is a picture of what everything looks like now...
http://www.zone.ee/IndrekSyntlk/GLpr_err.jpg

Blaah, I don't mind the image problem anymore...

[This message has been edited by IndrekSnt (edited 11-04-2003).]

11-06-2003, 04:46 AM
Make sure you check all division denominators for 0.