为什么 'gl_Position' 与 'position' 的数据类型不同

Why is 'gl_Position' a different data type than 'position'

我正在从代码示例中学习 GLSL 着色器,但我对以下内容感到困惑:gl_Position 是 vec4 数据类型并且 position 是一个 vec3 数据类型,为什么?这个 'position' 变量到底是什么?我在哪里可以找到这方面的文档?我能找到的只是 gl_Position 参考,gl_projectionMatrix 与 projectionMatrix 相同。 projectionMatrix 即使在 GLSL 备忘单中也没有定义。

<script type="x-shader/x-vertex" id="vertexshader">

    varying vec3 col;

    void main()
    {
        col         = vec3( uv, 1.0 );
        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
    }

</script>

<script type="x-shader/x-fragment" id="fragmentshader">

    varying vec3 col;

    void main()
    {
        gl_FragColor = vec4(col, 1);
    }

</script>

gl_Position是一个内置的vertex shader输出变量,其类型被OpenGL规范定义为vec4.

position 是顶点着色器属性,自从引入可编程着色器以来,(作为开发人员)可以完全控制其格式。

很可能,你有每个顶​​点的 3D 坐标(因此,每个顶点只有 3 个浮点数),你通过调用 glVertexAttribPointer(或类似的)将其配置为顶点着色器输入,告诉 OpenGL一次从 position 属性的缓冲区中拉取 3 个浮点数。由于 gl_Position 需要 4 个浮点数,因此在顶点着色器中需要扩展到 vec4(通过将 w 填充为 1.0)。

position是用户命名的变量。 position 对 WebGL 没有意义。它很可能被称为 foobarwhatever。它对 WebGL 没有意义,就像变量 xyz 在 JavaScript

中没有意义一样

JavaScript

var xyz = 123;      // this has no meaning to JavaScript, only to the programmer
var position = 789; // this has no meaning to JavaScript either.

WebGL GLSL

attribute vec3 xyz;       // this has no meaning to WebGL
attribute vec3 position;  // this has no meaning to WebGL either

projectionMatrix也是如此。这是程序员制作的变量。 WebGL 不关心名称是什么。如果您正在使用某个库(比如 three.js),它可能会为变量组成一些名称,但这些变量和所选名称是库的一部分,而不是 WebGL 的一部分。日本程序员可能会使用 haichishaeigyouretu 之类的名称,而不是 positionprojectionMatrix.

gl_开头的变量是特殊的全局变量。 WebGL Quick Reference Card 上有一个列表。 所有其他变量均由程序员编写

Built-In Inputs, Outputs, and Constants [7]

Shader programs use Special Variables to communicate with fixed-function parts of the pipeline. Output Special Variables may be read back after writing. Input Special Variables are read-only. All Special Variables have global scope.

Vertex Shader Special Variables [7.1]

Outputs:
Variable                   |Description           | Units or coordinate system
---------------------------+----------------------+------------------
highp vec4 gl_Position;    |transformed vertex    |clip coordinates
                           |position              | 
---------------------------+----------------------+------------------
mediump float gl_PointSize;|transformed point size|pixels
                           |(point rasterization  |
                           |only)                 |
---------------------------+----------------------+------------------

Fragment Shader Special Variables [7.2]

Fragment shaders may write to gl_FragColor or to one or more elements of gl_FragData[], but not both. The size of the gl_FragData array is given by the built-in constant gl_MaxDrawBuffers.

Inputs:
Variable                   |Description           | Units or coordinate system
---------------------------+----------------------+------------------
mediump vec4 gl_FragCoord; |fragment position     | window coordinates
                           | within frame buffer  |
---------------------------+----------------------+------------------
bool gl_FrontFacing;       |fragment belongs to a | Boolean
                           |front-facing primitive|
---------------------------+----------------------+------------------
mediump vec2 gl_PointCoord;|fragment position     | 0.0 to 1.0 for
                           |within a point (point | each component
                           |rasterization only)   | 
---------------------------+----------------------+------------------

Outputs:
Variable                   |Description           | Units or coordinate system
---------------------------+----------------------+------------------
mediump vec4 gl_FragColor; |fragment color        | RGBA color
---------------------------+----------------------+------------------
mediump vec4 gl_FragData[n]|fragment color for    | RGBA color
                           |color attachment n    |
---------------------------+----------------------+------------------

Built-In Constants With Minimum Values [7.4]

Built-in Constant                                 | Minimum value
--------------------------------------------------+------------------
const mediump int gl_MaxVertexAttribs             | 8
--------------------------------------------------+------------------
const mediump int gl_MaxVertexUniformVectors      | 128
--------------------------------------------------+------------------
const mediump int gl_MaxVaryingVectors 8          |
--------------------------------------------------+------------------
const mediump int gl_MaxVertexTextureImageUnits   | 0
--------------------------------------------------+------------------
const mediump int gl_MaxCombinedTextureImageUnits | 8
--------------------------------------------------+------------------
const mediump int gl_MaxTextureImageUnits         | 8
--------------------------------------------------+------------------
const mediump int gl_MaxFragmentUniformVectors    | 16
--------------------------------------------------+------------------
const mediump int gl_MaxDrawBuffers               | 1
--------------------------------------------------+------------------

Built-In Uniform State [7.5]

Specifies depth range in window coordinates. If an implementation does not support highp precision in the fragment language, and state is listed as highp, then that state will only be available as mediump in the fragment language.

struct gl_DepthRangeParameters {
   highp float near; // n
   highp float far; // f
   highp float diff; // f - n
};

uniform gl_DepthRangeParameters gl_DepthRange;

As var as position 是 vec3 这也是程序员的决定。成为 vec4float 或任何你想要的都一样好。

虽然着色器可以是您想要的任何东西并使用您想要的任何变量名,但最常见的顶点着色器可能类似于

attribute vec4 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

void main() {
  gl_Position = projectionMatrix * modelViewMatrix * position;
}

有些程序员将 vec3 用于 position,但无论如何他们都必须将其强制转换为 vec4

attribute vec3 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

void main() {
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);
}

并且由于 vec4 属性的 w 值默认为 1.0,因此没有理由手动执行此操作。

You might find these articles helpful in explaining WebGL