---
# System prepended metadata

title: Viewport
tags: [OpenGL]

---

# Viewport
NDC in OpenGL ranges from -1 to 1, and depth buffer ranges from 0 to 1. NDC will be transformed once again to Window (Screen) coordinates. (It's called Viewport Transform.) Then, the z (depth) value finally becomes from 0 to 1, here is how it works:
```cpp
glViewport(x, y, w, h);
glDepthRange(n, f);
```
$$
    \begin{bmatrix}x_w \\y_w \\z_w \\\end{bmatrix} =
    \begin{bmatrix}
    \frac{w}{2}x_n + \left( x + \frac{w}{2} \right) \\
    \frac{h}{2}y_n + \left( y + \frac{h}{2} \right) \\
    \frac{(f - n)}{2}z_n + \frac{(f + n)}{2} \\
    \end{bmatrix}
$$

## Reference
http://www.songho.ca/opengl/gl_transform.html#wincoord

###### tags: `OpenGL`