I've got a flying camera with some arbitrary rotation. (It also has a translation, but that's not important.) It's flying along its local Z axis, traveling in some direction over the ground with some rate of climb or descent.
I'd like to gently correct it so the horizon is level. Or, equivalently, I'd like to apply rotation around camera's pointing direction (Z) until it's up-vector (Y) is as up as it can be.
That is, I want my camera X and camera Y to adjust, leaving camera Z (forward) exactly the same.
The rotation is represented as a 3x3 matrix. Quaternion solution would be fine, also...
How much do I need to rotate it to get the horizon level?
Any help appreciated!
Edit: I'm currently in c++.
Edit2: Here's a picture further clarifying... I need to rotate in the local XY plane, the grey disk, til X touches the ground plane, Y is up-ish, and Z is unchanged... which sort of suggests an approach...