Jump to content
  • Advertisement


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

Cross Products

This topic is 6261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I now that the cross product can be used the find a normal when you know 2 verticies but how do you do it? Please tell me in simple terms as i am not excellent at maths Cheers Steve

Share this post

Link to post
Share on other sites
Here is my code that performs the cross product. I hope it helps.


$$Synopsis TPoint TPoint::operator* (const TPoint& tp) const

tp Ref to the operation''s right operand.

$$Returns Returns a TPoint which is the result of the operation.

$$Description TPoint-TPoint cross product operator.

| a , b , c |
(Rx, Ry, Rz) => | x1, y1, z1 |
| x2, y2, z2 |

(Rx, Ry, Rz) => ((y1*z2 - y2*z1), -(x1*z2 - x2*z1), (x1*y2 - x2*y1))

$$Remarks This operator is not inversable, i.e. A X B != B X A.

$$Pre-Conditions None.

$$Post-Conditions None.

$$Exceptions None.

$$See Also None.

TPoint TPoint::operator* ( const TPoint& tp ) const
TPoint result;

result.x = ( y * tp.z - tp.y * z );
result.y = ( tp.x * z - x * tp.z );
result.z = ( x * tp.y - tp.x * y );

return result;

Share this post

Link to post
Share on other sites
edit "HTML TAGS SUCK!" /edit
you actually need 3 vertices. The cross product takes two vectors as input, and returns a vector orthogonal(perpendicular) to both. So what you do is find two vectors in the plane of your polygon by doing P3-P2(the vector from P2 to P3) and P1-P2(the vector from P2 to P1), then take their cross product. For polygons wound counter-clockwise(ie vertices specified CCW), this produces the normal of the polygon. You then need to normalize this to use in lighting calculations.
A normalized vector means that it's magnitude is equal to one(sqrt(x*x+y*y+z*z)==1).
To do this, divide all components by the magnitude. Finally, to get the cross product, you take the determinant of the following matrix:

Where i,j,k are unit vectors. This evaluates to
Where x0,y0,z0 and x1,y1,z1 are the two source vectors for the cross product.

Edited by - sjelkjd on March 21, 2001 6:20:30 PM

Share this post

Link to post
Share on other sites
First you need 3 vectors (a triangle).

let's call them v1, v2 and v3

Now you need to get the two vectors needed for the crossproduct from these three vectors, we do that with simple substraction.

we call the two new vectors b1 and b2.

b1.x = v1.x - v2.x
b1.y = v1.y - v2.y
b1.z = v1.z - v2.z

b2.x = v3.x - v2.x
b2.y = v3.y - v2.y
b2.z = v3.z - v2.z

Now it's time to do the crossing, from this we will get the normal, so we call the resulting vector n.

n.x = b1.y * b2.z - b1.z * b2.y
n.y = b1.z * b2.x - b1.x * b2.z
n.z = b1.x * b2.y - b1.y * b2.x

This normal now has to be normalized in order to get accurate lighting. (set it's length to 1)

Lenght = sqrt(n.x^2 + n.y^2 + n.z^2)

n.x = n.x / Lenght
n.y = n.y / Lenght
n.z = n.z / Lenght

There, now you have a normal.

return 1;

Edited by - panic on March 25, 2001 5:45:32 PM

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
on my site I have a little chunk ''o'' code that does this for you. It actually uses the same meathod as was posted before me. It''s at www.a-tronic.com under downloads or projects. Was that from the superbible?

Share this post

Link to post
Share on other sites
Yes I purchased the super bible and it has a normilizing function and a cross product function. Why do you use the croxs product i know it''s to find a normal. But does it have a better effect or something than using the standred glNormal3f(); is it used for calcuiting a better normal for a smoother lighting effect on triangles

Please Reply


Share this post

Link to post
Share on other sites
Well, you use the cross product to FIND what you are going to put in glNormal3f(); .. glNormal3f() itself doesn''t do much.
It mearly put the normal for storage in a place where glLights can take use of them to compute the lighting.

You say, "well, if I have a cube, I can write glNormal3f(-1,0,0); to ''make'' the normal for the "left" side of the cube, why do I need the cross thingie?"

What if you doesn''t have a cube, but a model of a human face for example. Isn''t it better to let the computer find the normals for you, instead of typing glNormal3f(x,x,x); for each triangle in the model. And even if you did write them all manually (), you would only get flat shading, But if you instead had used the cross product to find your normals you could add a couple lines of code to avarage them to vertex normals for smooth shading.

All this can also be done by openGL by: glEnable(GL_NORMALIZE); (was that right?) .. but this will cause openGL to compute the normals for each frame, and slow things down. So as long your model doesn''t change in shape it''s best to precalculate the normals and then simply transform them with the model (do not scale them though).

Hope this will help

return 1;

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
me again (a-tronic), yea sorry about the e-mail. I thought you were talking about the
function. You use glNormal3f() with the returned normal vector from the cross product function. Also, now that I think about it. The formula is just simple math. Someone made is because it''s common sense. sjelkjd posted an explaination.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!