网格的边界按其世界位置缩放
Bounds of mesh are scaled by its world postion
我正在尝试获取网格边界的正确世界位置和大小,但是边界大小会根据网格位置进行缩放。
网格本身的边界是正确的,但试图将它们放入世界 space 是行不通的。
照片:1 and 2
渲染边界的代码:
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;
}
size
是一个向量,而不是一个位置。所以它必须是
bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 1.0);
bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 0.0);
我正在尝试获取网格边界的正确世界位置和大小,但是边界大小会根据网格位置进行缩放。
网格本身的边界是正确的,但试图将它们放入世界 space 是行不通的。
照片:1 and 2
渲染边界的代码:
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;
}
size
是一个向量,而不是一个位置。所以它必须是
bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 1.0);
bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 0.0);