无法在 GLSL 片段着色器中使用两个统一变量(仅第一个有效)

发布于 2024-11-19 18:43:15 字数 2979 浏览 3 评论 0原文

我正在尝试在片段着色器中混合两种纹理的颜色。但不幸的是我还没有真正开始工作。

这是着色器设置:

const char* vertex = "void main(){ \n\
                gl_Position = ftransform(); \n\
                        gl_TexCoord[0] = gl_MultiTexCoord0;\n\
                        gl_TexCoord[1] = gl_MultiTexCoord1;\n\
                     }\n";

const char* fragment = "\
                       uniform sampler2D userT;\
                       uniform sampler2D videoTex; \
                       \n\
                       void main(){\n\
                            vec4 texel = texture2D(userT, gl_TexCoord[0].st);\n\
                            gl_FragColor = texel;\n\
                        }\n";

m_vertexShader = glCreateShader(GL_VERTEX_SHADER);
m_fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource( m_vertexShader, 1, &vertex, NULL );
glShaderSource( m_fragmentShader, 1, &fragment, NULL );

glCompileShader( m_vertexShader );
glCompileShader( m_fragmentShader );

m_silhouetteShaderProg = glCreateProgram();
glAttachShader( m_silhouetteShaderProg, m_vertexShader );
glAttachShader( m_silhouetteShaderProg, m_fragmentShader );
glLinkProgram( m_silhouetteShaderProg );

目前片段着色器仅显示其中一个纹理,如果我让两个纹理都在这个地方工作,稍后将进行更改。

我确实检查了编译和链接错误,一切都很好。

这是我的显示函数:

 .... setup of the textures m_videoTexture and m_userPixelTexture with glTexSubImage2D calls ....

glUseProgram( m_silhouetteShaderProg );

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_videoTexture);
GLint videoTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "videoTex" );
glUniform1i( videoTexUnif, 0 );

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_userPixelTexture);
GLint userTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "userT" );
glUniform1i( userTexUnif, 1 );

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glBegin(GL_QUADS);
    glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 0.0f);
    glVertex3f(-0.5f, -0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 1.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 1.0f*stretch);
    glVertex3f(-0.5f, 0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1.0f*stretch);
    glVertex3f(0.5f, 0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f);
    glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();

glUseProgram( 0 );
glActiveTexture(GL_TEXTURE0);

我的问题是我只能使用“glGetUniformLocation”访问片段着色器的第一个统一(“userT”)。 调用:“GLint videoTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "videoTex" );"结果为“-1”。

如果我更改片段着色器中制服的顺序,我可以访问“videoTex”,但不能访问“userT”。

我看到很多在片段着色器中使用两个制服的例子,所以我强烈认为这是可能的。但我在这里做错了什么?

I am trying to mix the colors of two textures in a fragment shader. But unfortunately i am stuck before the real work yet.

Here is shader setup:

const char* vertex = "void main(){ \n\
                gl_Position = ftransform(); \n\
                        gl_TexCoord[0] = gl_MultiTexCoord0;\n\
                        gl_TexCoord[1] = gl_MultiTexCoord1;\n\
                     }\n";

const char* fragment = "\
                       uniform sampler2D userT;\
                       uniform sampler2D videoTex; \
                       \n\
                       void main(){\n\
                            vec4 texel = texture2D(userT, gl_TexCoord[0].st);\n\
                            gl_FragColor = texel;\n\
                        }\n";

m_vertexShader = glCreateShader(GL_VERTEX_SHADER);
m_fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource( m_vertexShader, 1, &vertex, NULL );
glShaderSource( m_fragmentShader, 1, &fragment, NULL );

glCompileShader( m_vertexShader );
glCompileShader( m_fragmentShader );

m_silhouetteShaderProg = glCreateProgram();
glAttachShader( m_silhouetteShaderProg, m_vertexShader );
glAttachShader( m_silhouetteShaderProg, m_fragmentShader );
glLinkProgram( m_silhouetteShaderProg );

For now the fragment shader only displays one of the txtures, this will be changed later, if i get both textures working at this place.

I did check for compilation and linking errors, and everything is fine.

And here is my display function:

 .... setup of the textures m_videoTexture and m_userPixelTexture with glTexSubImage2D calls ....

glUseProgram( m_silhouetteShaderProg );

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_videoTexture);
GLint videoTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "videoTex" );
glUniform1i( videoTexUnif, 0 );

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_userPixelTexture);
GLint userTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "userT" );
glUniform1i( userTexUnif, 1 );

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glBegin(GL_QUADS);
    glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 0.0f);
    glVertex3f(-0.5f, -0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 1.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 1.0f*stretch);
    glVertex3f(-0.5f, 0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1.0f*stretch);
    glVertex3f(0.5f, 0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f);
    glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();

glUseProgram( 0 );
glActiveTexture(GL_TEXTURE0);

My Problem is that i can only access the first uniform of my fragment shader ("userT") with "glGetUniformLocation".
The call: "GLint videoTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "videoTex" );" results in "-1".

If i change the order of the uniforms in the fragment shader, i can access "videoTex" but not "userT".

I saw many examples using two uniforms in the fragment shader, so i strongly assume that this is possible. But what am i doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2024-11-26 18:43:15

您的第二个统一采样器已被 GLSL 编译器优化掉,因为它未被使用。在链接的 GLSL 程序中,没有这样的统一,因此您的程序逻辑应该考虑到这一点(任何统一都可以根据您的代码进行优化)。

Your second uniform sampler was optimized out by the GLSL compiler because it's not used. In a linked GLSL program there is no such uniform, so your program logic should take that into account (any uniform can be optimized out based on your code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文