修改此处的示例http://doc.qt.io/qt-5/qtquick-visualcanvas-scenegraph.html。我相信我有一个正确的OpenGL程序,但它似乎没有正确使用Qt的包装类进行渲染。如上面的示例所示,在连接完所有内容之后,我只修改了绘制功能。如下所示:
void MainScreenRenderer::paint()
{
if (!m_ShaderProgram) {
initializeOpenGLFunctions();
m_ShaderProgram = new QOpenGLShaderProgram();
m_ShaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute vec4 vertex;"
"uniform mat4 mvp;"
""
"void main() {"
" gl_Position = mvp * vertex;"
"}");
m_ShaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,
"void main() {"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
"}");
m_ShaderProgram->bindAttributeLocation("vertex", 0);
m_ShaderProgram->link();
}
glViewport(0, 0, m_ViewportSize.width(), m_ViewportSize.height());
m_ShaderProgram->bind();
m_ShaderProgram->enableAttributeArray(0);
GLfloat values[] = {
-1.0, -1.0, +0.0, +0.0,
+1.0, -1.0, +0.0, +0.0,
+0.0, +1.0, +0.0, +0.0,
};
QMatrix4x4 model;
QMatrix4x4 view;
view.lookAt(QVector3D(0.0, 0.0, 10.0), QVector3D(0.0, 0.0, 0.0),
QVector3D(0.0, 1.0, 0.0));
QMatrix4x4 projection;
float aspect = m_ViewportSize.width() / ((m_ViewportSize.height()) ?
m_ViewportSize.height() : 1);
projection.perspective(45.0, aspect, 1.0, 1000.0);
m_MVP = model * view * projection;
m_ShaderProgram->setAttributeArray(0, GL_FLOAT, values, 4);
m_ShaderProgram->setUniformValue("mvp", m_MVP);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
m_ShaderProgram->disableAttributeArray(0);
m_ShaderProgram->release();
}
无论我拉回相机多远,或者重新调整窗口大小,投影似乎都没有效果,它只是呈现红色到整个窗口,甚至不接受我刚刚告诉它渲染三角形的事实3分。如下所示。
在评论之后,已经进行了以下更改,但现在我看到的只是一个黑屏。
... // Vertex Shader
gl_Position = mvp * vec4(vertex, 1.0);"
...
... // Data
GLfloat values[] = {
-1.0, -1.0, +0.0,
+1.0, -1.0, +0.0,
+0.0, +1.0, +0.0,
};
...
... // Vertex Attribution Setup
m_ShaderProgram->setAttributeArray(0, GL_FLOAT, values, 3); // Tuple is only 3 tightly packed.
...
答案 0 :(得分:0)
鉴于我对你的模型视图和投影矩阵的看法,如果你将相机放在z = 1.0处,这个三角形就会位于你的近剪裁平面上。尝试将眼睛位置沿z轴向下移动几个单位。理想情况下,你会从NDC空间(身份模型视图和投影矩阵)开始,然后一旦你在屏幕上看到一个三角形,就开始引入相机变换;婴儿步骤;) - Andon M. Coleman 3月6日20:33