I need to make a function:

``````void GenerateNormal(float x[3], float y[3], float z[3], float &ox, float &oy, float &oz)
{
//Calculate coordinates for a glNormal and return them ox,oy and oz
}``````

I have a sinking feeling that the math will be quite difficult. Can anybody help me?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Labdabeta

The math is very simple. Take the difference between two pair of points (like pt2 - pt1, and pt3 - pt1) to get two vectors, take the cross-product of the vectors, and normalize it. That's it. In other words:

``````void GenerateNormal(float x[3], float y[3], float z[3], float &ox, float &oy, float &oz)
{
//get difference vectors:
float dx1 = x[1] - x[0]; float dy1 = y[1] - y[0]; float dz1 = z[1] - z[0];
float dx2 = x[2] - x[0]; float dy2 = y[2] - y[0]; float dz2 = z[2] - z[0];
//cross-product:
ox = dy1 * dz2 - dz1 * dy2;
oy = dz1 * dx2 - dx1 * dz2;
oz = dx1 * dy2 - dy1 * dx2;
//normalize:
float mag = sqrt( ox * ox + oy * oy + oz * oz );
ox /= mag;
oy /= mag;
oz /= mag;
}``````