4
$\begingroup$

When we convert from canonical space to screen space,

To map x from -1 to 1 to 0 to width,

-1 < x < 1
0 < x+1 < 2
0 < x+1/2 < 1
0 < (x+1)w/2 < w
0 < xw/2 + w/2 < w

Similarly for y,

-1 < y < 1
0 < y+1 < 2
0 < y+1/2 < 1
0 < (y+1)h/2 < h
0 < yh/2 + h/2 < h

enter image description here

What is the reason behind choosing a screen that has (xmin, ymin) = (0.5, 0.5) and (xmax, ymax) = (1920 - 0.5, 1080 - 0.5) where (nx, ny) = (1920, 1080)?

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

I can't really answer this without the context so post some links where this is done.

What I can tell you is that the general formula for viewport transformation as done by OpenGL is given as (taken from wiki)

enter image description here

Here x,y are the what your picture says xmin, ymin and width, height is xmax, ymax. Now there is no specific reason I can think of why you would wanna do a shift of -0.5 but the viewport allows us to draw to a specific portion of the screen.

For example, in video games you would want to draw the minimap on the lower left corner of the screen, hence you could provide the values

x = 0, y = 0, width = 200, height = 200

This would render a 200x200 minimap in the lower left corner. Your second picture does this but instead it chooses a value of

x = -0.5, y = -0.5, width = nx, height = ny

If you substitute it in the above matrix you'll get

$ \frac{nx}{2} * x_{ndc} - \frac{1}{2} + \frac{nx}{2}$

for the $X$ value. This equals

$ \frac{nx}{2} * x_{ndc} + \frac{nx - 1}{2}$

Which is the same as what is done in the picture you posted. Hope it helped somewhat.

EDIT:- 0.5 offset is commonly used to get to the pixel center. See this image as an example.

Here the pixel 3,5 has the center 3.5, 5.5. This is the 0.5 offset you are seeing. I was confused at first when you were talking about an offset of negative 0.5.

enter image description here

$\endgroup$
3
  • $\begingroup$ Second point of this answer: "The pixel shader deals with fragments, not vertices, but when you do make use of coordinates (via SV_Position for example), those coordinates are in screen space (offset by 0.5)" $\endgroup$ Commented May 17, 2018 at 17:49
  • $\begingroup$ "I can think of why you would wanna do a shift of -0.5" I can't either. But I can find that in many slides online. So there's this 0.5 offset happening somewhere... $\endgroup$ Commented May 17, 2018 at 17:51
  • $\begingroup$ I think he is talking about the 0.5 offset to get to the pixel "center". 0.5 makes sense but -0.5 doesn't for me. See my edited answer. $\endgroup$ Commented May 17, 2018 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.