定义了顶点着色器:
GLbyte vShaderStr[] = "attribute vec4 vPosition; \n" "void main() \n" "{ \n" " gl_Position = vPosition; \n" "}; \n";
vPosition属性是一个四分量向量.
稍后在文本中,应用程序将与片段着色器一起编译顶点着色器.
使用glBindAttribLocation建立一个句柄,将应用程序顶点数据传递给着色器:
// Bind vPosition to attribute 0 glBindAttribLocation(programObject,"vPosition");
现在链接了两个着色器,程序已准备就绪.
用例是这样的:
GLfloat vVertices[] = {0.0f,0.5f,0.0f,-0.5f,0.0f}; ... // Load the vertex data glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,vVertices); glEnableVertexAttribArray(0); glDrawArrays(GL_TRIANGLES,3);
意思是:
> glVertexAttribPointer(0,vVertices):
将顶点数据传递给程序,使用位置0,每个顶点有3个组件,类型为float,不使用规范化.
> glEnableVertexAttribArray(0):
着色器将使用传递的数据.
> glDrawArrays(GL_TRIANGLES,3); :
着色器将使用在指定数组中找到的第一个(也是唯一的)三个顶点执行代码以绘制单个三角形.
我的问题是:
>顶点着色器期望四个组件的顶点位置(x,y,z,w),程序只传递三个组件(x,z)数据
这为什么有效?
>着色器需要vec4,w组件必须为1才能按预期工作.
哪个步骤负责正确添加w组件?
解决方法
如该帖所述,它出现在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,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,respectively. If size is three then x,and z are specified,and w is implicitly set to one. If size is four then all components are specified.