View Full Version : Calculating Normals

Zadkiel

12-06-2000, 03:23 AM

Hi, I'm sorry to post this again, but the answer I needed is the maths behind normals, not a routine. I don't program in c so I need the maths or the code (I'll be able to translate it). Prefarably I'd want the method for Quads, but I'll addapt the a method from triangles if thats all you can tell me.

If it helps the only info I have about the quad is its 4 vertices coordinates (x1,y1,z1,x2,y2,z2....)

Thanks in advance, Eoin

Nutty

12-06-2000, 04:25 AM

Right, for quads. First you need to create 2 vectors along the edges of the quad. done via

u.x = v2.x - v1.x

u.y = v2.y - v1.y

u.z = v2.z - v1.z

v.x = v3.x - v1.x

v.y = v3.y - v1.y

v.z = v3.z - v1.z

Now we need to do the cross prodouct on our 2 new vectors.

n.x = (u.y * v.z) - (u.z * v.y)

n.y = (u.z * v.x) - (u.x * v.z)

n.z = (u.x * v.y) - (u.y * v.x)

And hey presto "n" is the normal for the quad. (Not necessarily unit normal, you'll probably want to normalize it if you use it for lighting calcs)

Weather the normal points above or below the quad, is determined by the order of your vertices around it.

That any good?

Nutty http://www.opengl.org/discussion_boards/ubb/cool.gif

pleopard

12-06-2000, 05:22 AM

Bingo Nutty, good explanation. Zadkiel remember though that this is the normal to the facet. If you want to do vertex normals then you will need to do this for each facet joined to the vertex and average the normals. Vertex normals will give you a much smoother surface.

[This message has been edited by pleopard (edited 12-06-2000).]

Punchey

12-06-2000, 06:07 AM

How does one go about "averaging" the normals? I mean, if you have a mesh of a whole bunch of joined tris or quads? What exactly does this mean?

Deiussum

12-06-2000, 06:37 AM

To average the normals you basically just sum them all up then normalize it back to a length of 1. For instance if you have 3 faces sharing a vertex and the face normals are represented by n1,n2,n3...

n.x = n1.x + n2.x + n3.x;

n.y = n1.y + n2.y + n3.y;

n.z = n1.z + n2.z + n3.z;

len = sqrt(n.x*n.x + n.y*n.y + n.z*n.z);

n.x /= len;

n.y /= len;

n.z /= len;

Nutty

12-06-2000, 06:40 AM

I'm not sure for models, but for say some heightmapped ground build up of quads I normally do this. by the way this dont work at edge of the map.. Mostly nowadays you just use the normals exported from Max or another art package.

Take a vertex from a corner of a quad. Say looking down it's the top right one. This vertex will be used by 3 more quads. The quad above, above and right, and right.

For that vertex, create 4 normals from the surrounding quads, Always using your current vertex as the basis for your new vectors.

Then average the 4 new normals, to create the normal for that vertex. Repeat for all the other vertices.

Or something like that...

Nutty

[This message has been edited by Nutty (edited 12-06-2000).]

Zadkiel

12-06-2000, 07:37 AM

Thanks everyone. I've have it working now

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