i have a model that is painted roated an then translaten:

// rotate
glRotatef(x_rot, 1.0, 0.0, 0.0);
glRotatef(y_rot, 0.0, 1.0, 0.0);
glRotatef(z_rot, 0.0, 0.0, 1.0);
// x y z switched since rotated.
glTranslated(z_pos, -y_pos, +x_pos);

now i want to move the view by mouse which is done by this function

void View3D::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton)
{
int dx = event->x() - last_pos.x();
int dy = event->y() - last_pos.y();
x_pos += dx/(80.0*zoom);
y_pos += dy/(80.0*zoom);
updateGL();
}
}

The problem here is that now the movement is done along the “old” unrotated axis and not the rotated ones. Therefore i want to somehow get the unit vectors of the painted model in the actual view.
(and then apply a dot product to the global unitvectors to get the movement)
Is there a out of the box openGl function for this or do i need to calculate them from the angles myself?

Placing the glTranslate() before or after the glRotate() calls affects whether the translation is in object space or view space.

But if you need the transformations for anything other than rendering, or want to perform incremental updates, you’re probably better off avoiding the legacy OpenGL matrix functions. Use e.g. GLM to construct matrices and just upload the final matrix with glLoadMatrix().

void View3D::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton)
{
int dz = event->x() - last_pos.x();
int dy = event->y() - last_pos.y();
Vector3d x(1, 0, 0);
Vector3d y(0, 1, 0);
Vector3d z(0, 0, 1);
// this has to be the oposit order of glRotatef()
rotAboutZ(&x, z_rot);
rotAboutX(&x, x_rot);
rotAboutY(&x, y_rot);
double ndx = -x(1)*dy + x(2)*dz;
// this has to be the oposit order of glRotatef()
rotAboutZ(&y, z_rot);
rotAboutX(&y, x_rot);
rotAboutY(&y, y_rot);
double ndy = -y(1)*dy + y(2)*dz;
// this has to be the oposit order of glRotatef()
rotAboutZ(&z, z_rot);
rotAboutX(&z, x_rot);
rotAboutY(&z, y_rot);
double ndz = -z(1)*dy + z(2)*dz;
x_pos += ndx/(80.0*zoom);
y_pos += ndy/(80.0*zoom);
z_pos += ndz/(80.0*zoom);
updateGL();
}
}