1 回答

TA貢獻(xiàn)1775條經(jīng)驗 獲得超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 方法來獲取模型視圖和投影矩陣。我改為使用我已經(jīng)在其他類中創(chuàng)建的矩陣并將這些矩陣作為參數(shù)傳遞。然后,我將矩陣轉(zhuǎn)換為緩沖區(qū),以便我可以使用它們。倒帶它們之后,我終于能夠從 gluProject 中獲得一個點的正確屏幕坐標(biāo)。
即使問題解決了,我仍然不知道為什么這些行不起作用:
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
System.out.println("modelview:");
displayFloatBuffer(modelview);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
添加回答
舉報