#### Archived

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

# Cross Products

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

## 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 on other sites
Here is my code that performs the cross product. I hope it helps.

/*$Func************************************************************************ $$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. ******X******************X*******************X*******************X**********$*/
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 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:
|__i__j__k__|
|__x0_y0_z0_|
|__x1_y1_z1_|

Where i,j,k are unit vectors. This evaluates to
(y0*z1-y1*z0,x1*z0-x0*z1,x0*y1-x1*y0)
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 on other sites
What is the '' Then take the cross product part '' how do i perform a cross product???

Cheers

Steve

##### 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 on other sites
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 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

Thanks

Steve

##### 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 on other sites
Yes thanks alot for the help

Anyways does anybody know how you get to the crossproduct i mean where does the formula come from?

Thanks
Steve

##### Share on other sites
me again (a-tronic), yea sorry about the e-mail. I thought you were talking about the
glEnable(GL_NORMALIZE);
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.

-Alex
www.a-tronic.com