2015-07-20 71 views
1

我正在尝试使用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的功能)。我现在一直在努力尝试两天,我尝试了很多东西,但我无法弄清楚计算x和y的方程式。

请注意,所有返回并输入的点都是相对于地图的位置,特别是地图顶部的一部分(其顶角点为(0,0))。值getCameraLeft()和getCameraTop()在getMouseCoordinates()函数返回

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

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

任何帮助,将不胜感激。我希望,我没有表达自己太复杂。

回答

1

我终于找到了正确的公式:

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

谢谢反正,我敢肯定,最终会有人给我正确答案:)