opengl 网格的边界按其世界位置缩放

brccelvz  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(129)

我试图获得正确的世界位置和网格边界的大小,但是边界的大小是由网格的位置缩放的。
网格本身的边界是正确的,但尝试将它们放入世界空间中是不可行的。
照片:12
用于呈现边界的代码:

void MeshRenderer::drawBounds() // FIXME: world bounds are scaled by the transforms position
{
    glm::mat4 transformation = entity->transform.getTransformationMatrix();
    bounds.center = transformation * glm::vec4(m_Mesh.bounds.center, 1.0);
    bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 1.0);

    bounds.updateCornerVertices();
    bounds.draw();
}

void Bounds::updateCornerVertices()
{
    glm::vec3 halfSize = size * 0.5f;
    auto verts = GeomUtil::createLineCubeVertices(
        center + glm::vec3(-halfSize.x, halfSize.y, -halfSize.z),
        center + glm::vec3(-halfSize.x, -halfSize.y, -halfSize.z),
        center + glm::vec3(halfSize.x, -halfSize.y, -halfSize.z),
        center + glm::vec3(halfSize.x, halfSize.y, -halfSize.z),
        center + glm::vec3(-halfSize.x, halfSize.y, halfSize.z),
        center + glm::vec3(-halfSize.x, -halfSize.y, halfSize.z),
        center + glm::vec3(halfSize.x, -halfSize.y, halfSize.z),
        center + glm::vec3(halfSize.x, halfSize.y, halfSize.z));

    m_Vertexbuffer.setData(&verts[0], 48);
}
void Bounds::draw() const
{
    Renderer::shaderColor.use();
    Renderer::shaderColor.setMat4("_ModelMatrix", glm::mat4(1.0));
    m_Vertexbuffer.bind();
    glDrawArrays(GL_LINES, 0, 48);
    m_Vertexbuffer.unbind();
}

glm::mat4 Transform::getTransformationMatrix() const
{
    glm::mat4 matrix(1.0f);
    matrix = glm::translate(matrix, position);
    matrix = glm::scale(matrix, scale);
    matrix = glm::rotate(matrix, glm::radians(rotation.x), GeomUtil::X_AXIS); // TODO: Figure out how rotations work (quaternions)
    matrix = glm::rotate(matrix, glm::radians(rotation.y), GeomUtil::Y_AXIS);
    matrix = glm::rotate(matrix, glm::radians(rotation.z), GeomUtil::Z_AXIS);

    if (entity->parent)
        matrix = entity->parent->transform.getTransformationMatrix() * matrix;

    return matrix;
}
fxnxkyjh

fxnxkyjh1#

size是一个向量,但不是一个位置。所以它必须是
bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 1.0);

bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 0.0);

相关问题