我无法弄清楚如何正确地从QOpenGLBuffer:: PixelUnpackBuffer
.
- 写入 PBO 之前的正确设置是什么?
QOpenGLBuffer::write
无法使用简单的QImage.bits()
,或glReadPixels()
将 FBO 渲染传递到 PBO。它必须是特定类型的数据吗?- 您如何使用书面 PBO 和
Texture::setData()
? 一个简单的Texture.setData(*format*, *pixel_type*, pubo, nullptr)
就够了吗?
这里有一些代码来举例说明我在做什么:
QImage ScribbleArea::proImg(const QImage& image,
const QImage& tip,
const QString& vertexPosVar,
const QString& textureCoordVar){
QOpenGLContext context;
QOffscreenSurface offSurface;
offSurface.setFormat(context.format());
offSurface.create();
// I'm sharing the context to show the offscreen Render into a
// QOpenGLTextureBlitter under PaintGL()
context.setShareContext(ScribbleArea::context());
context.create();
context.makeCurrent(&offSurface);
m_fbo = new QOpenGLFramebufferObject(image.size());
m_fbo->bind();
context.functions()->glViewport(0, 0, image.width(), image.height());
QOpenGLShaderProgram program(&context);
program.addShaderFromSourceFile(QOpenGLShader::Vertex, "path to vertex shader");
program.addShaderFromSourceFile(QOpenGLShader::Fragment, "path to fragment shader");
program.link();
program.bind();
// The next block is basically what I understood how to setup a PBO using
// Qt's OpenGL wrapper.
QOpenGLBuffer *pubo = new QOpenGLBuffer(QOpenGLBuffer::PixelUnpackBuffer);
pubo->setUsagePattern(QOpenGLBuffer::DynamicCopy);
pubo->create();
pubo->bind();
pubo->map(QOpenGLBuffer::ReadWrite);
pubo->allocate(image.bits(),image.sizeInBytes());
pubo->write(0,image.bits(),image.sizeInBytes());
pubo->unmap();
pubo->release();
// Testing how to use texture::setData() using straight bytes instead of the
// baked method of setData(QImage).
// I believe this is how to use the PBO's content to set the texture using QOpenGL.
QOpenGLTexture textu(QOpenGLTexture::Target2D);
textu.setSize(image.width(),image.height());
textu.setFormat(QOpenGLTexture::RGBA8_UNorm);
textu.allocateStorage();
textu.setData(QOpenGLTexture::BGRA,QOpenGLTexture::UInt8,image.bits(),nullptr);
// The texture bellow is a test to see if I was able to set up two textures and
// change which one the shader should use.
QOpenGLTexture brush(QOpenGLTexture::Target2D);
brush.setData(tip);
// Using native OpenGL snippets never work. The texture remain black.
// GLuint tex;
// glGenTextures(1, &tex);
// glBindTexture(GL_TEXTURE_2D, tex);
// glTexImage2D(GL_TEXTURE_2D, 0, 3, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glBindTexture( GL_TEXTURE_2D, 0);
GLint brTip = 1;
brush.bind(brTip);
GLint oriTxt = 0;
textu.bind(oriTxt);
//Rest of the code. Following Amin Ahmadi tutorial.