簡體   English   中英

在LWJGL中縮放到鼠標位置

[英]Zooming to Mouse position in LWJGL

我正在嘗試使用LWJGL實現對等軸測圖的縮放。 目前我有功能

public static void setCameraPosition(float x, float y) {

    x *= zoom;
    y *= zoom;

    cameraX = x;
    cameraY = y;

    x -= (Display.getWidth() / 2) * zoom;
    y -= (Display.getHeight() / 2) * zoom;

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    GLU.gluLookAt(x, y, 1f, x, y, 0, 0.0f, 1.0f, 0.0f);
}   

將相機中心設置為一個點(x,y),

public static Point getMouseCoordinates() {
    float x = Mouse.getX() * getZoom() + getCameraLeft();
    float y = (Display.getHeight() - Mouse.getY()) * getZoom() + getCameraTop();
    return new Point((int) x, (int) y);
}

返回當前鼠標坐標,以及

public static void setZoom(int newZoom) {

    if (newZoom >= 4) newZoom = 4;
    else if (newZoom <= 1) newZoom = 1;

    if (zoom == newZoom) return;

    float x = ?; <-----
    float y = ?; <-----

    zoom = newZoom;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,  0+Display.getWidth() * zoom , 0+Display.getHeight() * zoom, 0, 1, -1);
    setCameraPosition((int) x, (int) y);
}

可以將縮放比例設置為1到4之間的整數。如您所見,我想在將縮放比例更改為某個點后設置相機位置-需要計算該點,以便當前鼠標位置不變(也就是放大到鼠標位置,例如Google Maps所做的操作)。 我已經嘗試了2天,已經嘗試了很多東西,但是我只是想不出計算x和y的方程式。

請注意,所有返回和輸入的點都相對於地圖的位置,特別是相對於地圖的頂部(其頂角點為(0,0))。 getMouseCoordinates()函數中的值getCameraLeft()和getCameraTop()返回

public static float getCameraLeft() {
    return cameraX - zoom * (Display.getWidth() / 2);
}

public static float getCameraTop() {
    return cameraY - zoom * (Display.getHeight() / 2);
}

任何幫助,將不勝感激。 我希望自己不會太復雜。

我終於找到了正確的方程式:

        float x = getMouseCoordinates().getX() + (getCameraX() - getMouseCoordinates().getX()) * (float) newZoom / (float) zoom;
        float y = getMouseCoordinates().getY() + (getCameraY() - getMouseCoordinates().getY()) * (float) newZoom / (float) zoom;

無論如何,謝謝,我相信最終會有人給我正確的答案:)

暫無
暫無

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

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