Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

opengl - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...