我有两个具有自己的模型坐标,颜色等的类。我也有两个逻辑上相同的着色器程序。首先我执行一个着色器程序,用传统的视图和投影矩阵编辑制服,然后我调用该类来独特地编辑模型矩阵,然后绘制它的基元。紧接着,我做了完全相同的事情,但是使用第二个着色器程序,再次编辑制服,并调用第二个类来绘制它的基元,并且它是它自己独特的模型矩阵坐标。 在第二堂课中,我翻译每次迭代的模型矩阵,但不是在第一课。由于某种原因,它也翻译了第一堂课的模型矩阵,我不知道为什么?为什么我的程序会翻译我所有的顶点?
源代码:
//First shader program, update view and proj matrix, and have first class draw it's vertices
executable.Execute();
GLuint viewMatrix = glGetUniformLocation(executable.getComp(), "viewMatrix");
glUniformMatrix4fv(viewMatrix, 1, GL_FALSE, glm::value_ptr(freeView.getFreeView()));
GLuint projMatrix = glGetUniformLocation(executable.getComp(), "projectionMatrix");
glUniformMatrix4fv(projMatrix, 1, GL_FALSE, glm::value_ptr(projectionMatrix.getProjectionMatrix()));
temp.useClass(executable);
//Second Shader program, update view and proj matrix, and have second class draw it's vertices
executable2.Execute();
viewMatrix = glGetUniformLocation(executable2.getComp(), "viewMatrix");
glUniformMatrix4fv(viewMatrix, 1, GL_FALSE, glm::value_ptr(freeView.getFreeView()));
projMatrix = glGetUniformLocation(executable2.getComp(), "projectionMatrix");
glUniformMatrix4fv(projMatrix, 1, GL_FALSE, glm::value_ptr(projectionMatrix.getProjectionMatrix()));
temp2.useClass(executable2);
VertexShader:
#version 330 core
layout(location = 0) in vec3 positions;
layout(location = 1) in vec3 colors;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
out vec3 color;
void main()
{
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(positions, 1.0f);
color = colors;
}
第二顶点着色器在逻辑上是相同的,只有不同的变量名,和片段着色器只是输出颜色。
useClass功能(从一个类):
glBindVertexArray(tempVAO);
glm::mat4 modelMat;
modelMat = glm::mat4();
GLuint modelMatrix = glGetUniformLocation(exe.getComp(), "modelMatrix");
glUniformMatrix4fv(modelMatrix, 1, GL_FALSE, glm::value_ptr(modelMat));
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
useClass功能(从两个类):
glBindVertexArray(tempVAO);
for(GLuint i = 0; i < 9; i++)
{
model[i] = glm::translate(model[i], gravity);
GLuint modelMatrix = glGetUniformLocation(exe.getComp(), "modelMatrix");
glUniformMatrix4fv(modelMatrix, 1, GL_FALSE, glm::value_ptr(model[i]));
glDrawArrays(GL_POINTS, 0, 1);
}
glBindVertexArray(0);
两个类都有数据保护,我只是不明白怎么翻译在使用两个着色器程序时,一个类中的模型矩阵是否会使另一个类中的模型矩阵也被翻译?当我为这两个类使用一个着色器程序时,翻译工作正常,但没有太多当我使用两个着色器程序(每个类一个)...
编辑:在我的项目上工作多一点后,我发现当我编译并链接两个具有相同确切顶点和片段着色器的不同着色器程序时,会发生同样的问题,并且在我从每个类中绘制之前使用每个着色器程序。所以现在我的问题更多的是:为什么在绘制之间使用两个相同的着色器程序会导致所有的顶点/模型矩阵被翻译?
我没有看到任何错误。也许在你不显示的代码中...你确定使用不同的着色器吗?你是否使用'glUseProgram'来为每个班加上正确的ID? – Ripi2
是的,我将每个着色器程序实例的引用传递给它的唯一类,并从它们的类中编辑模型矩阵,然后绘制原语。当我使用其中一个着色器程序时,它工作得很好,当我使用相同的着色器程序两次时,事情运行良好,但是当我使用两个不同的着色器程序(即使它们在逻辑上相同)时,所有内容都会被翻译。 .. – Flare
你在哪里'glUseProgram'?统一的位置可以在任何时候通过'glGetUniformLocation'获取,并且在程序链接成功后('glLinkProgram')存储起来供以后使用,但是为了设置统一变量的值,程序必须是活动程序。这意味着'glUseProgram'必须在'glUniformMatrix4fv'之前完成。 – Rabbid76