1

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

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;

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

有人可以快速解释一下这个着色器在做什么吗?

4

2 回答 2

3

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

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);

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

于 2011-10-06T02:34:08.110 回答
2

之后的 x、y、z 和 w 的顺序。确定从 gl_Vertex 的 x、y、z 和 w 到点的 x、y、z 和 w 的映射。

这被称为搅拌。point = gl_Vertex相当于point = gl_Vertex.xyzwpoint = gl_Vertex.yxzw产生一个与 gl_Vertex 等效的点,其中 x 和 y 值交换。

于 2011-10-06T02:28:50.570 回答