2016-02-25 62 views
3

我只是使用LWJGL 3设置了一个简单的渲染器,当它在我的外部显示器上运行时,它看起来像我期望的那样,但是当它在我的Macbook上调整大小时,会缩小视口。然后,如果我移动窗口它修复它。以下是我启动我的GL内容并调整窗口大小时运行的代码。Macbook Pro上的glFrustum给出了意想不到的效果

public void resize(int width, int height) 
{ 
    val near = 0.1 
    val far = 100.0 

    GL11.glViewport(0, 0, width, height); 

    GL11.glMatrixMode(GL11.GL_PROJECTION); 
    GL11.glLoadIdentity(); 

    float aspect = width/(float)height 
    float fov = (float)(45.0/360.0 * Math.PI) 

    float fH = Math.tan(fov) * near 
    float fW = fH * aspect 
    GL11.glFrustum(-fW, fW, -fH, fH, near, far); 

    GL11.glMatrixMode(GL11.GL_MODELVIEW); 
    GL11.glLoadIdentity(); 
} 

这里是我所看到的

错误 Looks Wrong Looks Right

如果我运行它在我的外部显示器上它不改变,它永远是对的。

+3

疯狂的猜测:你的内部显示器是一个视网膜显示器?它看起来像是从窗口左下角到两个屏幕截图中呈现的对象之间的距离差异的一个因素。 – JWWalker

+0

也许这可能有所帮助:http://wiki.lwjgl.org/wiki/Using_High_DPI_Mode – Amadeus

+0

我认为@JWWalker是对的!在您提到它之后,我在GLFW文档中发现了这一点:注意不要将窗口大小传递给glViewport或其他基于像素的OpenGL调用。窗口大小在屏幕坐标中,而不是像素。对于基于像素的调用,使用像素大小的framebuffer大小。 – CaseyB

回答

2

读取帧缓冲区的大小是这里的答案。我用用户传入的像素大小创建窗口,然后读取frameBuffer大小以传递给glViewport。

public Window(String title, int width, int height) 
{ 
    _errorCallback = GLFWErrorCallback.createPrint(System.err); 
    GLFW.glfwSetErrorCallback(_errorCallback); 

    if (GLFW.glfwInit() != GLFW.GLFW_TRUE) 
    { 
     throw IllegalStateException("Unable to initialize GLFW"); 
    } 

    GLFW.glfwDefaultWindowHints(); 
    GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE); 
    GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE); 

    _window = GLFW.glfwCreateWindow(width, height, title ?: "", 0, 0); 
    if (_window == 0L) 
    { 
     throw RuntimeException("Failed to create window"); 
    } 

    // Setup Callbacks 

    // Get the resolution of the primary monitor 
    GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()); 

    // Center our window 
    GLFW.glfwSetWindowPos(_window, (vidmode.width() - width)/2, (vidmode.height() - height)/2); 

    // Make the OpenGL context current 
    GLFW.glfwMakeContextCurrent(_window); 

    // Enable v-sync 
    GLFW.glfwSwapInterval(1); 

    // Make the window visible 
    GLFW.glfwShowWindow(_window); 
} 

然后我读取帧缓冲区大小传递到glViewport和glFrustum。

public Vector2 frameBufferSize() 
{ 
    IntBuffer bufferWidth = BufferUtils.createIntBuffer(4); 
    IntBuffer bufferHeight = BufferUtils.createIntBuffer(4); 

    GLFW.glfwGetFramebufferSize(_window, bufferWidth, bufferHeight); 

    return Vector2(bufferWidth.get(0), bufferHeight.get(0)); 
} 
相关问题