Results 1 to 16 of 16

Thread: OpenGL with Graphics View?

  1. #1

    Default OpenGL with Graphics View?

    I am making an application that has a lot of animations going on and would like to make "pure" opengl calls to do this.

    Right now I am using the GraphicsView framework and add a bunch of items to the scene. Some items are simple, like buttons. Others are rotating images, for example I have 25 fans rotating on the screen. For this I am using the time line stuff.

    I already set the viewport to qglwidget but the hardware I run this on still gets bogged down a bit.

    Could I do something like this:

    Qt Code:
    1. MyQGraphicsItem::paint()
    2. {
    3. quad(x1,y1,...)
    4. glRotatef(...)
    5. }
    To copy to clipboard, switch view to plain text mode 

    Or would I have to overpaint in a QGLWidget to do this and not use the graphics view framework?

  2. #2
    Join Date
    Apr 2009
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: OpenGL with Graphics View?

    you can drive a class from QGLWidget and then use this widget in scene->addWidget
    Qt Code:
    1. class myQtWidget: public QGLWIdget
    2. {
    3. public:
    4. myQtWidget();
    5. ~myQtWidget();
    6. protected:
    7. void initializeGL();
    8. void paintGL();
    9. void resizeGL(int w, int h);
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: OpenGL with Graphics View?

    When I add my QGLWidget to the scene, nothing draws.

    A simple example I'm trying to get working is taking the GLWidget from Qt's HelloGL example then doing the following:

    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. GLWidget *glWidget = new GLWidget;
    6.  
    7. scene.addWidget( glWidget );
    8.  
    9. GraphicsView view;
    10. view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    11. view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    12. view.setScene( &scene );
    13. view.show();
    14.  
    15. view.resize(1024, 768);
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    The widget's paintGL() function does get called when I set a breakpoint so it seems like it is trying to draw at least.

    Anyone know what I'm missing?

  4. #4
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL with Graphics View?

    Try subclassing QGraphicsScene and drawing in the overridden QGraphicsScene::drawBackground() function.

    See: http://doc.trolltech.com/qq/qq26-openglcanvas.html

  5. #5
    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: OpenGL with Graphics View?

    Quote Originally Posted by Dutch112 View Post
    Or would I have to overpaint in a QGLWidget to do this and not use the graphics view framework?
    You can make the QGLWidget a viewport of the graphicsview and then use pure GL calls inside the items' paint() routines to do the painting, that's not a problem. You can even mix GL and QPainter based items if you want. Either way be sure to draw in the right coordinates (remember about the bounding rect and transformations applied to items).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6

    Default Re: OpenGL with Graphics View?

    Quote Originally Posted by wysota View Post
    You can make the QGLWidget a viewport of the graphicsview and then use pure GL calls inside the items' paint() routines to do the painting, that's not a problem. You can even mix GL and QPainter based items if you want. Either way be sure to draw in the right coordinates (remember about the bounding rect and transformations applied to items).
    Is this preferable to creating a subclass of QGLWidget and trying to add that widget to the scene?

  7. #7

    Default Re: OpenGL with Graphics View?

    Quote Originally Posted by k2 View Post
    Try subclassing QGraphicsScene and drawing in the overridden QGraphicsScene::drawBackground() function.

    See: http://doc.trolltech.com/qq/qq26-openglcanvas.html
    I tried adding the glWidget to a control and to the scene itself. I also call update() on the widget inside of the scene's drawBackground. Still no luck.

    Tried
    Qt Code:
    1. QWidget *controls = createDialog(tr("Controls"));
    2. glWidget = new GLWidget;
    3. controls->layout()->addWidget(glWidget);
    4. addWidget(controls);
    To copy to clipboard, switch view to plain text mode 

    Tried
    Qt Code:
    1. glWidget = new GLWidget;
    2. addWidget(glWidget);
    To copy to clipboard, switch view to plain text mode 

    Tried
    Qt Code:
    1. void OpenGLScene::drawBackground()
    2. {
    3. glWidget->updateGL();
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: OpenGL with Graphics View?

    Quote Originally Posted by Dutch112 View Post
    Is this preferable to creating a subclass of QGLWidget and trying to add that widget to the scene?
    From what I know this will currently not work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL with Graphics View?

    Quote Originally Posted by Dutch112 View Post
    I tried adding the glWidget to a control and to the scene itself. I also call update() on the widget inside of the scene's drawBackground. Still no luck.

    Tried
    Qt Code:
    1. void OpenGLScene::drawBackground()
    2. {
    3. glWidget->updateGL();
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 
    You no longer use OpenGL commands in the glWidget's paintGL() function (in fact you don't need to subclass QGLWidget at all - one just needs to be set as the GraphicsView's viewport), you do them in drawBackground(). Also, you don't call updateGL(), just call update() inside of OpenGLScene::drawBackground() which will queue another call to drawBackground() (in the example link, they use a QTimer and singleShot to limit the number of update() calls and thus the framerate).

    Add widgets using the OpenGLScene::addWidget() function.

    See the link I provided, it shows how to mix an OpenGL scene with embedded Qt widgets using the GraphicsView framework.

  10. #10
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL with Graphics View?

    Hi!

    I followed the discussion and I am very interested in being able to use OpenGL commands inside the paint() code of the items in my scene. Actually, I have started implementing this, with great success.

    Here's a schematic overview of how I've done:
    Qt Code:
    1. //...
    2. QGraphicsView view(&scene);
    3. //the following line adds the opengl support
    4. view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::Rgba)));
    5.  
    6. //here's my item class
    7. class GLGraphicsItem : public QGraphicsItem
    8. {
    9. //...
    10. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    11. {
    12. Q_UNUSED(option);
    13. Q_UNUSED(widget);
    14. if (painter->paintEngine()->type() != QPaintEngine::OpenGL)
    15. qWarning("OpenGLScene: drawBackground needs a "
    16. "QGLWidget to be set as viewport on the "
    17. "graphics view");
    18. else{
    19. //opengl commands here
    20. }
    21. }
    22. //...
    23. }//end-class
    To copy to clipboard, switch view to plain text mode 
    The ablove code works nicely and does what it shoud - if I just display the view.

    Now I tried to use the view function
    Qt Code:
    1. QGraphicsView::render ( QPainter * painter, const QRectF & target = QRectF(), const QRect & source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio )
    To copy to clipboard, switch view to plain text mode 
    which will output the warning message, i.e. this rendering is performed *without* opengl support.

    I do not need the fastest hardware acceleration for this offscreen rendering at this point, just the opengl commends are very important for me.

  11. #11
    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: OpenGL with Graphics View?

    Where do you render to using the above call?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL with Graphics View?

    You mean the call to QGraphicsView::render() ?
    Well, I want to copy the contents of my scene, or to be more precise: I want to render my scene as it is shown by the view into an image . Eg. a qImage, or a Printjob could be such a target.

    What I found out meanwhile is QGLWidget::grabFramebuffer(), which partly does the job.
    The big disadvantage of this funtion is that it just gives you exactly the resolution of the QGLWidget as it is shown.

  13. #13
    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: OpenGL with Graphics View?

    Render to a QGLFrameBuffer object and then use QGLFrameBuffer::toImage() to convert the result to an image.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: OpenGL with Graphics View?

    Quote Originally Posted by wysota View Post
    From what I know this will currently not work.
    I'm doing to create my game level editor and everything works fine.


    @Dutch112. How are you going to control your animation time? are you going to use QTimer or just a common loop game?

  15. #15
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL with Graphics View?

    Well, that's not so easy.
    First, AFAIK not every system supports these framebuffers. Pls correct me if I'm wrong.

    Secondly, I would have to initialize (initializeGL, resizeGL and paintGL) this new OpenGL object. As I am doing it up to now, all these initialisations are already done for me by the Qt subsystem.
    This means that I would have to ready out the projection and modelview matices everytime and keep a copy of them locally up to date.

  16. #16
    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: OpenGL with Graphics View?

    It's either this or grabbing the widget or forgetting about GL... You can't use GL calls on a non-gl context and if you want a canvas that is different than the widget you have to set it up separately.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Graphics View Panning ,zooming
    By linuxdev in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2008, 07:17
  2. Replies: 4
    Last Post: 5th August 2008, 19:55
  3. Graphics View Event Propagation
    By pherthyl in forum Qt Programming
    Replies: 10
    Last Post: 3rd July 2008, 10:39
  4. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  5. Adding Rectangular overlay on graphics view
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 21st November 2006, 19:42

Tags for this Thread

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.