I am trying to get the correct world postion and size of the bounds for a mesh, however the size of the bounds are scaled by the position of the mesh.
The bounds of the mesh itself are correct, but trying to get them into world space does not work.
The code for rendering the bounds:
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;
}
CodePudding user response:
size
is a vector, but not a position. So it has to be
bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 1.0);
bounds.size = transformation * glm::vec4(m_Mesh.bounds.size, 0.0);