I am new to OpenGL ES 2.0, and cannot understand the following simplest shader:
我是OpenGL es2.0的新手,无法理解以下最简单的着色:
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
My question is, since a position would be a vector of (x, y, z)
, why is gl_Position a vec4 instead of vec3?
我的问题是,既然位置是向量(x, y, z)为什么gl_Position是向量4而不是向量3?
2 个解决方案
#1
24
The w in vec4(x, y, z, w) is used for clipping, and plays its part while linear algebra transformations are applied to the position.
w在vec4(x, y, z, w)中用于裁剪,并发挥其部分,而线性代数变换应用于该位置。
By default, this should be set to 1.0.
默认情况下,应该将其设置为1.0。
See here for some more info: http://web.archive.org/web/20160408103910/http://iphonedevelopment.blogspot.com/2010/11/opengl-es-20-for-iOS-chapter-4.html
更多信息请参见这里:http://web.archive.org/web/201608103910/http://iphonedevelopment.blogspot.com/2010/11/opengl-es-20-for ios-chapter -4.html
#2
3
If you provide your vertices to the shader directly in clip space, you could just pass x,y,z and add 1 as the w component in that shader.
如果你在剪辑空间中直接向着色器提供你的顶点,你可以通过x,y,z和添加1作为那个着色器中的w元素。
attribute vec3 vPosition; // vec3 instead of vec4
void main()
{
gl_Position = vec4 (vPosition, 1.0);
}
#1
24
The w in vec4(x, y, z, w) is used for clipping, and plays its part while linear algebra transformations are applied to the position.
w在vec4(x, y, z, w)中用于裁剪,并发挥其部分,而线性代数变换应用于该位置。
By default, this should be set to 1.0.
默认情况下,应该将其设置为1.0。
See here for some more info: http://web.archive.org/web/20160408103910/http://iphonedevelopment.blogspot.com/2010/11/opengl-es-20-for-iOS-chapter-4.html
更多信息请参见这里:http://web.archive.org/web/201608103910/http://iphonedevelopment.blogspot.com/2010/11/opengl-es-20-for ios-chapter -4.html
#2
3
If you provide your vertices to the shader directly in clip space, you could just pass x,y,z and add 1 as the w component in that shader.
如果你在剪辑空间中直接向着色器提供你的顶点,你可以通过x,y,z和添加1作为那个着色器中的w元素。
attribute vec3 vPosition; // vec3 instead of vec4
void main()
{
gl_Position = vec4 (vPosition, 1.0);
}