Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ GLSL像素着色器仅操作0纹理单位_C++_Qt_Opengl_Glsl_Shader - Fatal编程技术网

C++ GLSL像素着色器仅操作0纹理单位

C++ GLSL像素着色器仅操作0纹理单位,c++,qt,opengl,glsl,shader,C++,Qt,Opengl,Glsl,Shader,我正在尝试编写一个简单的片段着色器,它应该能够混合2个或更多纹理。我已经在Qt5.4上编写了测试项目,但由于某些原因,它无法操作绑定到非零unite的任何纹理。 它会忽略中的任何值 setUniformValue(“tex*”,*);(第83-90街) 任何采样2D总是只操作绑定到0的纹理 怎么了 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 int main(int argc,char*argv[]) { 质量保证申请a(argc、argv); QSurfaceFo

我正在尝试编写一个简单的片段着色器,它应该能够混合2个或更多纹理。我已经在Qt5.4上编写了测试项目,但由于某些原因,它无法操作绑定到非零unite的任何纹理。 它会忽略中的任何值 setUniformValue(“tex*”,*);(第83-90街) 任何采样2D总是只操作绑定到0的纹理

怎么了

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QSurfaceFormat格式;
格式.setMinorVersion(2);
格式。setMajorVersion(4);
setProfile(QSurfaceFormat::CompatibilityProfile);
//format.setProfile(QSurfaceFormat::CoreProfile);
语境;
setFormat(格式);
如果(!context.create()){
qFatal(“无法创建请求的OpenGL上下文!”);
}
Qoffscreen表面;
surface.setFormat(格式);
surface.create();
context.makeCurrent(&surface);
glMatrixMode(GLU模型视图);
glLoadIdentity();
glMatrixMode(GL_投影);
glLoadIdentity();
格洛托(0.0,1.0,0.0,1.0,0.0,1.0);
glClearColor(0.0f、0.0f、0.0f、0.0f);
glEnableClientState(GL_顶点_数组);
glEnableClientState(GL_纹理_坐标_阵列);
常数浮点c_012; 0平方顶点[8]={0.0f,0.0f,
1.0f,0.0f,
1.0f,1.0f,
0.0f,1.0f};
glVertexPointer(2,GL_FLOAT,0,c_);
glTexCoordPointer(2,GL_FLOAT,0,c_);
glDisable(GLU深度测试);
glDisable(GL_纹理_2D);
int最大纹理单位;
glGetIntegerv(GL最大组合纹理图像单位和最大纹理单位);

qDebug()在OpenGL的C API中,要使用或修改对象,必须首先绑定它*

显然,
pr.setUniformValue
在调用
glUniform
更改统一值之前没有绑定
pr
。虽然有点不方便和不直观,但这是可以理解的;重复绑定同一着色器会带来性能开销

所以只需将
pr.bind()
移动到上面调用
pr.setUniformValue
的位置



*EXT_direct_state_access扩展允许您在不绑定对象的情况下修改对象,但需要不同的API,并且不能保证在4.5(最新版本)之前的OpenGL版本中出现。

尝试将
pr.bind()
移动到第一个
pr.setUniformValue
之前(在
glClear
之后)Thanx很多人!很管用)@ColonelThirtyTwo你应该把你的评论转移到一个答案上来
#include <QApplication>
#include <QCoreApplication>
#include <QOffscreenSurface>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLFramebufferObject>
#include <QOpenGLShader>
#include <QOpenGLTexture>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QSurfaceFormat format;
    format.setMinorVersion( 2 );
    format.setMajorVersion( 4 );
    format.setProfile( QSurfaceFormat::CompatibilityProfile );
//    format.setProfile( QSurfaceFormat::CoreProfile );

    QOpenGLContext context;
    context.setFormat(format);
    if(!context.create()){        
        qFatal("Cannot create the requested OpenGL context!");
    }

    QOffscreenSurface surface;
    surface.setFormat( format );
    surface.create();
    context.makeCurrent( &surface );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    const float c_01SquareVertices[8] ={0.0f, 0.0f,
                                        1.0f, 0.0f,
                                        1.0f, 1.0f,
                                        0.0f, 1.0f};
    glVertexPointer(2, GL_FLOAT, 0, c_01SquareVertices);
    glTexCoordPointer(2, GL_FLOAT, 0, c_01SquareVertices);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_TEXTURE_2D);
    int maxTextureUnits;
    glGetIntegerv ( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits );
    qDebug()<<"GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS" << maxTextureUnits;

    QImage texImg = QImage(":/tex/tex");
    QOpenGLTexture tex(texImg.mirrored());
    QImage texImg1 = QImage(":/tex/tex1");
    QOpenGLTexture tex1(texImg1.mirrored());
    QImage texImg2 = QImage(":/tex/tex2");
    QOpenGLTexture tex2(texImg2.mirrored());

    QString fsc =
            "uniform sampler2D tex;"
            "uniform sampler2D tex1;"
            "uniform sampler2D tex2;"
            "varying vec4 gl_TexCoord[];"
            "void main(void)"
            "{"
            "   gl_FragColor = texture2D(tex2, gl_TexCoord[0].yx * 2.0);"
//            "   gl_FragColor = texture2D(tex1, gl_TexCoord[0].xy) + texture2D(tex2, gl_TexCoord[0].xy);"
            "}";

    QOpenGLShader fsh( QOpenGLShader::Fragment, &context );
    fsh.compileSourceCode( fsc );

    QOpenGLShaderProgram pr( &context );

    pr.addShader( &fsh );
    pr.link();

    QOpenGLFramebufferObjectFormat fboFormat;
//    fboFormat.setInternalTextureFormat(GL_ALPHA32F);
    QOpenGLFramebufferObject fbo( 1000, 1000, fboFormat );
    fbo.bind();
        glViewport(0,0,fbo.width(),fbo.height());
        glClear(GL_COLOR_BUFFER_BIT);

        tex.bind(0);
        pr.setUniformValue("tex", GLuint(1));

        tex1.bind(2);
        pr.setUniformValue("tex1", GLuint(2));

        tex2.bind(3);
        pr.setUniformValue("tex2", GLuint(3));

        pr.bind();

        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    fbo.release();

    QLabel w;
    w.resize(fbo.size());
    w.setPixmap(QPixmap::fromImage(fbo.toImage()));
    w.show();

    return a.exec();
}