rulfen

03-11-2011, 02:13 AM

Hi,

I'm trying to implement a basic trackball navigation based on quaternions, i've found the new orientation but im not sure how to plug it in, I would like to set it to my basic camera, which I have as vectors cameraPos, cameraView and cameraUp that I use with the glulookat() function.

If I assign the quaternion rotation to the vectors I get a trackball rotation, but it gets wrong after a couple of rotations. I guess I need to keep track of the rotation in some way, by setting up a own lookat view matrix and get the rotation matrix from that or save the old orientation? Is it even possible to use the glulookat for this, or need I glMultMatrix(..)/glLoadMatrix(..)?

This is how I setup the rendering

glViewport(0, 0, width, height);

glClearColor(0.f, 0.f, 0.f, 0.f);

// Enable Z-buffer read and write

glEnable(GL_DEPTH_TEST);

// Setup a perspective projection

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

// Camera position, view target, up vector.

gluLookAt( m_cameraPos.x() , m_cameraPos.y() , m_cameraPos.z() ,

m_cameraView.x(), m_cameraView.y(), m_cameraView.z(),

m_cameraUp.x() , m_cameraUp.y() , m_cameraUp.z() );

Here I get the point coords and assign the rotation

Vec3<double> p1 = MapToSphere(oldPoint);

Vec3<double> p2 = MapToSphere(currPoint);

Vec3<double> axis = p2.cross(p1);

double angle = asin(axis.length());

Quaternion<double> rot(axis,angle);

rot.normalize();

m_cameraPos = rotate(m_cameraPos, rot)*5;

m_cameraView = rotate(m_cameraView, rot);

m_cameraUp = rotate(m_cameraUp, rot);

Here is the rotate() function

Quaternion<double> rotconj = rot;

rotconj.conjugate();

Quaternion<double> qTarget(vec.x(),

vec.y(),

vec.z(),

0);

Quaternion<double> qResult = (rot * qTarget) * rotconj;

Vec3<double> vecRes(qResult.x(),qResult.y(),qResult.z());

vecRes.normalize();

return vecRes;

Thanks for your help!

I'm trying to implement a basic trackball navigation based on quaternions, i've found the new orientation but im not sure how to plug it in, I would like to set it to my basic camera, which I have as vectors cameraPos, cameraView and cameraUp that I use with the glulookat() function.

If I assign the quaternion rotation to the vectors I get a trackball rotation, but it gets wrong after a couple of rotations. I guess I need to keep track of the rotation in some way, by setting up a own lookat view matrix and get the rotation matrix from that or save the old orientation? Is it even possible to use the glulookat for this, or need I glMultMatrix(..)/glLoadMatrix(..)?

This is how I setup the rendering

glViewport(0, 0, width, height);

glClearColor(0.f, 0.f, 0.f, 0.f);

// Enable Z-buffer read and write

glEnable(GL_DEPTH_TEST);

// Setup a perspective projection

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

// Camera position, view target, up vector.

gluLookAt( m_cameraPos.x() , m_cameraPos.y() , m_cameraPos.z() ,

m_cameraView.x(), m_cameraView.y(), m_cameraView.z(),

m_cameraUp.x() , m_cameraUp.y() , m_cameraUp.z() );

Here I get the point coords and assign the rotation

Vec3<double> p1 = MapToSphere(oldPoint);

Vec3<double> p2 = MapToSphere(currPoint);

Vec3<double> axis = p2.cross(p1);

double angle = asin(axis.length());

Quaternion<double> rot(axis,angle);

rot.normalize();

m_cameraPos = rotate(m_cameraPos, rot)*5;

m_cameraView = rotate(m_cameraView, rot);

m_cameraUp = rotate(m_cameraUp, rot);

Here is the rotate() function

Quaternion<double> rotconj = rot;

rotconj.conjugate();

Quaternion<double> qTarget(vec.x(),

vec.y(),

vec.z(),

0);

Quaternion<double> qResult = (rot * qTarget) * rotconj;

Vec3<double> vecRes(qResult.x(),qResult.y(),qResult.z());

vecRes.normalize();

return vecRes;

Thanks for your help!