RotatingATextureUsingGetYaw
From Ogre Wiki
Original Author: Joost Passon, Release Date: January 5th, 2006
In this code I'm assuming you want to work with a flat-XZ rotation. You can also do this with a flat-YZ or a flat-XY rotation, but then you will need to use quaternion.getPitch() or quaternion.getRoll().
Also be careful because I think it will not return a value between 90 & -90 but 0 & 180, so then you will need the edit the part where I check for the camera direction.
// Get CamDirection and cancel out Y-Axis and Magnitude
Vector3 vCamDirection = mCamera->getDerivedOrientation() * Vector3::NEGATIVE_UNIT_Z;
vCamDirection.y = 0;
vCamDirection.normalise();
// Get rotations to X-Axis & Z-Axis
Quaternion qRotationToXAxis = vCamDirection.getRotationTo( Vector3::UNIT_X );
Quaternion qRotationToZAxis = vCamDirection.getRotationTo( Vector3::UNIT_Z );
// Get the degrees from both rotations
Real rRotationToXAxis = qRotationToXAxis.getYaw().valueDegrees();
Real rRotationToZAxis = qRotationToZAxis.getYaw().valueDegrees();
Real rRotation = 0;
// Check which direction the camera is looking
// And adjust rRotation if needed
if( rRotationToXAxis >= 0 ) {
rRotation = rRotationToZAxis;
}
else {
if( rRotationToZAxis >= 0 ) {
rRotation = rRotationToZAxis - rRotationToXAxis * 2;
}
else {
rRotation = rRotationToZAxis + rRotationToXAxis * 2;
}
}
// Rotate Texture
TextureUnitState *texture =
eCamView->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0);
texture->setTextureRotate( Degree( -rRotation ) );
In this part:
...
if( rRotationToXAxis >= 0 ) {
rRotation = rRotationToZAxis;
}
else {
if( rRotationToZAxis >= 0 ) {
rRotation = rRotationToZAxis - rRotationToXAxis * 2;
}
else {
rRotation = rRotationToZAxis + rRotationToXAxis * 2;
}
}
...
I'm figuring out if the camera is looking along the Z-Axis (in positive direction) and if so I just use the rotation to the Z-Axis, but when the camera is looking the other way you will have to add or subtract the rotation from the X-Axis to be able to rotate 180 degrees. Whether to add or to subtract depends if you are looking in a positive direction along side the X-Axis or not.
When the camera is pointing in the negative direction of the Z-Axis, rRotationFromZAxis will still give you a value between 90 & -90. Now to compensate for that, you just multiply rRotationsFromXAxis by 2.

