我目前正在尝试将纹理渲染为全屏QWindow
(Qt 5.9.1)。
由于纹理都是黑色的,我想通过告诉片段着色器将所有像素渲染成红色来确保凹痕是正确的,但显然这里发生了错误。
而不是四边形我在屏幕的左上角有一个红色三角形
我看不出我的代码所犯的错误,所以我请你查看我的代码
即使我使用glTexImage2D
上传的纹理看起来很好并且glGetError
中没有错误,也可能是仅获得黑色纹理的原因。
在此先感谢。
初始化OpenGL上下文和QWindow:
int i;
for( i=0;i<QApplication::screens().size();i++ )
{
if(QApplication::screens().at(i)->name().compare(szScreenName)==0)
break;
}
if(i==QApplication::screens().size())
i--;
setGeometry(QApplication::screens().at(i)->availableGeometry());
showFullScreen();
setSurfaceType(QWindow::OpenGLSurface);
QSurfaceFormat fmt;
fmt.setSwapBehavior(QSurfaceFormat::SingleBuffer);
fmt.setRenderableType(QSurfaceFormat::OpenGLES);
setFormat(fmt);
_pContext = new QOpenGLContext(this);
_pContext->setFormat(fmt);
//_pContext->setShareContext(QOpenGLContext::globalShareContext());
_pContext->create();
QObject::connect( this, &DLP::postRenderSignal, this, &DLP::render );
_pContext->makeCurrent(this);
initializeOpenGLFunctions();
//_pTexManager = new TextureManager(QOpenGLContext::globalShareContext()->functions());
_pTexManager = new TextureManager(this);
char* szVertexShader = "#version 110\n"
"attribute vec4 a_Position;\n"
"attribute vec2 a_TexCoord;\n"
"varying vec2 v_TexCoord;\n"
"void main()\n"
"{\n"
" gl_Position = a_Position;\n"
" v_TexCoord = a_TexCoord;\n"
"}\n";
char* szFragmentShader = "#version 110\n"
"precision mediump float;\n"
"varying vec2 v_TexCoord;\n"
"uniform sampler2D u_Texture;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0,0.0,0.0,1.0);//texture2D( u_Texture, v_TexCoord );\n"
"}\n";
//_nProgram = linkShader(QOpenGLContext::globalShareContext()->functions(), szVertexShader, szFragmentShader);
_nProgram = linkShader(this, szVertexShader, szFragmentShader);
if(_nProgram!=0)
{
_nShaderSamplerLoc = glGetUniformLocation(_nProgram,"u_Texture");
_nShaderTextureLoc = glGetAttribLocation(_nProgram,"a_TexCoord");
_nShaderPositionLoc = glGetAttribLocation(_nProgram,"a_Position");
}
glGenBuffers(2, _pBuffers );
static const unsigned short pIndexData[] = { 0, 1, 2, 3 };
glBindBuffer(GL_ARRAY_BUFFER, _pBuffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(pIndexData), pIndexData, GL_STATIC_DRAW);
static const float pPosBuffer[20] = {
-1, 1, 0, 0, 0,
1, 1, 0, 1, 0,
-1, -1, 0, 0, 1,
1 -1, 0, 1, 1,
};
glBindBuffer(GL_ARRAY_BUFFER, _pBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, 20*4, pPosBuffer, GL_STATIC_DRAW);
渲染代码(DLP :: render):
if (!isExposed() || !_pContext)
return;
_pContext->makeCurrent(this);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
glViewport(0,0,m_nWidth,m_nHeight);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(_nProgram);
glBindBuffer(GL_ARRAY_BUFFER, _pBuffers[0]);
// Load the vertex position
glVertexAttribPointer(_nShaderPositionLoc, 3, GL_FLOAT, false, 5 * 4, (void*)0);
glEnableVertexAttribArray(_nShaderPositionLoc);
// Load the texture coordinate
//glVertexAttribPointer(_nShaderTextureLoc, 2, GL_FLOAT, false, 5 * 4, (void*)(3 * 4));
//glEnableVertexAttribArray(_nShaderTextureLoc);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _pTexManager->getTexture(key));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(_nShaderSamplerLoc, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _pBuffers[1]);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
glDisableVertexAttribArray(_nShaderPositionLoc);
//glDisableVertexAttribArray(_nShaderTextureLoc);
glFinish();
_pContext->swapBuffers(this);
编辑:
此外,只有在Qt中有多个OpenGL上下文时才会出现此问题,例如: 4帧缓冲对象,QML场景图和这个QWindow,所以它可能是Qt如何处理OpenGL上下文的问题。
答案 0 :(得分:0)
好的,问题解决了:
显然我的纹理数据存在一些问题,它确实有奇怪的alpha数据,因此启用混合功能时它不会渲染纹理。
另外,矩形未正确渲染的原因是其中一个凹痕中的符号错误
顺便说一句:Vogl是一个非常有用的工具,用于查看OpenGL状态机以调试应用程序。