I am using opengl in QT, and want to draw a colored cube and a whte cube(simulating a light source). I use two different shader program and two different vbo. But, I find that I can draw only one cube. What to do about it?
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);
It is recommended to use the .setUniformValue()
methods of the QOpenGLShaderProgram
object, to set the values of uniforms.
But the issue is, that glUniform
* set the value of a uniform variable (in the default uniform block) of the currently installed program.
You've to install the program (glUseProgram
/ m_shaderProgram->bind()
), before setting the uniforms:
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());