OpenGL:从原点翻译时拉伸的模型

时间:2015-01-14 16:34:42

标签: c++ opengl

我在太空飞行模拟器上工作是我的第一个真正的OpenGL项目。如果立方体没有位于原点,我很难正确渲染立方体。发生的事情是立方体从原点伸出,拉伸程度取决于从原点转换的数量。

我相信它与着色器无关,因为我尝试了两个具有相同结果的不同顶点着色器。其中一个着色器是一个非常简单的着色器:

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;

// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;

void main(){    

    // Output position of the vertex, in clip space : MVP * position
    gl_Position =  MVP * vec4(vertexPosition_modelspace,1);

    // The color of each vertex will be interpolated
    // to produce the color of each fragment
    fragmentColor = vec3(255,255,255);
}

我觉得很明显问题不在于着色器。

当立方体在每个方向上翻译1个单位时,它看起来像这样:

enter image description here

在正Z方向上平移相机时,立方体会拉伸更多以跟随相机。

翻译前(相机位于(0,0,25),型号位于(5,5,5))

MVP矩阵:

(10.942525, 0.000000, 12.402489, 12.500000)   
(0.000000, 14.590034, 12.402489, 12.500000)   
(0.000000, 0.000000, 12.302468, 12.400000)   
(0.000000, 0.000000, 24.804977, 25.000000) 

转型矩阵:

(0.100000, 0.000000, 0.000000, 0.500000)   
(0.000000, 0.100000, 0.000000, 0.500000)   
(0.000000, 0.000000, 0.100000, 0.500000)   
(0.000000, 0.000000, 0.000000, 1.000000)   

enter image description here

翻译后(相机在正z方向翻译)

转型矩阵:

(0.100000, 0.000000, 0.000000, 0.500000)
(0.000000, 0.100000, 0.000000, 0.500000)
(0.000000, 0.000000, 0.100000, 0.500000)
(0.000000, 0.000000, 0.000000, 1.000000)

MVP矩阵:

(10.942525, 0.000000, 46.159237, 46.250000)
(0.000000, 14.590034, 46.159237, 46.250000)
(0.000000, 0.000000, 46.059216, 46.150002)
(0.000000, 0.000000, 92.318474, 92.500000)

enter image description here

这是我从模型中获取转换矩阵的代码:

glm::mat4 MovableObject::getTransformationMatrix() const
{
    return getTranslationMatrix() * getRotationMatrix() * getScaleMatrix();
}

glm::mat4 MovableObject::getScaleMatrix() const
{
    glm::mat4 scaleMatrix = glm::mat4(1.0);
    scaleMatrix[0][0] = scaleFactors[0];
    scaleMatrix[1][1] = scaleFactors[1];
    scaleMatrix[2][2] = scaleFactors[2];
    return scaleMatrix;
}

glm::mat4 MovableObject::getRotationMatrix() const
{
    return glm::toMat4(rotation);
}

glm::mat4 MovableObject::getTranslationMatrix() const
{
    glm::mat4 translationMatrix = glm::mat4(1.);
    translationMatrix[0][3] = center[0];
    translationMatrix[1][3] = center[1];
    translationMatrix[2][3] = center[2];
    return translationMatrix;
}

这里是绘制网格的渲染代码:

void Mesh::render(glm::mat4 modelMatrix, const Camera& camera)
{
    glUseProgram(programID);

    glm::mat4 view = camera.getViewMatrix();
    glm::mat4 pvm = camera.getProjectionMatrix() * view * modelMatrix;

    std::cout << "MVP Matrix: " << glm::to_string(pvm) << std::endl << std::endl;

    // Send our transformation to the currently bound shader,
    // in the "MVP" uniform
    glUniformMatrix4fv(matrixID, 1, GL_FALSE, &pvm[0][0]);
    glUniformMatrix4fv(modelMatrixID, 1, GL_FALSE, &modelMatrix[0][0]);
    glUniformMatrix4fv(viewMatrixID, 1, GL_FALSE, &view[0][0]);

    glUniform3f(lightID, 0.f, 0.f, 0.f); // TODO: hardwiring at 0 for now

    // Bind our texture in Texture Unit 0
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    // Set "myTextureSampler" sampler to user Texture Unit 0
    glUniform1i(textureID, 0);

    // 1st attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(
                          0,                  // attribute
                          3,                  // size
                          GL_FLOAT,           // type
                          GL_FALSE,           // normalized?
                          0,                  // stride
                          (void*)0            // array buffer offset
                          );

    // 2nd attribute buffer : UVs
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
    glVertexAttribPointer(
                          1,                                // attribute
                          2,                                // size
                          GL_FLOAT,                         // type
                          GL_FALSE,                         // normalized?
                          0,                                // stride
                          (void*)0                          // array buffer offset
                          );

    // 3rd attribute buffer : normals
    glEnableVertexAttribArray(2);
    glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
    glVertexAttribPointer(
                          2,                                // attribute
                          3,                                // size
                          GL_FLOAT,                         // type
                          GL_FALSE,                         // normalized?
                          0,                                // stride
                          (void*)0                          // array buffer offset
                          );

    // Draw the triangles !
    glDrawArrays(GL_TRIANGLES, 0, vertices.size() );

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);
}

以及获取相机矩阵的代码:

glm::mat4 Camera::getProjectionMatrix() const
{
    return glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 1000.0f);
}

glm::mat4 Camera::getViewMatrix() const
{
    glm::mat4 matrix = glm::toMat4(rotation);
    glm::vec3 up = glm::vec3(matrix[0][1], matrix[1][1], matrix[2][1]);
    glm::vec3 forward = -glm::vec3(matrix[0][2], matrix[1][2], matrix[2][2]);


    return glm::lookAt(center, center + forward, up);
}

2 个答案:

答案 0 :(得分:0)

glm使用列主要存储顺序,因此您必须以m [col] [row]的形式访问元素。您的getTranslationMatrix()函数不遵循该约定,因为转换向量应位于最后一列。打印矩阵编号的代码似乎也解释了转置的内容。

请注意,glm alread具有通过GLM_GTX_transform module创建转换矩阵(如旋转,缩放和平移)的功能。

答案 1 :(得分:0)

我必须假设您的函数存在问题,这些函数直接访问矩阵的成员

glm::mat4 MovableObject::getScaleMatrix() const {
  glm::mat4 scaleMatrix = glm::mat4(1.0);
  scaleMatrix[0][0] = scaleFactors[0];
  scaleMatrix[1][1] = scaleFactors[1];
  scaleMatrix[2][2] = scaleFactors[2];
  return scaleMatrix;
}

glm::mat4 MovableObject::getTranslationMatrix() const
{
  glm::mat4 translationMatrix = glm::mat4(1.);
  translationMatrix[0][3] = center[0];
  translationMatrix[1][3] = center[1];
  translationMatrix[2][3] = center[2];
  return translationMatrix;
}

你为什么这样做而不是更安全

glm::mat4 MovableObject::getScaleMatrix() const {
  return glm::scale(glm::mat4(), scaleFactors);
}

glm::mat4 MovableObject::getTranslationMatrix() const {
  return glm::translate(glm::mat4(), center);
}