0

I have a QMainWindow containing two QGLWidgets. The widgets use the default constructor and do not share a context. The widgets both display two dimensional data and allow for clicking and dragging.

This works using gluUnproject and works well on the first QGLWidget that is created. When clicking on the second widget, everything works as expected. However, when dragging the second widget, the movement is not smooth. What's worse is that once the second widget has been dragged, the first widget's gluUnproject is off by some margin that appears related to the direction and distance the second display was dragged.

I have tried calls to makeCurrent() at the start of the drawing method, at the start of the mouse event, and combinations of one, the other, and both and haven't seen any improvement.

void MyGLWidget::paintEvent(QPaintEvent *event){
    glViewport(0, 0, widgetWidth, widgetHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,(GLfloat)((widgetWidth/widgetHeight)),0.1f,-zoomMin);
    glTranslatef(camera.x, camera.y, camera.zoom);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //[GL Drawing code]

    glGetDoublev( GL_MODELVIEW_MATRIX, _modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, _projection );
    glGetIntegerv( GL_VIEWPORT, _viewport );
}
genpfault
  • 47,669
  • 9
  • 68
  • 119

1 Answers1

0

Make sure that the viewport and matrix data you pass to gluUnProject match those of the widget you're interacting with. Ideally you create copies of them at drawing time into some widget class member variable.

datenwolf
  • 149,702
  • 12
  • 167
  • 273
  • I have tried storing the model, projection, and view port matrices at drawing time but get the jerky movement on both widgets with that setup. I get smooth movement by getting the three matrices upon the mouse event. Getting the model and view port at draw time and the projection at mouse event time gets me smooth movement but doesn't fix the original issue. – Alex Barrett Feb 05 '13 at 16:37
  • @AlexBarrett: If you get jerky movement when obtaining the matrices at draw time, you're fetching the matrices at the wrong moment. Please post the code you used so far for this. – datenwolf Feb 05 '13 at 17:24
  • @AlexBarrett: As long as you don't manipulate the matrices within the drawing code the retrieval is in order. **BUT** the projection matrix is not the right place for the view (=camera) transform. The projection is kind of the lens, and applying translations and rotations to the projection is, as if you'd move around your camera's lens while keeping the camera body in place on its tripod. The view (=camera) transformation goes into the model-**view** matrix. – datenwolf Feb 05 '13 at 18:52
  • @datenwolf I have this same issue, can you tell me how to make sure that the viewport and matrix data you pass to gluUnProject match those of the widget you're interacting with? and How you create copies of them at drawing time into some widget class member variable? – Pete Jul 31 '17 at 13:10