this is some code to make the vectors orthogonal again and generate a valid orientation matrix.
// right-handed orthogonal system// vector basis// ------------------------------// Z = X x Y// Y = Z x X// X = Y x Z// Camera equivalent// -----------------// Up <=> Y// Dir <=> Z// Side <=> XVector& Y = Up;Vector& Z = Dir;Vector X;Matrix3x3 Orientation;// make sure vectors are not parallel// Or the orientation matrix will be completely shagged// -----------------------------------------------------if (fabs(Y * Z) > 0.999999f) // '*' <=> vector dot product{ Assert(false, "Camera Up and dir are parallel!"); // assert // Set Up to an arbitrary vector // ----------------------------- if (fabs(Y.y) < 0.99f) { Y = Vector(0, 1, 0); } else { Y = Vector(1, 0, 0); }}// re-orthogonalise the vector basis// ---------------------------------// Dir and Up have to be of length one beforehand// If not, normalise them.// ---------------------------------X = Y ^ Z; // '^' <=> vector cross productX.Normalise(); // re-normalise (should always be valid thanks to the parallel checks)Y = Z ^ X; // (should always be of length 1).Assert(ApproxEqual(X.LengthSquared(), 1.0f), "Error in Camera re-orthogonalisation");Assert(ApproxEqual(Y.LengthSquared(), 1.0f), "Error in Camera re-orthogonalisation");Assert(ApproxEqual(Z.LengthSquared(), 1.0f), "Error in Camera re-orthogonalisation");// Set the orietnation to the// vector basis (for a row-major matrix system).// --------------------------Orientation.SetRow(0, X);Orientation.SetRow(1, Y);Orientation.SetRow(2, Z);