When does the transition from clip space to screen coordinates happen?

霸气de小男生 提交于 2019-12-18 10:13:57

问题


I was studying the rendering pipeline and when I got to the clipping stage it was explained that from the view (eye or camera) space we have to pass to the clip space, also called normalized device space (NDC), that is a cubic space from -1 to 1.

However, now I don't understand when the passage from this space to the screen coordinates space happens:

  1. Right after clipping and before rasterization?

  2. After rasterization and before scissor and z-test?

  3. At the end just before writing on the frame buffer?


回答1:


No, clip space and NDC space are not the same thing.

Clip space is actually one step away from NDC, all coordinates are divided by Clip.W to produce NDC. Anything outside of the range [-1,1] in resulting NDC space corresponds to a point that is outside of the clipping volume. There is a reason the coordinate space before NDC is called clip space ;)

Strictly speaking, however, NDC space is not necessarily cubic. It is true that NDC space is a cube in OpenGL, but in Direct3D it is not. In D3D the Z coordinate in NDC space ranges from 0.0 to 1.0, while it ranges from -1.0 to 1.0 in GL. X and Y behave the same in GL and D3D (that is, they range from -1.0 to 1.0). NDC is a standard coordinate space, but it has different representation in different APIs.

Lastly, NDC space to screen space (AKA window space) occurs during rasterization and is defined by your viewport and depth range. Fragment locations really would not make sense in any other coordinate space, and this is what rasterization produces: fragments.


Update:

Introduced in OpenGL 4.5, the extension GL_ARB_clip_control allows you to adopt D3D's NDC convention in GL.

Traditional OpenGL behavior is:

glClipControl (GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE);

Direct3D behavior can be achieved through:

glClipControl (GL_UPPER_LEFT, GL_ZERO_TO_ONE); // Y-axis is inverted in D3D



回答2:


Clip space and NDC (normalized device coordinates) are not the same thing, otherwise they wouldn't have different names.

Clip space is where the space points are in after the point transformation by the projection matrix, but before the normalisation by w.

NDC space is the space points are in after the normalisation by w.

http://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/projection-matrix-GPU-rendering-pipeline-clipping

Camera space --> 
x projection matrix ---> 
Clip space (before normalisation) --->
Clipping ---> 
Normalisation by w (x/w, y/w, z/w) --->
NDC space (in the range [-1, 1] in x and y)



回答3:


Apparently, according to Apple, clip space is the same as NDC.

https://developer.apple.com/documentation/metal/hello_triangle

Quote:

"The main task of a vertex function (also known as a vertex shader) is to process incoming vertex data and map each vertex to a position in the viewport. This way, subsequent stages in the pipeline can refer to this viewport position and render pixels to an exact location in the drawable. The vertex function accomplishes this task by translating arbitrary vertex coordinates into normalized device coordinates, also known as clip-space coordinates."

Another quote from comments in sample code:

"The output position of every vertex shader is in clip space (also known as normalized device coordinate space, or NDC)."

Perhaps this is because the tutorial is in 2D? Misleading statements..




回答4:


NDC space is clip space, NDC space to window space is done by hardware, happened after NDC and before rasterization.

There is API to set the width and height, the default value is same size with window size.

// metal
func setViewport(_ viewport: MTLViewport)
// OpenGL
void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);

NDC space for OpenGL, xyz range [-1, 1]. For Metal, Z is from 0 to 1

NDC space is usually left hand system.



来源:https://stackoverflow.com/questions/21841598/when-does-the-transition-from-clip-space-to-screen-coordinates-happen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!