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

<p>What I can tell you is that the general formula for viewport transformation as done by OpenGL is given as (taken from <a href="https://www.khronos.org/opengl/wiki/Vertex_Post-Processing#Viewport_transform" rel="nofollow noreferrer">wiki</a>)</p>

<p><a href="https://i.sstatic.net/BWAUb.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BWAUb.png" alt="enter image description here"></a></p>

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

<p>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</p>

<p><code>x = 0, y = 0, width = 200, height = 200</code> </p>

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

<p><code>x = -0.5, y = -0.5, width = nx, height = ny</code></p>

<p>If you substitute it in the above matrix you'll get</p>

<p>$ \frac{nx}{2} * x_{ndc} - \frac{1}{2} + \frac{nx}{2}$</p>

<p>for the $X$ value. This equals</p>

<p>$ \frac{nx}{2} * x_{ndc} + \frac{nx - 1}{2}$</p>

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

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

<p>Here the pixel <code>3,5</code> has the center <code>3.5, 5.5</code>. 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.</p>

<p><a href="https://i.sstatic.net/kL2i3.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kL2i3.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2018-05-17T17:49:55.947Z
Second point of this [answer](https://stackoverflow.com/a/17896385/3125070): "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)"
Saravanabalagi Ramachandran
CC BY-SA 4.0
2018-05-17T17:51:58.020Z
_"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...
Saravanabalagi Ramachandran
CC BY-SA 4.0
2018-05-17T18:24:56.783Z
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.
gallickgunner
CC BY-SA 4.0
2018-05-17T15:34:07.217Z
2018-05-17T20:00:01.713Z
2018-05-17T20:00:01.713Z
gallickgunner
<p>When we convert from canonical space to screen space,</p>

<p>To map x from -1 to 1 to 0 to width,</p>

<pre><code>-1 &lt; x &lt; 1
0 &lt; x+1 &lt; 2
0 &lt; x+1/2 &lt; 1
0 &lt; (x+1)w/2 &lt; w
0 &lt; xw/2 + w/2 &lt; w
</code></pre>

<p>Similarly for y,</p>

<pre><code>-1 &lt; y &lt; 1
0 &lt; y+1 &lt; 2
0 &lt; y+1/2 &lt; 1
0 &lt; (y+1)h/2 &lt; h
0 &lt; yh/2 + h/2 &lt; h
</code></pre>

<p><a href="https://i.sstatic.net/RAs4q.png" rel="noreferrer"><img src="https://i.sstatic.net/RAs4q.png" alt="enter image description here"></a></p>

<p>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)?</p>

<p><a href="https://i.sstatic.net/ePX1x.png" rel="noreferrer"><img src="https://i.sstatic.net/ePX1x.png" alt="enter image description here"></a></p>

<transformations>
Canonical Space to Screen Space: Why do we have the strange -0.5?
Saravanabalagi Ramachandran
# Canonical Space to Screen Space: Why do we have the strange -0.5?

(Asked by: username_0 on 2018-05-17)

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](https://i.sstatic.net/RAs4q.png)](https://i.sstatic.net/RAs4q.png)

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](https://i.sstatic.net/ePX1x.png)](https://i.sstatic.net/ePX1x.png)

**Tags:** transformations


---

## 1 answers

### Answer by username_1 on 2018-05-17. Score: 1

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](https://www.khronos.org/opengl/wiki/Vertex_Post-Processing#Viewport_transform))

[![enter image description here](https://i.sstatic.net/BWAUb.png)](https://i.sstatic.net/BWAUb.png)

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](https://i.sstatic.net/kL2i3.png)](https://i.sstatic.net/kL2i3.png)

> **Comments:**
> **username_0**: Second point of this [answer](https://stackoverflow.com/a/17896385/3125070): "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)"  
> **username_0**: _"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...  
> **username_1**: 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.  

---

