本文介绍了可以使用glVertexAttribPointer将较小的向量分配给较大的向量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘录自OpenGL®ES阴影语言(v1 .00,r17)[PDF] (强调我的意思):

From section 5.8 of The OpenGL® ES Shading Language (v1.00, r17) [PDF] (emphasis mine):

因此,听起来像这样是合法的:

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

我认为 glVertexAttribPointer 具有相似之处要求.也就是说,如果您要分配给vec4,则size参数必须等于4.

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.

然后我遇到了适用于Android的> GLES20TriangleRenderer示例.一些相关的摘要:

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);

所以aPositionvec4,但是用于设置它的glVertexAttribPointer的调用的size为3.该代码正确吗,是GLES20TriangleRenderer依赖于未指定的行为,还是存在?还有其他我想念的东西吗?

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?

推荐答案

传递给着色器的属性数据的大小不必与该着色器中的属性的大小匹配.您可以将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 a vec4; the leftover two values are zero, except for the W component which is 1. And similarly, you can pass 4 values to a vec2 attribute; the extra values are discarded.

因此,您可以将顶点属性与所有想要的上传值混合并匹配.

So you can mix and match vertex attributes with uploaded values all you want.

这篇关于可以使用glVertexAttribPointer将较小的向量分配给较大的向量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:58