将矩阵变换应用于边界框(BoundingBox)。

8

我有一个模型矩阵,用于跟踪网格在我的世界中的位置。每次调用 glRotate()glTranslate() 时,我都会相应地调用 modelMatrix.rotate()modelMatrix.translate(),这似乎是正确的。

现在我需要更新与我的每个模型相关联的边界框。我正在使用 libGDX 框架,在 此处BoundingBox 类中,有一个名为 mul() 的方法,应该允许我将矩阵应用于边界框,但值没有被正确更新,我认为可能是我尝试应用它的方式不对。有什么想法吗?

以下是我相关的代码:

gl.glPushMatrix();

// Set the model matrix to the identity matrix
modelMatrix.idt();

// Update the orbit value of this model
orbit = (orbit + ORBIT_SPEED * delta) % 360;
gl.glRotatef(orbit, 1.0f, 1.0f, 0);

// Update the model matrix rotation
modelMatrix.rotate(1.0f, 1.0f, 0, orbit);

// Move the model to it's specified radius
gl.glTranslatef(0, 0, -ORBIT_DISTANCE);

// Update the model matrix translation
modelMatrix.translate(0, 0, -ORBIT_DISTANCE);

// Update the bounding box
boundingBox.mul(modelMatrix);

if (GameState.DEBUG)
{
    renderBoundingBox(gl, delta);
}

// Bind the texture and draw
texture.bind();
mesh.render(GL10.GL_TRIANGLES);

gl.glPopMatrix();

你确定在绘制边界框之前已经加载了身份吗? - aacotroneo
不,我在绘制模型和边界框之后才调用glLoadIdentity() - DRiFTy
2
我认为你应该这样做。因为你正在对bb进行两次变换。在renderbb中进行一次push / pop操作。保留投影。如果您想测试转换,不要加载identity也不要调用mul()。这样,您将使用与模型相同的转换绘制bb。应该可以工作。 - aacotroneo
为什么要使用两个不同的库进行相同的计算呢?也许它们会做出不同的假设(例如,一个可能顺时针旋转,而另一个则逆时针旋转)。只需在您的modelMatrix上计算这些矩阵操作一次,并使用glLoadMatrix()即可。 - Mr. Berna
1个回答

0
矩阵乘法的计算顺序很重要。你能否尝试使用ModelMatrix * Box呢?我认为这可能是问题所在。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接