Qt的bindTexture内部有什么神奇之处?

时间:2013-02-24 03:41:43

标签: qt opengl opengl-es qt5

我正在使用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);

1 个答案:

答案 0 :(得分:3)

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

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
}