在OpenGL顶点着色器中,w是什么,为什么除以w? [英] In OpenGL vertex shaders, what is w, and why do I divide by it?

查看:740
本文介绍了在OpenGL顶点着色器中,w是什么,为什么除以w?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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是什么,为什么除以w?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆