Macbook Pro上的glFrustum给出了意想不到的结果

时间:2016-02-25 21:00:28

标签: opengl lwjgl

我只是使用LWJGL 3设置一个简单的渲染器,当它在我的外部显示器上运行时,它看起来像我期望的但是当我在Macbook上调整它时它缩小了视口。然后,如果我移动窗口,它会修复它。这是我在初始化GL内容并在窗口调整大小时运行的代码。

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

如果我在外部显示器上运行它不会改变,它总是正确的。

1 个答案:

答案 0 :(得分: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));
}