Heuristic42
Blog
Opengl
Meta
Rendering
1
comment
Nov 24 at 1:24
Matrices
the macbook i bought from your amazon store is not working.. th…
–
anonymous
edited
Sep 17 at 23:20
Writing custom C++ containers, iterators and value references
Generic containers are awesome. 1. The effort required to wr…
–
pknowles
created
Sep 17 at 23:16
Writing custom C++ containers, iterators and value references
Generic containers are awesome. 1. The effort required to wr…
–
pknowles
comment
Sep 15 at 19:08
DerBard: Custom Split Mechanical Keyboard Prototype
Hey. Thanks for your interest! I've uploaded the files here, bu…
–
pknowles
comment
Sep 13 at 15:06
DerBard: Custom Split Mechanical Keyboard Prototype
Hi! Is it possible to get some models so I can also make it?
–
anonymous
edited
Aug 14 at 17:55
On docker stealing routes and breaking the internet
Boy this is frustrating. The internet just doesn't work with do…
–
pknowles
edited
Aug 14 at 16:55
On docker stealing routes and breaking the internet
Boy this is frustrating. The internet just doesn't work with do…
–
pknowles
created
Aug 14 at 16:52
On docker stealing routes and breaking the internet
Boy this is frustrating. The internet just doesn't work with do…
–
pknowles
comment
Jul 13 at 6:48
Matrices
[deleted]
–
anonymous
comment
Jul 6 at 0:39
Matrices
[deleted]
–
anonymous
edited
Jun 19 at 20:22
DerBard: Custom Split Mechanical Keyboard Prototype
![DerBard Cover Image][1] In my last post I presented my que…
–
pknowles
comment
Jun 2 at 11:49
Matrices
[deleted]
–
anonymous
comment
Jun 2 at 10:31
Matrices
[deleted]
–
anonymous
created
May 30 at 5:22
Calling Babel from Django for React+JSX
Sharing my frustrations so you can enjoy them too... :) As o…
–
pknowles
edited
May 22 at 18:57
Microsoft Natural Ergonomic 4000 Replacement
![my old natural 4k][1] **They stopped making it!** I'm actu…
–
pknowles
comment
May 22 at 16:00
Prime Mover - Processor (final problem) [Spoilers!]
this should be fixed now
–
pknowles
edited
May 18 at 22:15
Clip Space
Clip space is a linearly dependent vector space between eye spa…
–
pknowles
created
May 18 at 21:56
Clip Space
Clip space is a linearly dependent vector space between eye spa…
–
pknowles
edited
May 18 at 19:50
Vector Spaces
A number of vector spaces are discussed below and widely used i…
–
pknowles
edited
May 18 at 19:43
Projection Matrix
A projection is fundamental to [cameras](/8/rendering/cameras/)…
–
pknowles
edited
May 18 at 18:32
Matrices
Matrices are 2D arrays of numbers, grouped as such to enable hi…
–
pknowles
edited
May 14 at 21:47
DerBard: Custom Split Mechanical Keyboard Prototype
In my last post I talked about a [MS Natural 4K replacement](/b…
–
pknowles
created
May 14 at 21:46
Matching complex shapes for a custom keyboard in Fusion360
For my [custom mechanical keyboard](/blog/55/derbard-custom-spl…
–
pknowles
comment
May 8 at 0:32
Matrices
[deleted]
–
anonymous
…
View All
Log in
Clip Space
leave this field blank to prove your humanity
Slug
*
A URL path component
Parent page
<root>
3D Rendering (Computer Graphics)
--- Cameras
--- Matrices
------ Projection Matrix
--- Vectors
--- 3D Geometry
------ triangle_meshes
--- Shading
------ Transparency and Alpha Blending
--- Lights
--- rasterization
------ Deep Image
--- Shadows
--- Vector Spaces
------ Tangent Space
------ Clip Space
--- rotations
--- images
------ mipmapping
--- materials
OpenGL Tutorials
--- Order Independent Transparency (OIT)
--- The Framebuffer
Pages About This Site
--- Contributing
--- Bugs
--- Why does this website exist?
The parent page this belongs to.
Article title
*
Article revisions must have a non-empty title
Article body
*
Clip space is a linearly dependent vector space between eye space and *normalized device coordinates* (NDC). See [Spaces](/18/rendering/spaces/). It is where the viewing volume is finally defined, by: $$ -w \geq x \geq w $$ $$ -w \geq y \geq w $$ $$ -w \geq z \geq w $$ Geometry is typically clipped to be inside this volume. This is somewhat straight forward for lines and more complex for triangles, which may require multiple triangles to render after clipping. By convention, vectors in clip space are normalized with a [perspective divide](/11/rendering/matrices/projection/#clip-space-and-the-perspective-divide) to take them to NDC. The purpose of this page is to cover some of the maths and geometric oddities when working in clip space. A good presentation of clipping in clip space is at [chaosinmotion.com](https://chaosinmotion.com/2016/05/22/3d-clipping-in-homogeneous-coordinates/comment-page-1/). # Interpolation When working in clip space, vectors can mostly be dealt with the same as 3D but just with another component. For exmaple, the point $p$ at $t$ along a line between two vertices $\mathbf{a}$ and $\mathbf{b}$ in clip space is simply: $$ \mathbf{p} = \mathbf{a} (1 - t) + \mathbf{b} t $$ Computing perspective correct depth $d$ with a perspective divide is then: $$ d = \mathbf{p}_z / \mathbf{p}_w $$ If instead $\mathbf{a}$ and $\mathbf{b}$ were normalized before interpolation, the result would be incorrect. As a general rule, perform the perspective divide at the last moment, right when a 2D screen position or non-linear depth is needed. # Line--Plane Intersections Starting with just the left plane, $-w \geq x$. This inequality defines a deceptively simple plane equation in cartesian coordinates: $$x + w = 0$$ Think of this as a plane in 4D space intersecting the origin and with the normal $(1, 0, 0, 1)$. To find the intersection with a line $\mathbf{o} + \mathbf{d} t$ , plug the line equation into $x - w = 0$ and solve for t: $$(\mathbf{o}_x + \mathbf{d}_x t) - (\mathbf{o}_w + \mathbf{o}_w t) = 0$$ $$t = -\frac{\mathbf{o}_x + \mathbf{o}_w}{\mathbf{d}_w + \mathbf{d}_x}$$ For the positive $x$ clipping plane: $$t = \frac{\mathbf{o}_x - \mathbf{o}_w}{\mathbf{d}_w - \mathbf{d}_x}$$ This can almost be thought of as intersecting with an axis aligned box, plus a linearly dependent $w$. Unfortunately that tantalising simplicity is not so. One surprise is that even in the case $\mathbf{o}_x = 0$ and $\mathbf{d}_x > 0$, $t$ may be negative because $w$ in $x - w = 0$ "out-runs" in the $+t$ direction. For a regular axis aligned intersection this wouldn't happen --- if a line moves in positive $x$; it must hit axis-aligned planes in that direction. ![enter image description here][1] It's easy to see this geometrically when thinking about a perspective frustum. The gradient of the line's direction is simply not low enough to intersect the right hand clipping plane in the positive direction. It still intersects the right hand plane, just at negative $t$. In fact this intersection occurrs outside the viewing volume, which is another surprise. Both left and right clipping planes place lower $t$ bounds on this line and the $x$ axis has no upper bound on $t$. This complicates a general intersection algorithm. # Inside and Outside An easy algorithm to clip a line to a box is to first compute the intersection times $t$ of the axis aligned planes. Then sort them into $t_0$ and $t_1$ for each axis --- there's only two, so it's not much of a sort. Finally $t_0 = \max(t_{x0}, t_{y0}, t_{z0})$ and $t_1 = \min(t_{x1}, t_{y1}, t_{z1})$. This works for a bounding box because the first of two intersections on each axis is always the lower bound of the box and the second is always the upper bound. As described above, the same does not work for clipping against perspective projections. ![perspective clipping][2] As seen in the figure above, intersections on opposing planes may be both lower (in this case, shown in green) or both upper bounds on $t$. What is needed is a test for the side of the plane the line hits. Continuing the example above for a line $\mathbf{o} + \mathbf{d} t$. In the general case, a line hits the front of a plane if its direction vector $\mathbf{d}$ is opposite the plane's normal vector $\mathbf{n}$. The normal vectors for clipping planes are simply: $$(1, 0, 0, 1) \text{left}$$ $$(-1, 0, 0, 1) \text{right}$$ $$(0, 1, 0, 1) \text{bottom}$$ $$(0, -1, 0, 1) \text{top}$$ $$(0, 0, 1, 1) \text{near}$$ $$(0, 0, -1, 1) \text{far}$$ If $\mathbf{d} \cdot (1, 0, 0, 1) < 0$, i.e. $\mathbf{d}_x + \mathbf{d}_w < 0$, then the line will hit the left clipping plane coming from the right hand side, meaning the time of intersection $t$ (see the section above) becomes an upper limit to clip the line's $t_1$ to. If the inequality is false then the line hits the left clipping plane coming from the left and $t$ should instead be a lower limit on the line's $t_0$. For an example of this in use, see [this frustum clipping shadertoy](https://www.shadertoy.com/view/ctV3zc). [1]: https://img.heuristic42.com/img/675ebe68cd3a.svg [2]: https://img.heuristic42.com/img/4ec909adeb9d.svg
Toggle Preview
Edit message
*
A description of the changes made
Discard Draft
Save Draft
leave this field blank to prove your humanity
Flag
the thing you clicked
for moderator attention.
Reason choice:
Spam, promoting, advertising without disclosure
Rude, inappropriate, generally offensive
Too arrogant or demeaning to others
Other
Reason:
The reason for raising the flag
Error