简体   繁体   中英

Render Pixeldata into OpenGL Texture quad

After days of trying I was not able to render Pixeldata into a textured quad correctly.

What I want is fairly simple (I think): I am currently writing a VNC implementation for my company to bind it into an existing application. I already successfully implemented the RfbProtocol (at least as far as I need it) and I am able to get the correct Pixeldata. Because the VNC server is sending incremental changes only I am receiving small rectangles with the changed region and the pixel information.

I already worked out that the corresponding OpenGL Formats for the Pixeldata is GL_BGRA and GL_UNSIGNED_INT_8_8_8_8_REV .

Because I was not able to use Textured Quads (whats seems to be the optimal implementation for this purpose) I used a different approach:

  if(m_queue.size() > 0)
  {
    if (!m_fbo || m_fbo->size() != size())
    {
      delete m_fbo;
      QOpenGLFramebufferObjectFormat format;
      format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
      m_fbo = new QOpenGLFramebufferObject(size());
    }

    m_fbo->bind();

    Q_FOREACH (RfbProtocol::RfbRectangle rect, m_queue.dequeue().rectangles())
    {
      glViewport(0, 0, 1920, 1080);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, 1920, 0, 1080, -1, 1);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glRasterPos2d(rect.xpos, rect.ypos);
      glDrawPixels(rect.width,rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, rect.pixelData.data());
    }
    m_fbo->release();
    this->update();
  }

This is all done inside an QOpenGLWidget.

Obviously this is not the best approach but it is working for testing. The problem is that I need to scale the Pixeldata to the size of the Widget, which would work nicely together with a OpenGLTexture but as I mentioned at the beginning I simply wasn't able to do it correctly.

Don't use glDrawPixels . It's old and slow and has been removed from modern OpenGL.

Just use glTexSubImage2D to update the subrectangles of the texture holding the screen content.

EDIT :

GLuint create_texture(unsigned width, unsigned height)
{
    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);

    /* initialize texture to nil */
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
        width, height, 0,
        GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);

    /* disable mipmapping */
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    return tex;
}

void update_texture(GLuint tex, RECT r, RGBA *pixels)
{
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexSubImage2D(GL_TEXTURE_2D, 0,
        r.left, r.bottom,
        r.right-r.left, r.top-r.bottom,
        GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV,
        pixels );
}

I guess you can tink the definitions for RECT and RGBA yourself.

Then just draw a viewport filling textured quad and you're done. Textured quads give you nice scaling for free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM