不同模型的转换不起作用

Transformations of different models not working

我正在使用带有着色器的 OpenGL; glm 数学。

在我的代码中,我有两个模型:modelA 和 modelB。我有一个 Transformable class,它可以在不同的轴上平移、缩放、旋转,然后将它们与一个变换矩阵相乘,我将用它来定义任何 'thing' 的变换。重要的是这个 Transformable class returns 变换矩阵,它会乘以一个模型矩阵来应用某种变换。 modelA 和 modelB 都是 Model 类型。这个class继承自Transformable,因为它是可变形的。因此,它具有returns变换矩阵的功能。问题在于所有这些都汇聚在一起:

class scene
{
public:
    scene();
    bool initialize();
    void render();

private:
    Model modelA, modelB;
    glm::mat4 modelMatrix;

    void passUniforms();
};

然后在初始化函数中:

initialize()
{
    modelA.load("file.obj");
    modelB.load("file2.3ds");
    // compile and link shaders
    // init lighting
    // init camera

    modelA.translate(glm::vec3(0.f, 10.f, 0.f));
    modelB.translate(glm::vec3(-10.f, 0.f, 0.f));

    modelMatrix *= modelA.getTransformationMatrix();
}

passUniforms()
{
    auto MVP = projectionMatrix * viewMatrix * modelMatrix;

    GLint MVP_id = glGetUniformLocation(programHandle, "MVP");
    glUniformMatrix4fv(MVP_id, 1, GL_FALSE, glm::value_ptr(MVP));

    // pass other uniforms
}

void render()
{
    passUniforms();
    modelA.render(programHandle);
    modelB.render(programHandle);
}

现在,让我们明确一点:两个模型都渲染得很好,而且我没有制作场景图。照明工作正常,相机可以查看模型,并且模型的纹理都很好。但是有一个问题:转换都是一样的!

一看就知道问题出在哪里了:我没有考虑modelB的变换,难怪OpenGL都是按照modelA的变换矩阵来变换的。说我蠢,或者我只是没在这儿做点什么;但我现在陷入了如何独立更新每个模型位置的问题。我的第一个解决方案是这样做:

initialize()
{
    // load models, compile shaders etc

    modelA.translate(glm::vec3(0.f, 10.f, 0.f));
    modelB.translate(glm::vec3(-10.f, 0.f, 0.f));

    modelMatrix *= modelA.getTransMatrix();
    passUniforms();

    modelMatrix = glm::mat4(1.0);

    modelMatrix *= modelB.getTransMatrix();
    passUniforms();
}

那也没用;两种模型仍然具有彼此相同的转换。所以,我认为这可能与我的顶点着色器有关,因为那是我应用转换的地方:

#version 430

layout (location = 0) in vec3 vp; // vertex position
layout (location = 1) in vec3 vt; // vertex textures
layout (location = 2) in vec3 vn; // vertex normals

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;
uniform mat4 MVP;

// other things

void main()
{
    // eye calculations

    gl_Position = MVP * vec4(vp, 1.0);
}

在撰写本文时,我正在考虑可以解决此问题的各种解决方案。不过,我认为对我自己的代码进行 objective 分析会更好,这样我就可以看到我哪里错了。我怎样才能让每个模型都通过相互独立的转换来渲染?我不希望 modelA 的转换也是 modelB 的转换。如果此处的信息不够,请询问您还需要哪些其他信息。我希望你能在这里理解我的困境。 :-)

虽然不清楚您在哪些功能中实施了您建议的第一个解决方案,但我认为这样的事情会奏效 -

initialize()
{
    modelA.load("file.obj");
    modelB.load("file2.3ds");
    // compile and link shaders
    // init lighting
    // init camera

    modelA.translate(glm::vec3(0.f, 10.f, 0.f));
    modelB.translate(glm::vec3(-10.f, 0.f, 0.f));
}
void render()
{
   modelMatrix *= modelA.getTransMatrix();
   passUniforms();
   modelA.render(programHandle);

   // use the appropriate func to find inverse here to get original modelMatrix
   // if it not identity
   modelMatrix *= modelA.getTransMatrix().inverse;
   modelMatrix *= modelB.getTransMatrix();
   passUniforms();
   modelB.render(programHandle);
}

即使您没有制作明确的场景图,如果您要渲染多个对象,也总会涉及某种简单的场景图。

您必须重置为单位矩阵并对每个对象分别进行矩阵乘法。

initialize()
{
    modelA.load("file.obj");
    modelB.load("file2.3ds");
    // compile and link shaders
    // init lighting
    // init camera


    modelB.translate(glm::vec3(-10.f, 0.f, 0.f));
}

passUniforms()
{
    auto MVP = projectionMatrix * viewMatrix * modelMatrix;

    GLint MVP_id = glGetUniformLocation(programHandle, "MVP");
    glUniformMatrix4fv(MVP_id, 1, GL_FALSE, glm::value_ptr(MVP));

    // pass other uniforms
}

void render()
{
    modelMatrix = glm::mat4(1.0);
    modelMatrix *= modelA.getTransformationMatrix();
    passUniforms();
    modelA.render(programHandle);

    modelMatrix = glm::mat4(1.0);
    modelMatrix *= modelB.getTransformationMatrix();
    passUniforms();
    modelB.render(programHandle);
}

不需要计算倒数。