Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use semi-transparent QPixmaps as textures?
Forum Updated to NodeBB v4.3 + New Features

How to use semi-transparent QPixmaps as textures?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bp45738
    wrote on last edited by
    #1

    I have posted a similar question on stackoverflow and QtCentre with unsuccessful results, so I'll rephrase if anyone has seen those.

    I want to use QPixmaps as textures in a QGLWidget. The problem I am encountering is that the alpha seems to be applied to the color channels twice because the format of QPixmaps with transparency is premultiplied alpha. If I use a QImage then the alpha is applied only once and the texture has the correct colors. I don't want to use QImages as textures though because they are noticeably slower than QPixmaps because they are in main memory.

    My procedure is to call QPixmap::fill with a semitransparent QColor, and then call QGLWidget::bind/drawTexture on that QPixmap. If I use a color with a red value of 0.5 and an alpha of 0.5, then before binding the QPixmap (and after converting to a QImage for pixel sampling, in format ARGB32_Premultiplied), then the sampled values for the red channel are 0.25. This verifies the format as premultiplied alpha. When the bound QPixmap is drawn to the QGLWidget as a texture and the scene is rendered, the red channel then becomes 0.125, meaning the alpha has been applied twice, once for the premultiplication and again when rendering the scene.

    It seems logical that QPixmaps would be used as textures for QGLWidgets since QPixmaps can reside in video memory. How then do I use semi-transparent QPixmaps as textures without applying the alpha twice? Is there some procedure I am missing?

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      Why not just use QImage? QImage's can be created and drawn on in any thread whereas QPixmaps can only be used in the main GUI thread. Also, if you are using the raster paint engine then QPixmap shares a backend with QImage anyway since it is entirely software based.

      Once you bind the image as an OpenGL texture (and perhaps have mipmaps generated) they reside in graphics memory anyway (resource permitting) since OpenGL converts them to the most appropriate type for that implementation.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bp45738
        wrote on last edited by
        #3

        I did not want to use use QImages because they are stored in system memory and I was binding and deleting textures frequently. I refactored my code to use QImages and minimize binds, thus minimizing sending the data over the bus. The performance is good.

        Still, I think it is an oversight that semi-transparent QPixmaps used as textures are not displayed correctly, if my explanation of this phenomenon is indeed the case. It would be great in the future if there was a QGLContext::BindOption that did the inverse of PremultipliedAlphaBindOption and undid any premultiplication that had already occurred.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved