Qt绑定纹理的魔力是什么?



我使用Qt5和QGLWidget类来渲染实时像素流。我遇到了一些性能问题,想设置一个像素缓冲区来执行异步数据传输。

我试图用老式的方式绑定一个纹理,但它呈现为空白。当我回到我的Qt5代码时,一切都正常。

有人知道如何使用标准OpenGL版本吗?

工作

void glStream::reserveTextures()
{
    displayBuff = (GLubyte*) calloc(numGLFrames*widthGL*heightGL,
    sizeof(GLubyte));
    QImage mySurface(&displayBuff[displayStart],widthGL,heightGL,
    QImage::Format_Indexed8);
    textures[0]=bindTexture(mySurface,GL_TEXTURE_2D,GL_LUMINANCE);
    glBindTexture(GL_TEXTURE_2D,0);
    glECheck();//no errors
}

不工作

void glStream::reserveTextures()
{
    displayBuff = (GLubyte*) calloc(numGLFrames*widthGL*heightGL,sizeof(GLubyte));
    glGenTextures(1,&textures[0]);
    glBindTexture(GL_TEXTURE_2D,textures[0]);
    glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE,widthGL,heightGL,0,GL_LUMINANCE,
    GL_UNSIGNED_BYTE,&displayBuff[displayStart]);
    glBindTexture(GL_TEXTURE_2D,0);
    glECheck();//no errors
}

更新功能

glECheck();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
program1.bind();
program1.setUniformValue("texture", 0);
program1.enableAttributeArray(vertexAttr1);
program1.enableAttributeArray(vertexTexr1);
program1.setAttributeArray(vertexAttr1, vertices.constData());
program1.setAttributeArray(vertexTexr1, texCoords.constData());
glBindTexture(GL_TEXTURE_2D, textures[0]);
glECheck();
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glBindTexture(GL_TEXTURE_2D,0);
program1.disableAttributeArray(vertexTexr1);
program1.disableAttributeArray(vertexAttr1);
program1.release();
glECheck();

着色器

QGLShader *fshader1 = new QGLShader(QGLShader::Fragment, this);
    const char *fsrc1 =
        "uniform sampler2D texture;n"
        "varying mediump vec4 texc;n"
        "void main(void)n"
        "{n"
        "    gl_FragColor = texture2D(texture, texc.st);n"
        "}n";
    fshader1->compileSourceCode(fsrc1);
    program1.addShader(fshader1);

似乎需要启用一些其他设置才能显示纹理。

void glStream::reserveTextures()
{
    displayBuff = (GLubyte*) calloc(numGLFrames*widthGL*heightGL,sizeof(GLubyte));
    glGenTextures(1,&textures[0]);
    glBindTexture(GL_TEXTURE_2D,textures[0]);
    glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE,widthGL,heightGL,0,GL_LUMINANCE,
    GL_UNSIGNED_BYTE,&displayBuff[displayStart]);
    //This is the magic inside Qt5's implementation
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glBindTexture(GL_TEXTURE_2D,0);
    glECheck();//no errors
}   

相关内容

  • 没有找到相关文章

最新更新