[英]Bounds of mesh are scaled by its world postion
我正在嘗試獲取網格的正確世界位置和邊界大小,但是邊界的大小由網格的位置縮放。
網格本身的邊界是正確的,但試圖讓它們進入世界空間是行不通的。
渲染邊界的代碼:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.