[英]GLSL Pixel shader operates only 0 texture unite
我正在嘗試編寫一個簡單的片段着色器,它應該混合2個或更多紋理。 我已經在Qt 5.4上編寫了測試項目,但是由於某種原因,它不能操作綁定到非零單位的任何紋理。 它忽略setUniformValue(“ tex *”,*)中的任何值; (str。83-90),任何sampler2d始終僅操作綁定到0 unite的紋理。
怎么了?
可在bitbucket上獲得有關Qt 5.4的測試項目的來源
#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();
}
在OpenGL的C API中,要使用或修改對象,必須首先將其綁定*。
顯然,在調用glUniform
更改統一值之前, pr.setUniformValue
不會綁定pr
。 雖然有點不方便和不直觀,但這是可以理解的。 一遍又一遍地冗余綁定同一着色器會增加性能開銷。
因此,只需將pr.bind()
移動到上面調用pr.setUniformValue
。
* EXT_direct_state_access擴展允許您修改對象而不綁定它們,但是需要使用不同的API,並且不能保證在4.5(最新)之前的OpenGL版本中出現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.