Opengl Macbook Pro上的glFrustum给出了意想不到的结果
我只是使用LWJGL 3设置一个简单的渲染器,当它在我的外部显示器上运行时,它看起来像我期望的那样,但在我的Macbook上调整它的大小时,它会缩小视口。然后,如果我移动窗口,它会修复它。下面是我在初始化GL文件和调整窗口大小时运行的代码Opengl Macbook Pro上的glFrustum给出了意想不到的结果,opengl,lwjgl,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);
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();
}
这是我看到的
错误
对
如果我在外部显示器上运行它,它不会改变,它总是正确的。这里的答案是读取帧缓冲区的大小。我使用用户传入的像素大小创建窗口,然后读取要传递到视口的帧缓冲区大小
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));
}
胡乱猜测:你的内部显示器是视网膜显示器吗?在两个屏幕截图中,从窗口左下角到渲染对象的距离似乎有大约2倍的差异。也许这会有所帮助:我认为@JWWalker是对的!在您提到它之后,我在GLFW文档中发现了这一点:注意不要将窗口大小传递给glViewport或其他基于像素的OpenGL调用。窗口大小以屏幕坐标表示,而不是以像素表示。对于基于像素的调用,请使用帧缓冲区大小(以像素为单位)。