PDA

View Full Version : getting the unit vectors of the model in the current rotated view



gde230
03-29-2017, 09:50 AM
Hello,

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?

GClements
03-29-2017, 04:56 PM
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().

gde230
03-30-2017, 04:54 AM
at GClements

Thanks for the replie. I now solved it manually



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();
}
}


with some rotation matrices



void rotAboutX(Vector3d* v, double angle)
{
Matrix3d m = Matrix3d::Identity();

double sin_val = sin(angle/180*M_PI);
double cos_val = cos(angle/180*M_PI);
m(1,1) = cos_val;
m(2,1) = sin_val;
m(1,2) = -sin_val;
m(2,2) = cos_val;

*v = m*(*v);
}