• Create Account

The 3D graphics pipeline: I don't understand why clip space is a cube, or how it relates to normalized device coordinates

8 replies to this topic

#1axefrog  Members   -  Reputation: 177

Like
0Likes
Like

Posted 13 October 2013 - 11:37 AM

In the graphics pipeline, I understand that we take our models, which have localized coordinate systems that are really only relative to themselves, and we project them into our world view, by transforming their coordinates appropriately.

I also understand that we have a camera and we transform the vertices in the world view so that they are relative to the camera, as though the camera were a fixed object and we are moving the world around relative to the camera in order to create the illusion of a moving camera.

I think I kind of also get that we have "clip space", which seems to be the area of vertices that we'll see, because they are inside the perspective projection from the front clipping plane (the camera lens, effectively) to the back clipping plane. And this clipped perspective area is I guess sort of a truncated pyramid in shape.

Is clip space a cube because we kind of un-distort the aforementioned truncated pyramid volume back into a cube? And if so, why do we need to then scale by W to get normalized device coordinates? Wouldn't the conversion of the perspective projection to a clip space cube automatically scale the things at the back down to a smaller size?

I'm probably understanding this all wrong. Any help to understand this part of the pipeline would be much appreciated.

I'm blogging about my journey to learn 3D graphics and game programming: http://nathanridley.com

#2imoogiBG  Members   -  Reputation: 460

Like
1Likes
Like

Posted 13 October 2013 - 03:45 PM

The clip space is not a cube(not always). http://www.lighthouse3d.com/tutorials/view-frustum-culling/

The w component division i needed because you represent vertices not in 3D space but in a homogenous space. (in homogenus space p( 2,4,8, 2) and v(1,2,4,1) are the same point in 3D space..., really read a book about it. That is really important)

To find the real answers to your questions, you must read a book(or at least an article) about linear algebra.( can't give you good sources in engish).

Edited by imoogiBG, 13 October 2013 - 03:48 PM.

#3Brother Bob  Moderators   -  Reputation: 6469

Like
1Likes
Like

Posted 13 October 2013 - 03:54 PM

The clip space is not a cube(not always). http://www.lighthouse3d.com/tutorials/view-frustum-culling/

The w component division i needed because you represent vertices not in 3D space but in a homogenous space. (in homogenus space p( 2,4,8, 2) and v(1,2,4,1) are the same point in 3D space..., really read a book about it. That is really important)

To find the real answers to your questions, you must read a book(or at least an article) about linear algebra.( can't give you good sources in engish).

The images at your link shows a outsider's view of the view space, not the clip space. The projection matrix transforms that view space into clip space, which often has the shape of a cuboid. In OpenGL, the visible coordinate range in clip space is from -1 to 1 along all three axes, forming a cube. Another common setup is a visible range of -1 to 1 along X and Y, but 0 to 1 along the Z-axis, thus almost making it a cube.

edit: Actually, the cube in clip space is formed by [X,Y,Z]/W. That may have been what you were talking about with the perspective division.

Edited by Brother Bob, 13 October 2013 - 04:04 PM.

#4axefrog  Members   -  Reputation: 177

Like
0Likes
Like

Posted 14 October 2013 - 05:59 AM

Thanks for the responses, though I don't think they break things down enough to properly address what I was asking

@imoogiBG - "read a book"? No suggestions? I actually have two books on order, though the first is a game dev math book, so maybe that will help.

@Brother Bob - I get the impression your response was more for imoogiBG than for me, as you recite facts that I've read already, but don't properly understand just yet. e.g. "which often has the shape of a cuboid" ... could you elaborate? Are we simply scaling down the back clipping plane of the view space so that it has the same dimensions as the front clipping plane?

I'm blogging about my journey to learn 3D graphics and game programming: http://nathanridley.com

#5imoogiBG  Members   -  Reputation: 460

Like
0Likes
Like

Posted 14 October 2013 - 09:23 AM

Thanks for the responses, though I don't think they break things down enough to properly address what I was asking

@imoogiBG - "read a book"? No suggestions? I actually have two books on order, though the first is a game dev math book, so maybe that will help.

@Brother Bob - I get the impression your response was more for imoogiBG than for me, as you recite facts that I've read already, but don't properly understand just yet. e.g. "which often has the shape of a cuboid" ... could you elaborate? Are we simply scaling down the back clipping plane of the view space so that it has the same dimensions as the front clipping plane?

#6Brother Bob  Moderators   -  Reputation: 6469

Like
2Likes
Like

Posted 14 October 2013 - 10:19 AM

@Brother Bob - I get the impression your response was more for imoogiBG than for me, as you recite facts that I've read already, but don't properly understand just yet. e.g. "which often has the shape of a cuboid" ... could you elaborate? Are we simply scaling down the back clipping plane of the view space so that it has the same dimensions as the front clipping plane?

The transformation is a bit more elaborate than that. If you look at the first image in imoogiBG's link and visualize that the camera icon is pulled back towards infinity, then the projection lines (the edges of the frustum) becomes parallel, and the view volume takes the shape of a cuboid. That's what the projection matrix and the perspective division do.

There are some additional linear scales applied as well to transform the cuboid into a unit shape. For example, OpenGL defines a point [X, Y, Z, W] as visible in clip space if:

1. -W < X < W
2. -W < Y < W
3. -W < Z < W

After perspective division, the inequalities become:

1. -1 < X/W < 1
2. -1 < Y/W < 1
3. -1 < Z/W < 1

which has the shape of a cube since it extends from -1 to 1 along all three axes. I believe Direct3D defines, or did at least, define the third inequality as 0 < Z/W < 1 instead, which is not a cube since all since all dimensions are not equal in size.

#7axefrog  Members   -  Reputation: 177

Like
0Likes
Like

Posted 14 October 2013 - 10:40 AM

Ah, that makes more sense, thanks for the explanation!

I'm blogging about my journey to learn 3D graphics and game programming: http://nathanridley.com

#8Jason Z  Crossbones+   -  Reputation: 3295

Like
0Likes
Like

Posted 15 October 2013 - 04:17 PM

Perhaps a small addition to Brother Bob's explanation:  The reason you want to end up with a cube is so that you can easily map the coordinates in that space to a rectangular region which is your render target.  Since the typical perspective views through a rectangular window is actually a frustum, it isn't trivial to know what vertices map to which coordinates (in fact that is what ray tracing does - definitely non-trivial).

In addition to easing this mapping, it also greatly simplifies clipping and culling of primitives at that stage of the pipeline.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

#9allingm  Members   -  Reputation: 366

Like
0Likes
Like

Posted 22 October 2013 - 11:57 PM

Personally I prefer to understand this from a mathematical perspective.  There seems to be a decent explanation here:
http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c10123/Deriving-Projection-Matrices.htm#page-3

PARTNERS