簡體   English   中英

LWJGL獲取給定平面上的鼠標坐標

[英]LWJGL Get mouse coordinates on given plane

到目前為止,我已經有了獲取鼠標在世界上位置的代碼,但是我需要在給定的平面(z=0)上獲取它,我該怎么做呢? 這是我的領料代碼:

static public Vector3f getMousePositionIn3dCoords(int mouseX, int mouseY) {

    viewport.clear();
    modelview.clear();
    projection.clear();
    winZ.clear();;
    position.clear();
    float winX, winY;

    GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview );
    GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection );
    GL11.glGetInteger( GL11.GL_VIEWPORT, viewport );

    winX = (float)mouseX;
    winY = (float)mouseY;

    GL11.glReadPixels(mouseX, mouseY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);

    float zz = winZ.get();
    GLU.gluUnProject(winX, winY, zz, modelview, projection, viewport, position);
    Vector3f v = new Vector3f(position.get(0),position.get(1),position.get(2));
    return v ;
}

我已經解決了它,對於任何感興趣的人,這是我修改后的代碼,用於在z = 0時獲得世界上的x和y:

static public Vector3f getMouseOnPlaneZ(int mouseX, int mouseY)
{

    viewport.clear();
    modelview.clear();
    projection.clear();
    winZ.clear();;
    position.clear();
    float winX, winY;

    GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview );
    GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection );
    GL11.glGetInteger( GL11.GL_VIEWPORT, viewport );

    winX = (float)mouseX;
    winY = (float)mouseY;

    GLU.gluUnProject(winX, winY, 0.0f, modelview, projection, viewport, position);
    GLU.gluUnProject(winX, winY, 1.0f, modelview, projection, viewport, position1);

    float zeropoint, zeroperc;
    double posXt, posYt, posZt;
    posXt = position.get(0) - position1.get(0);
    posYt = position.get(1) - position1.get(1);
    posZt = position.get(2) - position1.get(2);

    if ((position.get(2) < 0.0 && position1.get(2) < 0.0) || (position.get(2) > 0.0 && position1.get(2) > 0.0))
        return null;

    zeropoint = 0.0f - (float)position.get(2);

    //Find the percentage that this point is between them
    zeroperc = (zeropoint / (float)posZt);

    Vector3f v = new Vector3f((float)position.get(0) + (float)(posXt * zeroperc),(float)position.get(1) + (float)(posYt * zeroperc),(float)position.get(2) + (float)(posZt * zeroperc));
    return v ;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM