Results 1 to 6 of 6

Thread: OpenGL texture on QGraphicsScene background

  1. #1
    Join Date
    Jul 2008
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default OpenGL texture on QGraphicsScene background

    Hello!

    I try to draw texture on QGraphicsScene background. QGraphicsView object has QGLWidget object as the viewport.
    Reimplemented QGraphicsScene::drawBackground function is:
    Qt Code:
    1. void OWorkspaceScene::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. GLuint texture[1];
    4. QImage t, b;
    5.  
    6. b.load("../images/image.bmp"); // image size 256x256
    7.  
    8. t = QGLWidget::convertToGLFormat(b);
    9. glGenTextures(1, texture);
    10. glBindTexture(GL_TEXTURE_2D, texture[0]);
    11. glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
    12. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    13. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    14.  
    15. glEnable(GL_TEXTURE_2D);
    16.  
    17. glClearColor(1, 1, 1, 1);
    18. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    19.  
    20. glBegin(GL_QUADS);
    21. glTexCoord2f(0, 0);
    22. glVertex2f(0, 0);
    23. glTexCoord2f(0, 255);
    24. glVertex2f(0, 255);
    25. glTexCoord2f(255, 255);
    26. glVertex2f(255, 255);
    27. glTexCoord2f(255, 0);
    28. glVertex2f(255, 0);
    29. glEnd();
    30.  
    31. glFinish();
    32. }
    To copy to clipboard, switch view to plain text mode 
    As a result i have grey square nice image instead.
    When i draw color square (without texture), it works fine.
    I work using QT-4.3.1 under Visual Studio 2005.

    What do i do wrong?
    Last edited by jpn; 17th July 2008 at 20:04. Reason: missing [code] tags

  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: OpenGL texture on QGraphicsScene background

    Have you checked that the path to the image is correct and that the image gets loaded?

    BTW. Using Qt methods for binding the texture might be a bit less error prone, i.e.
    Qt Code:
    1. bindTexture(QPixmap(":/images/side.png"), GL_TEXTURE_2D);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2008
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL texture on QGraphicsScene background

    Quote Originally Posted by wysota View Post
    Have you checked that the path to the image is correct and that the image gets loaded?

    BTW. Using Qt methods for binding the texture might be a bit less error prone, i.e.
    Qt Code:
    1. bindTexture(QPixmap(":/images/side.png"), GL_TEXTURE_2D);
    To copy to clipboard, switch view to plain text mode 
    Yes. The path to the image is correct and that the image gets loaded.
    Same code works fine with QGLWidget in QGLWidget:aintGL().

  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: OpenGL texture on QGraphicsScene background


  5. #5
    Join Date
    Jul 2008
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL texture on QGraphicsScene background

    Quote Originally Posted by wysota View Post
    Unfortunately, i use QT-4.3.1. In addition, i want solve problem with method presented above.

  6. #6
    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 texture on QGraphicsScene background

    The version doesn't matter. I gave you the link because there is OpenGL used to implement drawBackground() for a graphics scene. Maybe you need to do something special for GL to work there and maybe you'll find it in the source code of the article.

Similar Threads

  1. Moving the QGraphicsScene background content
    By maverick_pol in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2008, 08:37
  2. Using QGraphicsScene instead of OpenGL
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2008, 22: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.