为什么视网膜屏幕坐标值是像素值的两倍

Why retina screen coordinate value is twice the value of pixel value

我的电脑是 Mac 专业版,配备 13 英寸视网膜屏幕。屏幕分辨率为1280*800(默认)。

使用以下代码:

gWindow = glfwCreateWindow(800, 600, "OpenGL Tutorial", NULL, NULL);

//case 1
glViewport(0,0,1600,1200);
//case 2
glViewport(0,0,800,600);

案例 1 生成一个三角形,该三角形适合 window。

案例 2 生成的三角形大小是 window 的 1/4。

一半视口:

GLFW 文档指出以下内容(来自 here):

While the size of a window is measured in screen coordinates, OpenGL works with pixels. The size you pass into glViewport, for example, should be in pixels. On some machines screen coordinates and pixels are the same, but on others they will not be. There is a second set of functions to retrieve the size, in pixels, of the framebuffer of a window.

为什么我的视网膜屏幕坐标值是像素值的两倍?

帧缓冲区大小永远不需要等于 window 的大小,因为您需要使用 glfwGetFramebufferSize:

This function retrieves the size, in pixels, of the framebuffer of the specified window. If you wish to retrieve the size of the window in screen coordinates, see glfwGetWindowSize.

每当您调整 window 大小时,您需要检索其 frambuffer 的大小并根据它更新视口:

 glfwGetFramebufferSize(gWindow, &framebufferWidth, &framebufferHeight);

 glViewport(0, 0, framebufferWidth, framebufferHeight);

正如Sabuncu所说,在不知道如何绘制三角形的情况下很难知道什么结果应该是正确的。

但我猜你的问题与视网膜屏幕有关,当你使用 2.0 比例因子时,你需要渲染两倍于普通屏幕的像素 - see here

您想要的方法显示在您的 GLFL 下面几行 link

There is also glfwGetFramebufferSize for directly retrieving the current size of the framebuffer of a window.

int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);

The size of a framebuffer may change independently of the size of a window, for example if the window is dragged between a regular monitor and a high-DPI one.

在您的情况下,我打赌您将获得的帧缓冲区大小将是 window 大小的两倍,并且您的 gl 视口需要与之匹配。

对于 Retina 显示器,默认帧缓冲区(渲染到 canvas 的帧缓冲区)是显示器分辨率的两倍。因此,如果显示器为 800x600,则内部 canvas 为 1600x1200,因此您的 viewpoert 应该为 1600x1200,因为这是进入帧缓冲区的 "window"。