QGLWidget->bindTexture from QPixmap is blank [solved]



  • [Edit]:Solution in the second post

    I have a QGraphicsView with a QGLWidget as its viewport. I'm passing the address of this QGLWidget to my QGraphicsScene subclass, which is the scene of the graphics view. I generate the texture in the constructor of "MainScene", using the pointer to the QGLWidget, and I try to draw the texture later. It draws a white square, so the texture isn't working. Drawing flat colors using GL works fine, and so does drawing the pixmap I'm creating the texture from.

    Here's the QGraphicsView constructor:
    @
    MainWindow::MainWindow() : QGraphicsView()
    {
    setAttribute(Qt::WA_QuitOnClose);
    setFocusPolicy(Qt::WheelFocus);
    //Viewport
    QGLWidget* glWidget = new QGLWidget(QGLFormat(QGL::DoubleBuffer));
    setViewport(glWidget);
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
    setRenderHints(QPainter::SmoothPixmapTransform);
    //Scene
    mScene = new MainScene(glWidget);
    setScene(mScene);

    setFixedSize(800, 600);
    }
    @

    Here's the scene constructor:
    @
    MainScene::MainScene(QGLWidget* glWidget) : QGraphicsScene()
    {
    //GL Init
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, width(), height(), 0.0, 0.0, 1.0);
    glMatrixMode(GL_MODELVIEW); glLoadIdentity();
    //Texture setup
    this->glWidget = glWidget;
    tileset = new QPixmap;
    if(!tileset->load("data/tileset.png")) {
    std::cerr << "Tileset.png could not be found (it should be in /data)\n";
    delete tileset;
    tileset = new QPixmap(256, 1024);
    tileset->fill(Qt::black);
    }
    else { tileset->setMask(tileset->createMaskFromColor(QColor(255, 0, 255))); }
    glWidget->makeCurrent();
    texture = glWidget->bindTexture(*tileset);
    glWidget->doneCurrent();
    }
    @

    Here is the drawing code:
    @
    void MainScene::drawBackground(QPainter* p, const QRectF&)
    {
    p->drawPixmap(0, 0, *tileset, 96, 0, 32, 32); //This works but the rest doesn't
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, width(), height(), 0.0, 0.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2i(0, 0);
    glTexCoord2f(1, 0); glVertex2i(tileset->width(), 0);
    glTexCoord2f(1, 1); glVertex2i(tileset->width(), tileset->height());
    glTexCoord2f(0, 1); glVertex2i(0, tileset->height());
    glEnd();
    glBindTexture(GL_TEXTURE_2D, 0);
    }
    @

    These are the potential reasons that I could think of, but I don't know what to do about them if they're right: A problem with the png being 24 bits; A problem with the QGLWidget context being used by a different class (I'm not multithreading but Qt might be doing it by itself somewhere?) - this wouldn't make sense because the context worked fine for calling bindTexture, but then again I can't even be sure if that texture is valid.



  • Solution: You have to use the QGLContext or QGLWidget drawTexture function. I don't know how to draw only a specific part of the texture like you would with glTexCoord2f, but I don't need to in this case anyway.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.