Results 1 to 3 of 3

Thread: getting the unit vectors of the model in the current rotated view

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    5

    getting the unit vectors of the model in the current rotated view

    Hello,

    i have a model that is painted roated an then translaten:
    Code :
        // 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

    Code :
    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?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    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().

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    5
    at GClements

    Thanks for the replie. I now solved it manually

    Code :
    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

    Code :
    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);
    }

Similar Threads

  1. Ray casting with a fixed camera toward a rotated model
    By Alketi in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 05-09-2015, 12:45 AM
  2. model view matrix and view direction
    By sajis997 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-27-2014, 03:24 AM
  3. Replies: 1
    Last Post: 06-02-2006, 11:52 AM
  4. Replies: 2
    Last Post: 01-07-2004, 11:48 PM
  5. compare normals with view vectors
    By Nico in forum OpenGL: Advanced Coding
    Replies: 16
    Last Post: 06-16-2000, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean