In the OpenGL ES 2.0 introduction, found here: http://www.webreference.com/programming/opengl_es/2.html a vertex shader is defined:
在OpenGL ES 2.0的介绍中,可以在这里找到:http://www.webreference.com/programming/opengl_es/2.html一个顶点着色器被定义为:
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"}; \n";
The vPosition attribute is a four component vector.
vPosition属性是一个四分量向量。
Later in the text the application will compile the vertex shader, together with the fragment shader. With the glBindAttribLocation a handle to pass the applications vertex data to the shader is established:
在后面的文本中,应用程序将编译顶点着色器,以及碎片着色器。使用glBindAttribLocation一个句柄将应用程序顶点数据传递给着色器:
// Bind vPosition to attribute 0
glBindAttribLocation(programObject, 0, "vPosition");
The 2 shaders are now linked and the program is ready to use.
这两个着色现在被连接和程序准备使用。
The use case is this:
用例是:
GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f};
...
// Load the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
Which means:
这意味着:
-
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices)
: pass the vertex data to the program, use location 0, each vertex has 3 components, type is float, no normalisation shall be used.glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0, v):将顶点数据传递给程序,使用位置0,每个顶点有3个分量,类型为float,没有应使用规范化。
-
glEnableVertexAttribArray(0)
: The shader will use the passed data.glenableverattribarray(0):着色器将使用传递的数据。
-
glDrawArrays(GL_TRIANGLES, 0, 3);
: The shader will execute the code to draw a single triangle, using the first (and only) three vertices found in the specified array.glDrawArrays(GL_TRIANGLES 0 3);:着色器将执行代码来绘制一个三角形,使用指定数组中找到的第一个(也是唯一的)三个顶点。
My Questions are:
我的问题是:
-
The vertex shader is expecting a four component vertex position (x,y,z,w), the program passes only three components (x,y,z) data Why is this working?
顶点着色器期望一个四分量顶点位置(x,y,z,w),程序只传递三个分量(x,y,z)数据为什么会这样?
-
The shader needs a vec4, the w-component must be 1 to work as expected. Which step is responsible for adding the w-component correctly?
着色器需要一个vec4, w组件必须是1才能正常工作。哪个步骤负责正确添加w组件?
1 个解决方案
#1
17
See this answer: Can you use glVertexAttribPointer to assign a smaller vector to a larger one?
看看这个答案:你能使用glVertexAttribPointer将一个较小的向量分配给一个较大的向量吗?
As mentioned in that post it appears in the gles specifications: http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.24.pdf
In section 2.8 of this document it says:
正如在那篇文章中提到的,它出现在gles规范中:http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.24.pdf,在本文件2.8节中它说:
When an array element i is transferred to the GL by the DrawArrays or DrawElements commands, each generic attribute is expanded to four components. If size is one then the x component of the attribute is specified by the array; the y, z, and w components are implicitly set to zero, zero, and one, respectively. If size is two then the x and y components of the attribute are specified by the array; the z, and w components are implicitly set to zero, and one, respectively. If size is three then x, y, and z are specified, and w is implicitly set to one. If size is four then all components are specified.
当阵列元素i被DrawArrays或DrawElements命令传输到GL时,每个通用属性被扩展为四个组件。如果大小为1,则属性的x组件由数组指定;y、z和w的分量分别被隐式地设置为0、0和1。如果大小为2,则属性的x和y分量由数组指定;z分量和w分量分别被隐式设置为0和1。如果大小为3,则指定x、y和z,并将w隐式设置为1。如果大小为4,则指定所有组件。
#1
17
See this answer: Can you use glVertexAttribPointer to assign a smaller vector to a larger one?
看看这个答案:你能使用glVertexAttribPointer将一个较小的向量分配给一个较大的向量吗?
As mentioned in that post it appears in the gles specifications: http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.24.pdf
In section 2.8 of this document it says:
正如在那篇文章中提到的,它出现在gles规范中:http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.24.pdf,在本文件2.8节中它说:
When an array element i is transferred to the GL by the DrawArrays or DrawElements commands, each generic attribute is expanded to four components. If size is one then the x component of the attribute is specified by the array; the y, z, and w components are implicitly set to zero, zero, and one, respectively. If size is two then the x and y components of the attribute are specified by the array; the z, and w components are implicitly set to zero, and one, respectively. If size is three then x, y, and z are specified, and w is implicitly set to one. If size is four then all components are specified.
当阵列元素i被DrawArrays或DrawElements命令传输到GL时,每个通用属性被扩展为四个组件。如果大小为1,则属性的x组件由数组指定;y、z和w的分量分别被隐式地设置为0、0和1。如果大小为2,则属性的x和y分量由数组指定;z分量和w分量分别被隐式设置为0和1。如果大小为3,则指定x、y和z,并将w隐式设置为1。如果大小为4,则指定所有组件。