在 OpenGL 顶点着色器中,w 是什么,为什么要除以它? [英] In OpenGL vertex shaders, what is w, and why do I divide by it?
问题描述
void main(void)
{
vec4 clipCoord = glModelViewProjectionmatrix * gl_Vertex;
gl_Position = clipCoord;
gl_FrontColor = gl_Color;
vec3 ndc = clipCoord.xyz / clipCoord.w;
所以 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?
推荐答案
W 是三维顶点的第四个坐标;这个顶点称为齐次顶点坐标.
W is the fourth coordinate of a three dimensional vertex; This vertex is called homogeneous vertex coordinate.
简而言之,W 分量是划分其他向量分量的因子.当 W 为 1.0 时,齐次顶点坐标被标准化".要比较两个顶点,您应该将 W 值归一化为 1.0.
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.
想想顶点 (1,1,1,1).现在增加 W 值(w > 1.0).标准化位置正在缩放!它会到达原点.想想顶点 (1,1,1,1).现在减小 W 值(W <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.
除了缩放顶点坐标之外,W 坐标也是必需的,因为您必须将 4x4 矩阵(模型视图和/或投影矩阵)与 4x1 矩阵(顶点)相乘.
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:
这篇关于在 OpenGL 顶点着色器中,w 是什么,为什么要除以它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!