为什么我的程序会翻译我的所有顶点?

时间:2017-09-16 23:33:40

标签: opengl

我有两个类,它们有自己的模型坐标,颜色等。我还有两个逻辑上相同的着色器程序。首先,我执行一个着色器程序,使用传统视图和投影矩阵编辑制服,然后我调用该类来唯一地编辑模型矩阵,然后绘制它的基元。紧接着,我做了同样的事情,但是使用第二个着色器程序,再次编辑制服,并调用第二个类来绘制它的基元并且它拥有自己独特的模型矩阵坐标。 在第二节课中,我每次迭代都会翻译模型矩阵,但不会在第一节课中翻译。由于某种原因,它也会翻译第一类中的模型矩阵,我不知道为什么?

源代码:

//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);

这两个类都有数据保护,我只是不了解如何在一个类中翻译模型矩阵,使得在使用两个着色器程序时,另一个类中的模型矩阵也会被翻译?当我为这两个类使用一个着色器程序时,翻译效果很好,但是当我使用两个着色器程序(每个类一个)时,翻译效果不是很好......

编辑:在我的项目工作之后,我发现当我使用相同的精确顶点和片段着色器编译和链接两个不同的着色器程序时,会发生同样的问题,并且在我绘制之前只使用每个着色器程序每节课。所以现在我的问题更多的是:为什么在两次绘制之间使用两个相同的着色器程序会导致所有顶点/模型矩阵被翻译?

1 个答案:

答案 0 :(得分:0)

我弄清楚问题是什么。基本上,由于没有真正的方法直接退出着色器的执行,当我将着色器通过函数执行到程序的其他部分时,我的程序变得混乱。由于某种原因,该程序认为两个着色器程序同时被执行,因此模型矩阵没有得到一致的重置。为了解决这个问题,我限制了每个着色器的范围。我没有将着色器在同一个函数中执行然后传递给其他类,而是将每个着色器放在它所使用的相应类中。