在 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";
vPosition 属性是一个四分量向量。
在文本的后面,应用程序将编译顶点着色器和 fragment 着色器。 使用 glBindAttribLocation 可以建立将应用程序顶点数据传递给着色器的句柄:
// Bind vPosition to attribute 0
glBindAttribLocation(programObject, 0, "vPosition");
现在 2 个着色器已链接,程序可以使用了。
用例是这样的:
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);
这意味着:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices)
: 将顶点数据传递给程序,使用位置0,每个顶点有3个分量,类型为float,不使用归一化。glEnableVertexAttribArray(0)
: 着色器将使用传递的数据。glDrawArrays(GL_TRIANGLES, 0, 3);
: 着色器将执行代码来绘制单个三角形,使用在指定数组中找到的第一个(也是唯一的)三个顶点。
我的问题是:
顶点着色器需要一个四分量顶点位置 (x,y,z,w),程序只传递三个分量 (x,y,z) 数据 为什么会这样?
着色器需要一个 vec4,w 分量必须为 1 才能按预期工作。 哪个步骤负责正确添加 w 组件?
最佳答案
查看此答案:Can you use glVertexAttribPointer to assign a smaller vector to a larger one?
如该帖子中所述,它出现在 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.
关于android - OpenGL ES 2.0 指定位置属性 vec3 或 vec4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8551935/