Results 1 to 10 of 10

Thread: Problem when mixing openGL and QPainter

  1. #1
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Problem when mixing openGL and QPainter

    hi,

    I am using openGL selection and picking mechanism in QGLWidget in order to identify the objects under cursor at mouse press.

    It works fine if i only used openGL.

    But further i mixed openGL and QPainter (like overpainting example of Qt examples) in the samr application,selection and picking mechanism is not working.

    I am using Qt 4.4 preview edition.

    Can any tell me is it possible or not.
    Or else anything i m doing wrong.

    regards,
    ~sanjay

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when mixing openGL and QPainter

    Could we see some related code?

  3. #3
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem when mixing openGL and QPainter

    here is the related code.....

    In order to mix QPainter and openGL i have reimplemented paintEvent().

    Qt Code:
    1. void QPyramidTool::paintEvent(QPaintEvent *event)
    2. {
    3.  
    4. QPainter painter;
    5. painter.begin(this);
    6.  
    7. resizeGL(width(),height());
    8. // Rotate the Pyramid and update.
    9. glClearColor(2.0, 2.0, 2.0, 0.0);
    10. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    11. glLoadIdentity();
    12. glTranslated(0.0, 0.0,-10.0);
    13. glRotated(m_nXRot / 10.0, 2.0, 0.0, 0.0);
    14. glRotated(m_nYRot / 10.0, 0.0, 2.0, 0.0);
    15. glRotated(m_nZRot / 10.0, 0.0, 0.0, 2.0);
    16.  
    17. glCallList(m_glObject);
    18.  
    19. event->accept();
    20. }
    21. and the resizeGL() is
    22. void QPyramidTool::resizeGL(int width, int height)
    23. {
    24. int side = qMin(width, height);
    25. glViewport((width - side) / 2, (height - side) / 2, side, side);
    26.  
    27. glMatrixMode(GL_PROJECTION);
    28. glLoadIdentity();
    29. glOrtho(-3.5, +3.5, +3.5, -3.5, 4.0, 20.0);
    30. glMatrixMode(GL_MODELVIEW);
    31. }
    To copy to clipboard, switch view to plain text mode 

    regards,
    sanjay
    Last edited by wysota; 17th March 2008 at 14:15. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when mixing openGL and QPainter

    1. What about the selection code?
    2. Why do you initialize the painter if you don't even use it?
    3. You should use OpenGL code in QGLWidget::paintGL() and not paintEvent().

  5. #5
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem when mixing openGL and QPainter

    I have to use QPainter for painting on this QGLWidget,so i have reimpleneted paintEvent().
    For reference you can see the overpainting example from Qts examples where they have reimplemented paintEvent() and used QPainter and openGL.

    regards,
    sanjay

  6. #6
    Join Date
    Feb 2008
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem when mixing openGL and QPainter

    Yeah, using paintEvent is fully valid.

    Anyway, Qt uses OpenGL for painting primitives as well, when you "mix OpenGL and QPainter". Keep that in mind. It doesn't always return to your painting method with the same state as before (altough it tries to).

    The use of OpenGL selection/picking modes is not recommended though. For once, they are more or less deprecated (OpenGL3 won't have them, IIRC), and they're becoming very badly supported by some vendors, specifically by ATI.

    If you would like to implement picking I would recommend to either use custom color coding or ray picking with intersection tests. You can find code for that about anywhere on the web (look for basic raytracing, it's almost the same), it's even easier than the picking mode when you get the hang of it.

    The stencil buffer is used/cleared interally by the Qt painter as well, afaik, so I'm not sure if that's usable when you mix the two.

    As for why it doesn't work.. can you post the rest of the code? I'm not seeing any picking/selection-related calls to OpenGL in the code you posted. As far as I can see you're only rendering an object.

  7. #7
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem when mixing openGL and QPainter

    thanks a lot.
    i am not ware of the other techniques for picking.but as u suggest i will go through it.
    here is the related code for the selection and picking mechanism.

    following code i have used on mousePressEvent().

    Qt Code:
    1. makeCurrent();
    2. GLuint selectBuf[BUFSIZE];
    3. GLint hits;
    4. GLint viewport[4];
    5.  
    6.  
    7. glSelectBuffer (BUFSIZE, selectBuf);
    8. glGetIntegerv(GL_VIEWPORT, viewport);
    9. glRenderMode (GL_SELECT);
    10.  
    11. glInitNames();
    12. glPushName(0);
    13.  
    14. glMatrixMode (GL_PROJECTION);
    15. glPushMatrix ();
    16. glLoadIdentity ();
    17.  
    18. gluPickMatrix((GLdouble) x, (GLdouble)(viewport[3] - y),
    19. 2.0, 2.0, viewport);
    20. glOrtho(-3.5, +3.5, +3.5, -3.5, 2.0, 15.0);
    21.  
    22. glMatrixMode (GL_MODELVIEW);
    23. glLoadName(1);
    24. glBegin(GL_TRIANGLES);
    25. glVertex3d(0.0, 2.0, 0.0);
    26. glVertex3d(-2.0,-2.0, 2.0);
    27. glVertex3d(2.0,-2.0, 2.0);
    28. glEnd();
    29.  
    30. glLoadName(2);
    31. glBegin(GL_TRIANGLES);
    32. glVertex3d(0.0,2.0,0.0);
    33. glVertex3d(2.0,-2.0,2.0);
    34. glVertex3d(2.0,-2.0,-2.0);
    35. glEnd();
    36.  
    37. glLoadName(3);
    38. glBegin(GL_TRIANGLES);
    39. glVertex3d(0.0,2.0,0.0);
    40. glVertex3d(2.0,-2.0,-2.0);
    41. glVertex3d(-2.0,-2.0,-2.0);
    42. glEnd();
    43.  
    44. glLoadName(4);
    45. glBegin(GL_TRIANGLES);
    46. glVertex3d(0.0,2.0,0.0);
    47. glVertex3d(-2.0,-2.0,-2.0);
    48. glVertex3d(-2.0,-2.0, 2.0);
    49. glEnd();
    50.  
    51.  
    52. glLoadName(5);
    53. glBegin(GL_QUADS);
    54. glVertex3d(2.0,-2.0,2.0);
    55. glVertex3d(-2.0,-2.0,2.0);
    56. glVertex3d(-2.0,-2.0,-2.0);
    57. glVertex3d(2.0,-2.0,-2.0);
    58. glEnd();
    59.  
    60. glMatrixMode(GL_PROJECTION);
    61. glPopMatrix();
    62. glFlush();
    63.  
    64. hits = glRenderMode (GL_RENDER); // Returns no.of mouse hits to each face.
    To copy to clipboard, switch view to plain text mode 

    regards,
    sanjay
    Last edited by jpn; 20th March 2008 at 11:45. Reason: missing [code] tags

  8. #8
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem when mixing openGL and QPainter

    can any body help me in this.

    regards,
    ~sanjay

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when mixing openGL and QPainter

    Hmm... why are you drawing in mousePressEvent? The event should handle mouse events, not draw anything. If the state of your widget changes as a result of mouse event handler, you should call updateGL() or update() to redraw it.

  10. #10
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem when mixing openGL and QPainter

    Actually in mousePressEvent() i am not drawing anything.I am just loading the names for the primitive objects on the stack.It does not draw anything it just enters into the selection mode and loads the name on to the stack.

    please can you explain more.

    regards,
    ~sanjay

Similar Threads

  1. qpainter "drawText() problem
    By impeteperry in forum Qt Programming
    Replies: 10
    Last Post: 25th March 2007, 00:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.