Qtopengl,为啥不能使用不同的vbo绘制两个立方体
Posted
技术标签:
【中文标题】Qtopengl,为啥不能使用不同的vbo绘制两个立方体【英文标题】:Qtopengl, why can not draw two cubes using different vboQtopengl,为什么不能使用不同的vbo绘制两个立方体 【发布时间】:2019-07-18 03:07:57 【问题描述】:我在 QT 中使用 opengl,并想绘制一个彩色立方体和一个白色立方体(模拟光源)。我使用两个不同的着色器程序和两个不同的 vbo。但是,我发现我只能画一个立方体。该怎么办?
m_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vertexShader.vert");
m_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fragmentShader.frag");
m_shaderProgram->link();
//m_shaderProgram->bind();
m_programId = m_shaderProgram->programId();
m_lightShaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/lightVertexShader.vert");
m_lightShaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/lightFragShader.frag");
m_lightShaderProgram->link();
//m_lightShaderProgram->bind();
m_lightProgramId = m_lightShaderProgram->programId();
GLint modelLoc = glGetUniformLocation(m_programId, "model");
GLint viewLoc = glGetUniformLocation(m_programId, "view");
GLint projectionLoc = glGetUniformLocation(m_programId, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, m_model.data());
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, m_camera.getViewMatrix().data());
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, m_camera.getProjectionMatrix().data());
// light source
GLint lightModelLoc = glGetUniformLocation(m_lightProgramId, "lightModel");
GLint lightViewLoc = glGetUniformLocation(m_lightProgramId, "lightView");
GLint lightProjectionLoc = glGetUniformLocation(m_lightProgramId, "lightProjection");
glUniformMatrix4fv(lightModelLoc, 1, GL_FALSE, m_lightModel.data());
glUniformMatrix4fv(lightViewLoc, 1, GL_FALSE, m_camera.getViewMatrix().data());
glUniformMatrix4fv(lightProjectionLoc, 1, GL_FALSE, m_camera.getProjectionMatrix().data());
// draw colored cube
m_shaderProgram->bind();
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// draw light source
m_lightShaderProgram->bind();
glBindBuffer(GL_ARRAY_BUFFER, m_lightVbo);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindBuffer(GL_ARRAY_BUFFER, 0);
【问题讨论】:
m_vbo 和 m_lightVbo 绑定相同的顶点数据 【参考方案1】:推荐使用QOpenGLShaderProgram
对象的.setUniformValue()
方法来设置制服的值。
但问题是,glUniform
* 设置了当前安装程序的统一变量(在默认统一块中)的值。
在设置制服之前,您必须安装程序(glUseProgram
/ m_shaderProgram->bind()
):
GLint modelLoc = glGetUniformLocation(m_programId, "model");
GLint viewLoc = glGetUniformLocation(m_programId, "view");
GLint projectionLoc = glGetUniformLocation(m_programId, "projection");
GLint lightModelLoc = glGetUniformLocation(m_lightProgramId, "lightModel");
GLint lightViewLoc = glGetUniformLocation(m_lightProgramId, "lightView");
GLint lightProjectionLoc = glGetUniformLocation(m_lightProgramId, "lightProjection");
m_shaderProgram->bind();
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, m_model.data());
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, m_camera.getViewMatrix().data());
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, m_camera.getProjectionMatrix().data());
m_lightShaderProgram->bind();
glUniformMatrix4fv(lightModelLoc, 1, GL_FALSE, m_lightModel.data());
glUniformMatrix4fv(lightViewLoc, 1, GL_FALSE, m_camera.getViewMatrix().data());
glUniformMatrix4fv(lightProjectionLoc, 1, GL_FALSE, m_camera.getProjectionMatrix().data());
【讨论】:
以上是关于Qtopengl,为啥不能使用不同的vbo绘制两个立方体的主要内容,如果未能解决你的问题,请参考以下文章
为啥绘制我的 OpenGL-ES VBO 网格会阻止其他三角形显示?