In OpenGL vertex shaders, what is w, and why do I divide by it?

雨燕双飞 提交于 2019-12-17 21:49:25

问题


void main(void)
{
  vec4 clipCoord = glModelViewProjectionmatrix * gl_Vertex;
  gl_Position = clipCoord;

  gl_FrontColor = gl_Color;

  vec3 ndc = clipCoord.xyz / clipCoord.w;

So the clipCoord is just doing standard fixed pipeline transforms. Why do I divide by w, and what do I get from this?


回答1:


W is the fourth coordinate of a three dimensional vertex; This vertex is called homogeneous vertex coordinate.

In few words, the W component is a factor which divides the other vector components. When W is 1.0, the homogeneous vertex coordinates are "normalized". To compare two vertices, you should normalize the W value to 1.0.

Think of the vertex (1,1,1,1). Now increase the W value (w > 1.0). The normalized position is scaling! and it is going to the origin. Think of the vertex (1,1,1,1). Now decrease the W value (W < 1.0). The normalized position is going to an infinite point.

Apart from scaling vertex coordinates, the W coordinate is necessary since you have to multiply a 4x4 matrix (the model view and/or the projection matrices) with a 4x1 matrix (the vertex).

Of course, the Red Book is the definite guide:

Red Book Appendix



来源:https://stackoverflow.com/questions/2422750/in-opengl-vertex-shaders-what-is-w-and-why-do-i-divide-by-it

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