我在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);
答案 0 :(得分:0)
建议使用.setUniformValue()
对象的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());