Follow-up for: Calculating world coordinates from camera coordinates
我将 2D vector 与变换矩阵(OpenGL 的模型 View 矩阵)相乘,以从相机坐标中获取世界坐标。
我是这样计算的:
private Vector2f toWorldCoordinates(Vector2f position) {
glPushMatrix();
glScalef(this.zoom, this.zoom, 1);
glTranslatef(this.position.x, this.position.y, 0);
glRotatef(ROTATION, 0, 0, 1);
ByteBuffer m = ByteBuffer.allocateDirect(64);
m.order(ByteOrder.nativeOrder());
glGetFloatv(GL_MODELVIEW_MATRIX, m);
float x = (position.x * m.getFloat(0)) + (position.y * m.getFloat(4)) + m.getFloat(12);
float y = (position.x * m.getFloat(16)) + (position.y * m.getFloat(20)) + m.getFloat(28);
glPopMatrix();
return new Vector2f(x, y);
}
现在我也想反之亦然:计算世界中某个位置的相机坐标。我怎样才能反转这个计算?
最佳答案
要创建一个表示上述矩阵逆变换的矩阵,请反向应用变换,旋转和平移为负值,缩放为负值:
glRotatef(-ROTATION, 0, 0, 1);
glTranslatef(-this.position.x, -this.position.y, 0);
glScalef(1.0f / this.zoom, 1.0f / this.zoom, 1);
然后像以前一样乘以位置 vector 。
另一种方法是计算 inverse matrix ,不过这种方式就简单多了。
关于java - 逆向进行矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080477/