Rotating volume texture

Started by
1 comment, last by Aressera 15 years, 5 months ago
Hello everyone. I'm rendering the volume (IDirect3DVolumeTexture9). To rotate it I use texture transform matrix which is based on quaternions. Everything works fine except that texture rotates around it's local axis but I need it to rotate around the global axis. For example I render the human's head and by default the camera is directed to human's left ear. I say "Rotate around Z axis" (nod the head) and it nods. But when I rotate the head around Y axis and then I say "Rotate around Z axis" I expect the head to tilt towards me but it doesn't - the head nods. The reason for this is quite obvious. When I rotate the texture it's local axis also rotate. But does anybody know what I should do to make my head rotate around global axis? That mean when I say "Rotate around Y axis" - the head must rotate around global Y axis but not around the local one. I have to simulate camera rotation around the head. Here's the code which allows the head to rotate around it's local axis:


//Gets texture transformation matrix.
D3DXMATRIX MyTexture::GetTransformMatrix()
{
    D3DXMATRIX result;
    D3DXMATRIX transformMatrix;

    //Current texture rotation.
    D3DXQUATERNION quat;
    D3DXQuaternionRotationYawPitchRoll(&quat, D3DXToRadian(m_YawOffset), 
    D3DXToRadian(m_PitchOffset), D3DXToRadian(m_RollOffset));
    m_TransformQuaternion *= quat; //m_TransformQuaternion stores texture     rotation parameters and is updated every time when camera is moved.

    //Because this method is called every time when WM_PAINT
    //these variables need to be set to NULL. Otherwise the camera 
    //will rotate on each WM_PAINT
    m_YawOffset = 0.0f;
    m_PitchOffset = 0.0f;
    m_RollOffset = 0.0f;

    //Converting from quaternion to matrix
    D3DXMatrixRotationQuaternion(&transformMatrix, &m_TransformQuaternion);
    result = transformMatrix;

    return result;
}


Advertisement
I couldn't follow your code out of context, but it sounds like you might just be doing the rotations in the wrong order. Remember that for 3D rotations, a * b is not the same as b * a.
The Trouble With Robots - www.digitalchestnut.com/trouble
You should eliminate the use of Euler angles wherever you can. They cannot fully describe 3D rotations and you will run into problems like gimbal lock, etc. Use either 3x3 matrices or quaternions to represent rotations in 3D. Basically every object has a 3x3 rotation matrix R.

When rotating about each axis:

R = Rx * R (Rx is an x axis rotation matrix)
R = Ry * R (Ry is a y axis rotation matrix)
R = Rz * R (Rz is a z axis rotation matrix)

Depending on which order you apply these transformations, you will get different results. The difference between this approach and the approach you are currently using is that Euler angles will rotate the object but each rotation (x, y, or z) will relative to the previously applied rotation, while the scheme I present will always perform the rotations relative to the global axes.

Note: don't forget to orthonormalize your rotation matrix R every few rotations or else it will become non-orthonormal due to precision errors.

This topic is closed to new replies.

Advertisement