我正在尝试使用两个单独的着色器程序来绘制两个对象。运行程序时,仅渲染使用第一个着色器程序的对象。此外,在运行后删除第一个着色器时,仅渲染第二个对象。
代码如下。任何帮助表示赞赏。
void display()
{
mat4 mod, p, v;
glBindBuffer (GL_ARRAY_BUFFER, buffer);
//First Shader
glUseProgram(lightProgram);
p = perspective(zoom*45.0, aspect, zNear, zFar);
v = getViewMatrix();
mod = translate(0.0, 0.0, -5.0);
GLint objectColorLoc = glGetUniformLocation(lightProgram, "objectColor");
GLint lightColorLoc = glGetUniformLocation(lightProgram, "lightColor");
glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);
glUniform3f(lightColorLoc, 1.0f, 0.5f, 1.0f);
model = glGetUniformLocation( lightProgram, "model" );
view = glGetUniformLocation( lightProgram, "view" );
projection = glGetUniformLocation( lightProgram, "projection" );
glUniformMatrix4fv(projection, 1, GL_TRUE, &p.m[0][0]);
glUniformMatrix4fv( view, 1, GL_TRUE, &v.m[0][0] );
glBindVertexArray(containerVAO);
glUniformMatrix4fv( model, 1, GL_TRUE, &mod.m[0][0] );
glDrawArrays( GL_TRIANGLES, 0, 36 );
glBindVertexArray(0);
//glDeleteProgram(lightProgram);
//Model 2
//Second Shader
glUseProgram(lampProgram);
model = glGetUniformLocation( lampProgram, "model" );
view = glGetUniformLocation( lampProgram, "view" );
projection = glGetUniformLocation( lampProgram, "projection" );
glUniformMatrix4fv(projection, 1, GL_TRUE, &p.m[0][0]);
glUniformMatrix4fv( view, 1, GL_TRUE, &v.m[0][0] );
mod = translatevec3(lightPos);
glBindVertexArray (lightVAO);
glUniformMatrix4fv( model, 1, GL_TRUE, &mod.m[0][0] );
glDrawArrays( GL_TRIANGLES, 0, 36 );
glBindVertexArray(0);
}
此外,我正在使用
创建我的程序lightProgram = glCreateProgram();
glAttachShader(lightProgram, lightVertShader);
glAttachShader(lightProgram, lightFragShader);
glLinkProgram(lightProgram);
lampProgram = glCreateProgram();
glAttachShader(lampProgram, lampVertShader);
glAttachShader(lampProgram, lampFragShader);
glLinkProgram(lampProgram);
感谢。