Results 1 to 10 of 10

Thread: beginNativePainting() with QPainter's shader (for transformation and opacity purpose)

  1. #1
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1

    Question beginNativePainting() with QPainter's shader (for transformation and opacity purpose)

    I created a object inherited from QGraphicsItem and reimplement the paint() function.
    Inside the paint() function, I draw something by QPainter and call painter->beginNativePainting() to draw some elements with native openGL command.
    It all works well, but when I rotate the object( object->rotate(45)), only the elements drawn by QPainter is rotated, the elements drawn by openGL command doesn't work.
    It seems the QPainter use the shader to do those transformation(rotate), how can I apply the shader to my openGL elements?

    Sample Code:
    Qt Code:
    1. class MyItem:public QGraphicsItem
    2. {
    3. public:
    4. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    5. {
    6. painter->drawText(0, 0, "123");
    7. painter->fillRect(0, 0, 128, 128, Qt::green);
    8. painter->beginNativePainting();
    9.  
    10. glEnable(GL_SCISSOR_TEST);
    11. glScissor(0, 0, 64, 64);
    12.  
    13. glClearColor(1, 0, 0, 1);
    14. glClear(GL_COLOR_BUFFER_BIT);
    15.  
    16. glDisable(GL_SCISSOR_TEST);
    17.  
    18. glEnd();
    19. painter->endNativePainting();
    20. }
    21. QRectF boundingRect(void) const
    22. {
    23. return QRectF(0, 0, 100, 100);
    24. }
    25. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 7th September 2011 at 22:19. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2011
    Location
    Ukraine, Kharkov
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    I think, you need to use glRotate function and Qt rotate ( if you paint something with QPainter ).

  3. #3
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    Quote Originally Posted by UASlaX View Post
    I think, you need to use glRotate function and Qt rotate ( if you paint something with QPainter ).
    Yes, I know this. But there may have some other transformations, if I call glRotate manually, it would be complexity. So, I am wonder if I could apply the shader in QPainter

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

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    What happens if you don't call beginNativePainting() at all?
    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. #5
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    Quote Originally Posted by wysota View Post
    What happens if you don't call beginNativePainting() at all?
    Explain it clearly, the QPainter's drawing function all works well with transformation. But the elements drawn by gl command (follow by beginNativePainting()) won't be affected by the transformations.

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

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    Ok but what happens if you don't use beginNativePainting() and just start making gl calls?
    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.


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

    diro (9th September 2011)

  8. #7
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    Quote Originally Posted by wysota View Post
    Ok but what happens if you don't use beginNativePainting() and just start making gl calls?
    Oh, I got you! I have trace into the beginNativePainting() function, and it will call glUseProgram(0) to unload the shader program.
    But I have two questions about don't use beginNativePainting() before gl calls:
    1.Will it cause any side-effect? If not, why Qt design this function?
    2.Because I use shader program, too. Could we have two program at the same time?

    Anyway, really thanks for your suggestion :-)

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

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    Quote Originally Posted by diro View Post
    1.Will it cause any side-effect?
    I don't know, make an example and find it out on your own. I don't even know if it works at all.
    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. #9
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    Quote Originally Posted by wysota View Post
    I don't know, make an example and find it out on your own. I don't even know if it works at all.
    After some testing..
    1.Will it cause any side-effect? If not, why Qt design this function?
    Not sure, but the projection seems wrong, the elements didn't draw in the position I wanted.

    2.Because I use shader program, too. Could we have two program at the same time?
    Yes, I still can use my own shader program.

    Finally, I give up this way and solve this problem by my own transformation matrix.

  11. #10
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    1

    Default Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp

    I think I have to apology first, after many study and experiments, I got a new conclusion now.
    For Qt painting framework, after calling the beginNativePainting(), the following gl commands will be affected by the shaders constructed by Qt(all transformation of coz)
    So, you don't have to do anything, and all the elements drawn by gl commands will also be rotate, flip and so on.

    The code I made in previous edition, I use some custom projection in openGL, so it looks like the previous shaders is not enabled, it is a wrong concept.

Similar Threads

  1. purpose of Q_GLOBAL_STATIC
    By raj_iv in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2011, 14:20
  2. QPainter and Shader
    By shetan in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2011, 08:33
  3. Geometry shader with QT4.7
    By saraksh in forum General Programming
    Replies: 3
    Last Post: 28th March 2011, 16:40
  4. Navigation purpose
    By jerkymotion in forum Qt Programming
    Replies: 6
    Last Post: 17th March 2011, 17:59
  5. How to make a general purpose storage class?
    By khikho in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2009, 23:16

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.