相机计算坐标公式_根据相机坐标计算世界坐标

I have a world that is rendered in 2D and I'm looking at it from the top. Tjat looks like this (the floor tiles have no texture and only random green color yet):

Before rendering my entities, I transform the model-view matrix like this (while position is the position and zoom the zoom of the camera, ROTATION is 45):

glScalef(this.zoom, this.zoom, 1);

glTranslatef(this.position.x, this.position.y, 0);

glRotatef(ROTATION, 0, 0, 1);

Now I want to calculate the world coordinates for the current position of my camera. What I'm trying is to create a new matrix with glPushMatrix, then transform it the same way that the camera is transformed, and then get the matrix and multiply the given camera coordinate with it:

private Vector2f toWorldCoordinates(Vector2f position) {

glPushMatrix();

// do the same as when rendering

glScalef(this.zoom, this.zoom, 1);

glTranslatef(this.position.x, this.position.y, 0);

glRotatef(ROTATION, 0, 0, 1);

// get the model-view matrix

ByteBuffer m = ByteBuffer.allocateDirect(64);

m.order(ByteOrder.nativeOrder());

glGetFloatv(GL_MODELVIEW_MATRIX, m);

// calculate transformed position

float x = (position.x * m.getFloat(0)) + (position.y * m.getFloat(4)) + m.getFloat(12);

float y = (position.x * m.getFloat(1)) + (position.y * m.getFloat(5)) + m.getFloat(13);

System.out.println(x + "/" + y);

glPopMatrix();

return new Vector2f(x, y);

}

The problem now is: this works for the x coordinate, but the y coordinate is wrong and always 0. Have I misused the matrix somehow? Is there a "smoother" way of getting the world coordinates from the eye coordinates?

解决方案

The problem is with the way you're calling getFloat(). When you call it with an index on a ByteBuffer, the index is the number of bytes into the buffer at which to start reading the float, not the number of floats. You need to multiply each of your indices by 4:

float x = (position.x * m.getFloat(0)) + (position.y * m.getFloat(16)) + m.getFloat(48);

float y = (position.x * m.getFloat(4)) + (position.y * m.getFloat(20)) + m.getFloat(52);

However given that x is working for you already, I suspect you might also need to transpose your matrix co-ordinates, and so the correct code is:

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);

(By a co-incidence, transposing the first row of the matrix into the first column gives indices that are 4 times as great, so the 2 bugs cancel each other out in the case of x but not y).

If you're looking for a smoother way of doing it, look into using gluUnProject, although you may have to apply some additional transforms (it maps from window to object co-ordinates).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值