QOpenglWidget中使用glortho无法绘制出纹理图,在glut下可以

在QOpenglWidget中绘制图形,使用glm::ortho模式进行正交投影,发现图形无法显示。如下图所示,显示的是清屏的红色。
图片说明
相同的绘制代码使用glut绘制的窗体中,可以正常显示。如下图所示。
图片说明
以下贴出核心代码,请问可能什么原因?

const GLfloat vertexVertices[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};

const GLfloat textureVertices[] =
{
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};

void HozoHorizontalDrawer::InitDrawer()
{
InitShader();//加载shader source
GenTextureArray();//创建纹理
glUniform1i(m_iYTextureUniform, 0);
SetTextureEnvInitFlag(true);

glUseProgram(m_nShaderProgram);
glGenBuffers(2, m_nBufferArray);
glBindBuffer(GL_ARRAY_BUFFER, m_nBufferArray[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexVertices), vertexVertices, GL_STATIC_DRAW);
glVertexAttribPointer(eVertex, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(eVertex);

glBindBuffer(GL_ARRAY_BUFFER, m_nBufferArray[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(textureVertices), textureVertices, GL_STATIC_DRAW);
glVertexAttribPointer(eTexture, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(eTexture);

}

void HozoHorizontalDrawer::Draw(float PlayerControlWidth, float PlayerControlHeight, tagTextureDescription panoramaFrame)
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);

glUseProgram(m_nShaderProgram);

//正交投影
m_mat4View = glm::lookAt(glm::vec3(0, 0, -1), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
m_mat4Projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f);
m_mat4Model = glm::mat4(1.0f);
m_mat4Model = glm::translate(m_mat4Model, glm::vec3(m_fXTranslate, m_fYTranslate, m_fZTranslate));
m_mat4Model = glm::rotate(m_mat4Model, glm::radians(m_fXRotateDegree), glm::vec3(1.0f, 0, 0));
m_mat4Model = glm::rotate(m_mat4Model, glm::radians(m_fYRotateDegree), glm::vec3(0, 1.0f, 0));
m_mat4Model = glm::rotate(m_mat4Model, glm::radians(m_fZRotateDegree), glm::vec3(0, 0, 1.0f));
m_mat4Model = glm::scale(m_mat4Model, glm::vec3(m_fXScale, m_fYScale, m_fZScale));
m_mat4ProjectionModelView = m_mat4Projection * m_mat4View * m_mat4Model;
glUniformMatrix4fv(m_iProjectionModelViewIDUniform, 1, GL_FALSE, &m_mat4ProjectionModelView[0][0]);
glEnable(GL_TEXTURE_2D);
SetTexturesImages(panoramaFrame);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
glFlush();

glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);

}

对应QOpenGLWidget代码如下。
void QMyOpenGLWidget::initializeGL()
{
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->initializeOpenGLFunctions();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

void QMyOpenGLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}

void QMyOpenGLWidget::paintGL()
{
makeCurrent();
if (nullptr != m_Picture.pTextureData[0] && nullptr != m_pDrawer)
{
this->m_pDrawer->Draw(this->width(), this->height(), m_Picture);
}
}

void QMyOpenGLWidget::drawPicturepaintGL()
{
this->update();
return;
}
对应顶点着色器和片段着色器代码如下。
#version 330 core
uniform mat4 ProjectionModelView;
attribute vec4 vertexCoord;
attribute vec2 textureCoord;
varying vec2 outTextureCoord;

void main(void){
gl_Position = ProjectionModelView * vertexCoord;
outTextureCoord = textureCoord;
}

varying vec2 outTextureCoord;
uniform sampler2D textureY;

void main(void)
{
vec3 bgr;
bgr = texture2D(textureY, outTextureCoord).rgb;
gl_FragColor = vec4(bgr, 1.0);
}

2个回答

w774937253
Calarence 我也用这篇文章尝试了,没有出现图片。我是Qt5.7
接近 3 年之前 回复
lzg0610210238
lzg0610210238 这篇文章有看过,没有发现什么特别的。
接近 3 年之前 回复

楼主,你解决了吗?3085259894 我的QQ,一起交流下

lzg0610210238
lzg0610210238 可能是QT框架的问题,重新调用QOpenglWidget中initializeGL()函数就OK。
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐