Qtopengl,为什么不能使用不同的 vbo 绘制两个立方体

Qtopengl, why can not draw two cubes using different vbo

我在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);

建议使用.setUniformValue() methods of the QOpenGLShaderProgram对象来设置制服的值。

但问题是,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());