1 回答
TA贡献1775条经验 获得超11个赞
我找到了解决我的问题的方法,并且效果很好。这是我的新 get2DFrom3D 方法:
public Vector2f get2DFrom3D(float x, float y, float z)
{
FloatBuffer screen_coords = BufferUtils.createFloatBuffer(4);
IntBuffer viewport = BufferUtils.createIntBuffer(16);
FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
FloatBuffer projection = BufferUtils.createFloatBuffer(16);
Matrix4f modelviewMatrix = new Matrix4f();
Matrix4f transformationMatrix = new Matrix4f();
Matrix4f.mul(transformationMatrix
, Maths.createViewMatrix(camera)
, modelviewMatrix);
modelviewMatrix.store(modelview);
modelview.rewind();
projectionMatrix.store(projection);
projection.rewind();
GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
boolean result = GLU.gluProject(x, y, z, modelview, projection, viewport, screen_coords);
if (result)
{
Vector2f vector = new Vector2f((int)screen_coords.get(0), (int)(screen_coords.get(1)));
return vector;
}
else
{
return null;
}
}
而不是使用 glGetFloat 方法来获取模型视图和投影矩阵。我改为使用我已经在其他类中创建的矩阵并将这些矩阵作为参数传递。然后,我将矩阵转换为缓冲区,以便我可以使用它们。倒带它们之后,我终于能够从 gluProject 中获得一个点的正确屏幕坐标。
即使问题解决了,我仍然不知道为什么这些行不起作用:
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
System.out.println("modelview:");
displayFloatBuffer(modelview);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
添加回答
举报