带有Target2DArray的QopenGLTexture

时间:2017-05-13 07:58:34

标签: c++ qt opengl

我尝试使用QOpenGLTextures在qt中处理2D数组纹理,我想将多个图像发送到着色器以在每个帧之间进行一些处理。这是我到目前为止所做的和调试我只是处理前两个图像:

std::vector< QImage* > vtextureImgs;
vtextureImgs.resize(2);

QImage *textures0 = new QImage(m_dataPath + "/" +fileslist[0]);
m_width         =   textures0->width();
m_height        =   textures0->height();
m_QImgFormat    =   textures0->format();
vtextureImgs[0] =   textures0;

QImage *textures1 = new QImage(m_dataPath + "/" +fileslist[1]);
vtextureImgs[1] =   textures1;


GLsizei layerCount = 1;
GLsizei mipLevelCount = 1;




SAFE_DELETE(m_GLTexture);
m_GLTexture = new QOpenGLTexture(QOpenGLTexture::Target::Target2DArray);
m_GLTexture->destroy();
m_GLTexture->create();

m_GLTexture->bind(); 

m_GLTexture->setMinificationFilter(QOpenGLTexture::Nearest);
m_GLTexture->setMagnificationFilter(QOpenGLTexture::Nearest);

m_GLTexture->setLayers(layerCount);


// m_GLTexture->setSize(m_width,m_height,1 );
m_GLTexture->setWrapMode(QOpenGLTexture::ClampToEdge);

m_GLTexture->setData(0, 0, QOpenGLTexture::RGB, QOpenGLTexture::UInt16, vtextureImgs[0]);

m_GLTexture->bind(0);


if(m_ShaderProgram!=nullptr)
{
    m_ShaderProgram->bind();
    m_ShaderProgram->setUniformValue("bgTexSampler",0);
    m_ShaderProgram->setUniformValue("rows",1);
    m_ShaderProgram->setUniformValue("cols",2);
    m_ShaderProgram->release();

}

但是当我调用glTexSubImage3D时它会给我分段错误。关于这里有什么问题的任何想法?以及如何为这种纹理设置统一值?我的类继承自QOpenGLWidget类。

编辑:通过继承QOpenGLFunctions来修复分段错误。很奇怪,我没有简单的2D纹理这个问题。 现在我只得到黑屏而不是我发送到着色器的纹理。这是我的paintGL的样子:

    QMatrix4x4 projectionMatrix, modelMatrix, viewMatrix;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

projectionMatrix.ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
modelMatrix.setToIdentity();
viewMatrix.setToIdentity();

makeCurrent();
m_ShaderProgram->bind();

if(m_GLTexture->isCreated())
{

    m_GLTexture->bind();

    m_ShaderProgram->setUniformValue("projectionMatrix", projectionMatrix);
    m_ShaderProgram->setUniformValue("ModelMatrix", modelMatrix);
    m_ShaderProgram->setUniformValue("viewMatrix", viewMatrix);
    m_ShaderProgram->setUniformValue("cameraPositionX",(float)m_vfCameraPosition.x());
    m_ShaderProgram->setUniformValue("cameraPositionY",(float)m_vfCameraPosition.y());

    const QVector3D vertices[4] = {
        QVector3D(-1.0f, -1.0f, 0.0f),
        QVector3D(-1.0f,  1.0f, 0.0f),
        QVector3D( 1.0f,  1.0f, 0.0f),
        QVector3D( 1.0f, -1.0f, 0.0f)
    };

    const QVector2D texcoords[4] = {
        QVector2D(0.0f, 1.0f),
        QVector2D(0.0f, 0.0f),
        QVector2D(1.0f, 0.0f),
        QVector2D(1.0f, 1.0f)
    };

    const unsigned int indices[4] = { 0, 1, 2, 3 };

    m_ShaderProgram->enableAttributeArray("vertices");
    m_ShaderProgram->enableAttributeArray("vTexCoords");

    m_ShaderProgram->setAttributeArray("vertices", vertices);
    m_ShaderProgram->setAttributeArray("vTexCoords", texcoords);

    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indices);

    m_ShaderProgram->disableAttributeArray("vertices");
    m_ShaderProgram->disableAttributeArray("vTexCoords");

    m_GLTexture->bind(0);
}
 m_ShaderProgram->release();

和我的着色器:

uniform sampler2DArray bgTexSampler;
uniform int rows;
uniform int cols;
uniform float cameraPositionX;
uniform float cameraPositionY;
smooth in vec2 FragTexCoord;
out vec4 fragColor;

void main(void)
{
   vec4 color = texture(bgTexSampler, vec3(FragTexCoord, 0));
   fragColor = vec4(color.rgb, 1.0);
}

0 个答案:

没有答案