gpt4 book ai didi

opengl-es - 为什么 'gl_Position' 与 'position' 是不同的数据类型

转载 作者:行者123 更新时间:2023-12-03 15:05:39 24 4
gpt4 key购买 nike

我正在从代码示例中了解 GLSL 着色器,但我对以下内容感到困惑:gl_Position 是 vec4 数据类型,而 position 是 vec3 数据类型,为什么?这个“位置”变量到底是什么,我在哪里可以找到有关此的某种文档?我能找到的只是 gl_Position 引用,对于 gl_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>

最佳答案

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 的一部分。日本程序员可能会使用像 haichi 这样的名字。和 shaeigyouretu而不是 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;


作为 var 为 position成为 vec3 这也是程序员的决定。做一个 vec4 也很好或 float或任何你想要的。

虽然着色器可以是你想要的任何东西并使用你想要的任何变量名称,但最常见的顶点着色器可能是沿着
attribute vec4 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

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

一些程序员使用 vec3position但是无论如何他们都必须将其强制转换为 vec4
attribute vec3 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

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

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

You might find these articles helpful in explaining WebGL

关于opengl-es - 为什么 'gl_Position' 与 'position' 是不同的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31125387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com