Results 1 to 19 of 19

Thread: How to speed up a transparent QGraphicsTextItem in Opengl?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Question How to speed up a transparent QGraphicsTextItem in Opengl?

    Hi there!

    I'm trying to increase the rendering speed of a transparent QGraphicsTextItem. Buffering the output in a pixmap using the GraphicsItem CacheMode doesn't work for me, because the scenes perspective changes a lot and with it the background behind the transparent text.

    So I thought of compiling an opengl-displaylist of the QGraphicsTextItem.paint. Did anyone already try that?

    My problem is that before I can call paint myself, I need to setup a QStyleOptionGraphicsItem which seems like a complex undertaking. Is there a simple way?

    Thx in advance!

    Johannes

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

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by JohannesMunk View Post
    I'm trying to increase the rendering speed of a transparent QGraphicsTextItem. Buffering the output in a pixmap using the GraphicsItem CacheMode doesn't work for me, because the scenes perspective changes a lot and with it the background behind the transparent text.
    The background is irrelevant. The cache contains the item only - without its background. The cache would only be useless if you constantly changed items themselves. In other situations at least one of the cache modes should introduce speedup.

    So I thought of compiling an opengl-displaylist of the QGraphicsTextItem.paint. Did anyone already try that?
    That's essentially what Qt does automatically if you use OpenGL viewport and do caching. It keeps the pixmap in memory of your gfx card and just blits it to the framebuffer.

    My problem is that before I can call paint myself, I need to setup a QStyleOptionGraphicsItem which seems like a complex undertaking. Is there a simple way?
    Contents of the style option object depends on the state of the view so the best you can do is to rerun paint everytime this object changes which is basically what Qt already does with caching.
    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.


  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    First of all: Thx for your fast response!

    Quote Originally Posted by wysota View Post
    The background is irrelevant. The cache contains the item only - without its background. The cache would only be useless if you constantly changed items themselves. In other situations at least one of the cache modes should introduce speedup.
    Is this achieved by alphablending the pixmap/texture?

    Quote Originally Posted by wysota View Post
    That's essentially what Qt does automatically if you use OpenGL viewport and do caching. It keeps the pixmap in memory of your gfx card and just blits it to the framebuffer.
    Caching a pixmap is not what I had in mind when I spoke of an OpenGL-Displaylist. I sort of hoped to compile the actual vertex ops etc..

    Quote Originally Posted by wysota View Post
    Contents of the style option object depends on the state of the view so the best you can do is to rerun paint everytime this object changes which is basically what Qt already does with caching.
    Yes, so far that's clear. But if I would want to render the TextItem once outside a paintevent to compile a displaylist myself, I need to pass a valid style option. How do I get that?

    Probably my problem with the cachemodes is that for nearly each repaint (camera position changed) I'm changing the projection of all items. My old 3d mapping thing..

    Where in the Qt sources do I find the caching modes implementation for the opengl painter?

    Thx again!

    Johannes

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

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by JohannesMunk View Post
    Is this achieved by alphablending the pixmap/texture?
    Yep, something like that.

    Caching a pixmap is not what I had in mind when I spoke of an OpenGL-Displaylist. I sort of hoped to compile the actual vertex ops etc..
    The text is most likely rasterized first so you'd get four vertices and a texture. You get the same for a pixmap.

    Yes, so far that's clear. But if I would want to render the TextItem once outside a paintevent to compile a displaylist myself, I need to pass a valid style option. How do I get that?
    You need to fill one properly but then there is a question what would you fill in there. I assure you - you won't do better than what Qt already does in this field. You can't do better than blitting a texture into the buffer.

    Probably my problem with the cachemodes is that for nearly each repaint (camera position changed) I'm changing the projection of all items.
    You should be able to use ItemCoordinateCache with this approach. As long as you modify the transformations of items and not their internals, this should work.

    Where in the Qt sources do I find the caching modes implementation for the opengl painter?
    Either in src/gui/graphicsview/qgraphicsitem* or in src/opengl/qpaintengine_opengl* and src/opengl/qpixmapdata_gl*. I assume the bare caching code is the same regardless of the backend - it just stores an appropriate pixmap. The magic is how the pixmap works.
    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.


  5. The following user says thank you to wysota for this useful post:

    JohannesMunk (27th October 2009)

  6. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Thx for your help, but using ItemCoordinateCache just gives me a solid black background. I suppose when the text-background is cached my background still is black.

    I will investigate this further, when there is more time for it. Just thought I might ask and rule out any obvious solutions.

    Gn8!

    Johannes

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by JohannesMunk View Post
    Thx for your help, but using ItemCoordinateCache just gives me a solid black background.
    How exactly do you activate the cache?
    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.


  8. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    In the constructor of my ProjectionItem I create the textchilditem:

    Qt Code:
    1. _textitem = new QGraphicsTextItem(text,this);
    2. ...
    3. _textitem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    And what is the boundingRect() of the item?

    Edit: I'm not able to reproduce the behaviour you observe using the following code. Can you change it so that it reproduces your problem?

    Qt Code:
    1. #include <QtGui>
    2. #include <QGLWidget>
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. scene.setBackgroundBrush(Qt::blue);
    7. view.setScene(&scene);
    8. view.setViewport(new QGLWidget);
    9. QString txt = "Some text";
    10. QGraphicsTextItem *item = scene.addText(txt);
    11. QFont f;
    12. f.setPointSize(36);
    13. item->setFont(f);
    14. item->setFlag(QGraphicsItem::ItemIsMovable);
    15. item->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    16. QTransform trans;
    17. trans.rotate(60, Qt::YAxis);
    18. item->setTransform(trans);
    19. item->setOpacity(0.7);
    20. view.show();
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 28th October 2009 at 00:41.
    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.


  10. The following user says thank you to wysota for this useful post:

    JohannesMunk (28th October 2009)

  11. #9
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    The BoundingRect for the ProjectedItem is

    QPainterPath CGL3dProjectedItem::shape() const
    {
    QPainterPath p;
    QPolygonF polygon = mapToScene(this->childrenBoundingRect());
    p.addPolygon(polygon);
    return p;
    }
    virtual QRectF boundingRect() const {return _bounds;}

    in UpdateTransformation:
    ..
    _bounds = shape().boundingRect();
    ..

    The black area is fine. Exactly the area of the textitem. And the text shows as it should with transparent background as soon as I disable the caching.

    I do really appreciate your efforts!

  12. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    I have seen your example. Added a gradient to the background and it still works. Amazing :->

    I don't know exactly, where my problem comes in. With the transformation or the way I'm drawing the background. I am drawing the background directly with gl. Maybe thats the problem.

    I will implement something simple but similiar and report back.

  13. #11
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Well the GL Background isn't it:

    Qt Code:
    1. #include <QtGui>
    2. #include <QGLWidget>
    3.  
    4. class CGLScene : public QGraphicsScene
    5. {
    6. protected:
    7. void drawBackground(QPainter *painter, const QRectF &rect)
    8. {
    9. glClearColor(1.0,0.0,0.0,1.0);
    10. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    11. }
    12. };
    13.  
    14. int main(int argc, char **argv){
    15. QApplication app(argc, argv);
    16. CGLScene scene;
    17.  
    18. view.setScene(&scene);
    19. view.setViewport(new QGLWidget);
    20. QString txt = "Some text";
    21. QGraphicsTextItem *item = scene.addText(txt);
    22. QFont f;
    23. f.setPointSize(36);
    24. item->setFont(f);
    25. item->setFlag(QGraphicsItem::ItemIsMovable);
    26. item->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    27. QTransform trans;
    28. trans.rotate(60, Qt::YAxis);
    29. item->setTransform(trans);
    30. item->setOpacity(0.7);
    31. view.show();
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

    Problem is, introducing the perspective would blow this up a lot. We would need the camera, the projecteditem etc.. I could integrate the TextItem into the ProjectionDemo I did some time ago.. But than it would be a lot of code for you to read..

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Your implementation of shape() and boundingRect() is awfully slow. You are creating painter paths all the time. Cache all the data you can (meaning the shape and the bounding rect), this should already give you some speed improvement.

    BTW. The background shouldn't influence the contents of the item's cache... I suspect this is caused by an incorrect result of boundingRect() at the time the cache is being enabled.
    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.


  15. #13
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by wysota View Post
    Your implementation of shape() and boundingRect() is awfully slow. You are creating painter paths all the time. Cache all the data you can (meaning the shape and the bounding rect), this should already give you some speed improvement.
    Yes, I know. But that's definitely not the speed problem. It makes no difference if I have just one TextItem (thus only one transformation, shape, boundingrect calculation) with a lot of text or lots of textitems with just one line of text. Both are equally slow.

    Quote Originally Posted by wysota View Post
    BTW. The background shouldn't influence the contents of the item's cache... I suspect this is caused by an incorrect result of boundingRect() at the time the cache is being enabled.
    That is definitely the case! The cache is enabled in the constructor. The boundingrect changes all the time. Through the perspective change..

    We are on to something here :->

Similar Threads

  1. OpenGL and Qt Question
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2009, 19:04
  2. Does OpenGL speed up the loading image?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2008, 10:26
  3. QMatrix rotate on QGraphicsTextItem speed on render..
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 22nd December 2007, 20:46
  4. OpenGL app at full speed? Can you do it?
    By mattropolis in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2007, 11:54
  5. Qtopia Core & OpenGL ES?
    By zelko in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 28th May 2007, 08:21

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
  •  
Qt is a trademark of The Qt Company.