PDA

View Full Version : Quaternions rotation

AlexArd
04-30-2011, 11:53 PM
Hello,

I want to draw a cube on (0,0,0) coordinates and to rotate on (0,0,0)-(1,1,-1) axis. When i use this code it works fine:

see the video: http://www.dailymotion.com/video/xihlz8_1

void C_Shape::v_Spin()
{
glRotatef(m_xSpinAngle, 1, 1, -1); //m_xSpinAngle is incremedted with 0.05f on other thread
glRotatef(m_ySpinAngle, 1, 1, -1); //m_ySpinAngle is incremedted with 0.05f on other thread
glRotatef(m_zSpinAngle, 1, 1, -1); //m_ZSpinAngle is incremedted with 0.05f on other thread
}

Now i want to use quaternions rotation, but the rotation is very strange. Can someone give me a hint ?

http://www.dailymotion.com/video/xihlzi_2

void C_Shape::v_Spin()
{

glQuaternion q;
float Matrix[16];
m_qX.CreateFromAxisAngle(m_xSpinAngle, 1.0f, 0.0f, 0.0f);
m_qY.CreateFromAxisAngle(m_ySpinAngle, 0.0f, 1.0f, 0.0f);
m_qZ.CreateFromAxisAngle(m_zSpinAngle, 0.0f, 0.0f, 1.0f);

q = m_qX * m_qY * m_qZ;
q.CreateMatrix(Matrix);

glMultMatrixf(Matrix);

}

void glQuaternion::CreateFromAxisAngle(float degrees, float x, float y, float z) {

float angle = float((degrees / 180.0f) * PI);

float result = (float)sin( angle / 2.0f );

m_w = (float)cos( angle / 2.0f );

m_x = float(x * result);
m_y = float(y * result);
m_z = float(z * result);

//#]
}

void glQuaternion::CreateMatrix(float * pMatrix) {

if(!pMatrix) return;

// First row
pMatrix[ 0] = 1.0f - 2.0f * ( m_y * m_y + m_z * m_z );
pMatrix[ 1] = 2.0f * (m_x * m_y + m_z * m_w);
pMatrix[ 2] = 2.0f * (m_x * m_z - m_y * m_w);
pMatrix[ 3] = 0.0f;

// Second row
pMatrix[ 4] = 2.0f * ( m_x * m_y - m_z * m_w );
pMatrix[ 5] = 1.0f - 2.0f * ( m_x * m_x + m_z * m_z );
pMatrix[ 6] = 2.0f * (m_z * m_y + m_x * m_w );
pMatrix[ 7] = 0.0f;

// Third row
pMatrix[ 8] = 2.0f * ( m_x * m_z + m_y * m_w );
pMatrix[ 9] = 2.0f * ( m_y * m_z - m_x * m_w );
pMatrix[10] = 1.0f - 2.0f * ( m_x * m_x + m_y * m_y );
pMatrix[11] = 0.0f;

// Fourth row
pMatrix[12] = 0;
pMatrix[13] = 0;
pMatrix[14] = 0;
pMatrix[15] = 1.0f;

}

glQuaternion glQuaternion::operator*(glQuaternion q) {

glQuaternion r;

r.m_w = m_w*q.m_w - m_x*q.m_x - m_y*q.m_y - m_z*q.m_z;
r.m_x = m_w*q.m_x + m_x*q.m_w + m_y*q.m_z - m_z*q.m_y;
r.m_y = m_w*q.m_y + m_y*q.m_w + m_z*q.m_x - m_x*q.m_z;
r.m_z = m_w*q.m_z + m_z*q.m_w + m_x*q.m_y - m_y*q.m_x;

return(r);
//#]
}

glQuaternion::glQuaternion() : m_w(1.0f), m_x(0.0f), m_y(0.0f), m_z(0.0f) {
//#[ operation glQuaternion()
//#]
}

SpiderPig500
05-03-2011, 03:33 AM
void C_Shape::v_Spin()
{
glRotatef(m_xSpinAngle, 1, 1, -1); //m_xSpinAngle is incremedted with 0.05f on other thread
glRotatef(m_ySpinAngle, 1, 1, -1); //m_ySpinAngle is incremedted with 0.05f on other thread
glRotatef(m_zSpinAngle, 1, 1, -1); //m_ZSpinAngle is incremedted with 0.05f on other thread
}

What exactly are you trying to achieve here?
Are you trying to rotate the current object by a certain x,y and z amount?