由于某种原因,我正在渲染的四边形不会显示,而只会渲染黑屏。我已经多次检查了代码,但找不到问题,也许有人可以看到我看不到的内容!
目的是要使四边形跟随摄像机,现在我只想用单色显示四边形,但是我得到的只是一个黑屏。我正在使用QOpenGLWindow
和QOpenGLFunctions
。
void CSLFWindow::renderQuad()
{
float x0 = -(float)1.f, y0 = -(float)1.f;
float x1 = (float)1.f, y1 = (float)1.f;
const QVector3D vertices[4] = {
QVector3D( x0, y0, 0.0f),
QVector3D( x0, y1, 0.0f),
QVector3D( x1, y1, 0.0f),
QVector3D( x1, y0, 0.0f)
};
const QVector3D normals[4] = {
QVector3D(0.0f, 0.0f,1.0f),
QVector3D(0.0f, 0.0f,1.0f),
QVector3D(0.0f, 0.0f,1.0f),
QVector3D(0.0f, 0.0f,1.0f)
};
const QVector2D texcoords[4] = {
QVector2D(0.0f, 1.0f),
QVector2D(0.0f, 0.0f),
QVector2D(1.0f, 0.0f),
QVector2D(1.0f, 1.0f)
};
const unsigned int indices[4] = { 3, 2, 1, 0 };
m_shaderProgram.enableAttributeArray("vVertices");
m_shaderProgram.enableAttributeArray("vTexCoords");
m_shaderProgram.enableAttributeArray("vNormals");
m_shaderProgram.setAttributeArray("vVertices", vertices);
m_shaderProgram.setAttributeArray("vTexCoords", texcoords);
m_shaderProgram.setAttributeArray("vNormals", normals);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indices);
m_shaderProgram.disableAttributeArray("vVertices");
m_shaderProgram.disableAttributeArray("vTexCoords");
m_shaderProgram.disableAttributeArray("vNormals");
}
和渲染:
void CSLFWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_shaderProgram.bind();
m_model.setToIdentity();
m_view = m_camera.toMatrix();
QMatrix4x4 modelMatrix = m_model ;
QMatrix4x4 modelViewMatrix = m_view * modelMatrix;
QMatrix4x4 mvp = m_projection * modelViewMatrix;
m_shaderProgram.setUniformValue("MV", modelViewMatrix);
m_shaderProgram.setUniformValue("MVP", mvp);
m_shaderProgram.setUniformValue("P", m_projection);
renderQuad();
m_shaderProgram.release();
}
我将投影矩阵设置为:
m_view.setToIdentity();
float aspect = h / w;
m_projection.setToIdentity();
m_projection.perspective(
m_fov,
aspect,
0.1f,
1000.0f);
这是我的相机参数:
m_cameraPos = QVector3D(0.0f, 0.0f, 3.0f);
m_cameraFront = QVector3D(0.0f, 0.0f, -1.0f);
m_cameraUp = QVector3D(0.0f, 1.0f, 0.0f);
QMatrix4x4 toMatrix()
{
QMatrix4x4 vMatrix;
vMatrix.setToIdentity();
vMatrix.lookAt(m_cameraPos, QVector3D(0.0f, 0.0f, 0.0f),
m_cameraUp);
return vMatrix;
}
这是我的顶点着色器:
#version 330 core
layout (location = 0)in vec3 vVertices;
layout (location = 1)in vec2 vTexCoords;
layout (location = 2)in vec3 vNormals;
uniform mat4 MV;
uniform mat4 MVP;
uniform mat4 P;
out vec2 FragTexCoord;
out vec3 FragNormals;
void main()
{
FragTexCoord = vTexCoords;
FragNormals = vNormals;
gl_Position = MVP * vec4(vVertices,1);
}
和我的片段着色器:
#version 330 core
out vec4 fragmentColor;
in vec2 FragTexCoord;
in vec3 FragNormals;
void main()
{
fragmentColor = vec4(1.0,1.0,1.0,1.0);
}
答案 0 :(得分:0)
嘿,我无法添加评论,但是您可以尝试更改顶点着色器吗?
void main()
{
FragTexCoord = vTexCoords;
FragNormals = vNormals;
gl_Position = vec4(vVertices,1);
}
如果没有看到任何内容,请告诉我,尝试更改索引的顺序
答案 1 :(得分:0)
我发现问题出在设置表面格式!当我删除format.setProfile(QSurfaceFormat :: CoreProfile);我看到四边形。但我不明白为什么会发生
将GL_QUAD
更改为GL_TRIANGLE_FAN
,它将起作用:
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, indices);
GL_QUAD
已过时,并已在核心配置文件中删除。
另见Legacy OpenGL - Removed functionality,OpenGL Context和Forward compatibility