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

标签 c++ opengl glsl vertex-shader

我最近接手了一个项目,该项目停滞不前,一名团队成员几个月前退出了。在努力让自己跟上速度的过程中,我遇到了这个顶点着色器,但我很难理解它在做什么:

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;

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

谁能快速解释一下这个着色器在做什么?

最佳答案

I can't seem to find documentation that explains this.

GLSL specification很清楚如何swizzle selection有效。

着色器基本上做的是一种从 vec4 中选取两个轴的钝化方法。请注意, 的 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);

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

关于c++ - 这个顶点着色器在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669680/

相关文章:

c++ - 初始化结构 vector 的 vector 的好方法(每个 vector 都有一个初始值)?

c++ - 使用 pokerstove 库提高性能

opengl - 我应该在 GPU 上还是在 CPU 上计算矩阵?

opengl-es - 如何将 GLSL #version 330 核心转换为 GLSL ES #version 100?

c++ - 收到 native 消息,但响应失败

c++ - friend 类声明

OpenGL - glDrawElements 与顶点数组对象

c++ - 渲染大块时出现纹理锯齿问题 : OpenGL

opengl - 如何使用深度缓冲区来存储索引

opengl - 什么和为什么GLSL textureGrad