Archived

This topic is now archived and is closed to further replies.

Moe

Calculating normals? (DX8)

Recommended Posts

Moe    1256
Lets suppose I have 3 vertices that make a triangle. Now lets suppose we don''t know what way the triangle is facing. How would the normals be calculated? Is it something like the crossproduct of the 3 points? Has anyone done this? If so would you mind posting some source?

Share this post


Link to post
Share on other sites
SirKnight    316
Just create two vectors from those 3 points and do the cross product on those two vectors. So if you have 3 points, lets call them P1, P2, and P3. The two vectors V1 and V2 would be found like this:

V1 = P2 - P1
V2 = P3 - P1

Then do the cross product on V1 and V2 and wala, you got your normal. I assume you know how to do simple math on vectors (such as subtracting and adding) and know how, or know where to go to find how, to do the cross product.

-SirKnight

Edited by - SirKnight on March 21, 2001 6:01:22 PM

Share this post


Link to post
Share on other sites
Moe    1256
Actually I am pretty sketchy on things like this...

So to subtract two points I would first subtract the x positions, then the y positions, then the z positions?

Share this post


Link to post
Share on other sites
SirKnight    316
When subtracting two vectors, you subtract the x's, the y's and the z's separately. So if you have these two vectors
V1 = < 4,5,6 > and V2 = < 10, 8, 20 > So if you wanted to subtract these tow vectors like this -> V1 - V2 you would do this:
V3 = < V1.x - V2.x, V1.y - V2.y, V1.z - V2.z > which would be
V3 = < 4 - 10, 5 - 8, 6 - 20 >
= < -6, -3, -14 >

And adding or any other math operation would be done the same way. You keep each component of the vectors together.

-SirKnight

Edited by - SirKnight on March 21, 2001 7:02:21 PM

Share this post


Link to post
Share on other sites
swordfighter    122
Hi!

just bear in mind that the sequence in which you multiply the 2 vectors makes a difference:

V1xV2=(-1)*V2xV1

(I''m really tired atm, so I hope that I wrote the correct thing - anyone correct me if I''m wrong!)

Swordfighter

Share this post


Link to post
Share on other sites
dean_harding    138
FYI, if you want to know which direction the answer to your cross product will be, you use the "right-hand rule" (different to a "left-hand coordinate system", or "right-hand coordinate system)

How this works is, say you''re doing V3 = V1 x V2. Hold your hand out, as if you''re abount to shake hands with someone (Your thumb should be pointing up). Point your fingers in the direction of V1. Now, curl your fingers in the direction of V2. Since your fingers only curl one way, you''ll have to rotate your hand to get them curling in the right direction. Your thumb is now pointing in the direction of V3.

It''s kind of hard to explain in words, a picture is usually better, but I don''t have any on-hand.

Note: There is a different way of doing this which involves pointing your index finger at V1 and your middle finger at V2, but I find this way is too similar to the "left/right-handed coordinate system" rules and gets confusing...

Share this post


Link to post
Share on other sites
Pactuul    122
you have a class vector...it has three variables, x,y,z.
and your triangle has points P1, P2, and P3

to find this x,y,z (the normal of your triangle) you do this.

normal = vec1 x vec2 (reads vector 1 crosses vector 2)

vec1.x = P2.x - P1.x
vec1.y = P2.y - P1.y
vec1.z = P3.z - P1.z
vec2.x = P3.x - P1.x
vec2.y = P3.y - P1.y
vec2.z = P3.z - P1.z
//here''s how to cross vec1 and vec2
normal.x = vec1.y * vec2.z - vec2.y*vec2.z;
normal.y = vec1.z * vec2.x - vec2.z*vec1.x;
normal.z = vec1.x * vec2.y - vec2.x*vec1.y;

so now you have normal to the triangle. this works regardless of api. with directx you would pump this in where you need a normal at. and opengl is the same way.



"The thing I like about friends in my classes is that they can''t access my private members directly."



Share this post


Link to post
Share on other sites
Moe    1256
One last question: Lets suppose I have 3 vertices of a triangle: P1, P2, and P3. Do I have to subtract them in any peticular order to get my vectors or does it matter if I subtract P1 from P3, and P2 from P3?

Share this post


Link to post
Share on other sites