您可以使用 glVertexAttribPointer 将较小的向量分配给较大的向量吗?
来自 OpenGL® ES 着色语言 (v1.00, r17) [PDF](强调我的):
赋值运算符将右值表达式的值存储到左值中,并返回具有左值表达式的类型和精度的右值。 左值表达式和右值表达式必须具有相同的类型。所有所需的类型转换都必须通过构造函数显式指定。
所以听起来这样做不是合法的:
vec3 my_vec3 = vec3(1, 2, 3);
vec4 my_vec4 = my_vec3;
并且为了使其合法,第二行必须是像:
vec4 my_vec4 = vec4(my_vec3, 1); // add 4th component
我假设 glVertexAttribPointer
有类似的要求。也就是说,如果您要分配给 vec4
,则 size
参数必须等于 4。
然后我遇到了 适用于 Android 的 GLES20TriangleRenderer 示例。一些相关片段:
attribute vec4 aPosition;
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
所以 aPosition
是一个 vec4
,但是对用于设置它的 glVertexAttribPointer
的调用具有 size
> of 3. 此代码是否正确,GLES20TriangleRenderer
是否依赖于未指定的行为,或者是否还有其他我遗漏的内容?
From section 5.8 of The OpenGL® ES Shading Language (v1.00, r17) [PDF] (emphasis mine):
The assignment operator stores the value of the rvalue-expression into the l-value and returns an r-value with the type and precision of the lvalue-expression. The lvalue-expression and rvalue-expression must have the same type. All desired type-conversions must be specified explicitly via a constructor.
So it sounds like doing something like this would not be legal:
vec3 my_vec3 = vec3(1, 2, 3);
vec4 my_vec4 = my_vec3;
And to make it legal the second line would have to be something like:
vec4 my_vec4 = vec4(my_vec3, 1); // add 4th component
I assumed that glVertexAttribPointer
had similar requirements. That is, if you were assigning to a vec4
that the size
parameter would have to be equal to 4.
Then I came across the GLES20TriangleRenderer sample for Android. Some relevant snippets:
attribute vec4 aPosition;
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
So aPosition
is a vec4
, but the call to glVertexAttribPointer
that's used to set it has a size
of 3. Is this code correct, is GLES20TriangleRenderer
relying on unspecified behavior, or is there something else I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
传递给着色器的属性数据的大小不必与该着色器中属性的大小匹配。您可以将 2 个值(来自
glVertexAttribPointer
)传递给定义为vec4
的属性;剩下的两个值都为零,除了 W 分量为 1。同样,您可以将 4 个值传递给 vec2 属性;多余的值将被丢弃。因此,您可以根据需要将顶点属性与上传的值混合和匹配。
The size of the attribute data passed to the shader does not have to match the size of the attribute in that shader. You can pass 2 values (from
glVertexAttribPointer
) to an attribute defined as avec4
; the leftover two values are zero, except for the W component which is 1. And similarly, you can pass 4 values to avec2
attribute; the extra values are discarded.So you can mix and match vertex attributes with uploaded values all you want.