2

I have a problem to render a QImage with a derived QOpenGLWidget class. If I create my own raw graphic everything works fine but I can't create a proper QOpenGLTexture. I see only some random pixel from memory. I tried a lot but it doesn't work. Here is my test code:

initializeGL()

    initializeOpenGLFunctions();

    glClearColor(0.0, 0.0, 0.0, 0.0);

    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glOrtho(0, this->width(), 0, this->height(), 0, 1);
    glMatrixMode(GL_MODELVIEW);

    glShadeModel(GL_FLAT);


paintGL()

    mTexture->bind();
    glDrawPixels(mTexture->width(), mTexture->height(), QOpenGLTexture::RGBA, GL_UNSIGNED_BYTE, mTexture);

loadImage()

    mTexture = new QOpenGLTexture(QImage::fromData(image).convertToFormat(QImage::Format_RGBA8888));

"image" is a QByteArray and works fine in a QGraphicsView. So it seems that I am missing something.

1 Answer 1

2

glDrawPixels last parameter expect a pointer to the image data, you are passing a pointer to a QOpenGLTexture that is a completely unrelated object (it probably does not even contain the image data).

What you want is something like this:

QImage m_img;

void initializeGL() 
{
  m_img = QImage::fromData(image).convertToFormat(QImage::Format_RGBA8888);
}

void paintGL()
{
  glPixelStorei(GL_UNPACK_ROW_LENGTH, m_img.bytesPerLine() / 4);
  glDrawPixels(m_img.width(), m_img.height(), QOpenGLTexture::RGBA, GL_UNSIGNED_BYTE, m_img.bits());
}

Notice that glDrawPixels is not the right tool to do that anyway (it transfers the entire image from client to host memory every time you call it). The best way is probably to create the texture like you did and then render a full screen quad with proper texture coordinates

.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.