Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My computer is a Mac pro with a 13 inch retina screen. The screen resolution is 1280*800 (default).

Using the following code:

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

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

Case 1 results in a triangle that fits the window.

Case 2 results in a triangle that is 1/4th the size of the window.

Half of viewport:

enter image description here

The GLFW documentation indicates the following (from 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.

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

like image 990
Jackson Zheng Avatar asked Apr 17 '16 05:04

Jackson Zheng


2 Answers

As Sabuncu said is hard to know what result should be correct without knowing how you draw the triangle.

But I guess your problems is related to the fact that with retina screen, when you use the 2.0 scale factor you need to render twice the pixels as you would with a regular screen - see here

The method you're after is shown just a few lines below your 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.

In your case I'm betting the framebuffer size you'll get will be twice the window size, and your gl viewport needs to match it.

like image 120
rickyviking Avatar answered Nov 12 '22 14:11

rickyviking


The frame-buffer size never needs to be equal to the size of the window, as of that you need to use 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.

Whenever you resize your window you need to retrieve the size of its frambuffer and update the Viewport according to it:

 glfwGetFramebufferSize(gWindow, &framebufferWidth, &framebufferHeight);

 glViewport(0, 0, framebufferWidth, framebufferHeight);
like image 3
t.niese Avatar answered Nov 12 '22 14:11

t.niese