这个顶点着色器在做什么?

What is this vertex shader doing?

本文关键字:什么 顶点      更新时间:2023-10-16

我最近接手了一个停滞不前的项目,一个团队成员几个月前辞职了。当我试图让自己加快速度时,我遇到了这个顶点着色器,我很难理解它在做什么:

uniform int axes;
varying vec4 passcolor;
void main()
{
  // transform the vertex
  vec4 point;
  if (axes == 0) {
     point = gl_Vertex;
  } else if (axes == 1) {
     point = gl_Vertex.xzyw;
  } else if (axes == 2) {
     point = gl_Vertex.xwzy;
  } else if (axes == 3) {
      point = gl_Vertex.yzxw;
  } else if (axes == 4) {
     point = gl_Vertex.ywxz;
  } else if (axes == 5) {
     point = gl_Vertex.zwxy;
  }
  point.z = 0.0;
  point.w = 1.0;
  // eliminate w point
  gl_Position = gl_ModelViewProjectionMatrix * point;
 passcolor = gl_Color;
}

我想更好地理解像这样的行:

point = gl_Vertex.xwzy;

我似乎找不到解释这个的文档。

谁能给一个快速的解释这个着色器是做什么?

我似乎找不到解释这个的文档。

GLSL规范非常清楚swizzle选择是如何工作的。

着色器所做的基本上是一种从vec4中选取两个轴的钝化方式。注意,point的Z和W在选择之后被覆盖。无论如何,它可以重写为:

vec2 point;
if (axes == 0) {
   point = gl_Vertex.xy;
} else if (axes == 1) {
   point = gl_Vertex.xz;
} else if (axes == 2) {
   point = gl_Vertex.xw;
} else if (axes == 3) {
   point = gl_Vertex.yz;
} else if (axes == 4) {
   point = gl_Vertex.yw;
} else if (axes == 5) {
   point = gl_Vertex.zw;
}
gl_Position = gl_ModelViewProjectionMatrix * vec4(point.xy, 0.0, 1.0);

所以它只是从输入顶点中选择两个坐标。我不知道为什么它需要这样做。

x, y, z和w的顺序。确定从gl_Vertex的x, y, z, w到point的x, y, z, w的映射。

这叫做搅拌。point = gl_Vertex等于point = gl_Vertex.xyzwpoint = gl_Vertex.yxzw的结果相当于gl_Vertex的x和y值交换。