2

在 libgdx Mesh, Color, Texture 教程中,Mesh 表示一个简单的三角形,每个顶点的颜色和纹理信息由以下方法创建:

mesh = new Mesh(true, 3, 3, 
                new VertexAttribute(Usage.Position, 3, "a_position"),
                new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
                new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords"));

VertexAttribute构造函数中,整数参数是什么?该文档说它是编码信息所需的“组件数量”。

我将其读取为每个条目使用的“顶点”数组(浮点数组)中的条目数。因此,对于第一个VertexAttribute是 3,这是有道理的(x、y 和 z 各一个)。但是,ColorPacked 属性有 4,但是由于颜色数据被编码为单个浮点数,这不应该是 1 吗?最后添加纹理坐标(得到 2,与每个顶点所需的两个浮点 u,v 坐标相匹配)。

VertexAttribute 构造函数的javadoc说这个参数是:

numComponents - the number of components of this attribute, must be between 1 and 4.

请注意一个较旧的问题, VertexAttribute() 中的第三个参数用于 libgdx 中是什么?, 涵盖了这个构造函数的第三个参数,所以我们只需要一个 SO 问题来涵盖第一个。:)

4

2 回答 2

7

VertextAttribute 构造函数的 numComponents 整数参数是 gl*Pointer 的“大小”参数

  • Usage.Position -> glVertexPointer
  • Usage.Color -> glColorPointer
  • Usage.ColorPacked -> glColorPointer
  • Usage.TextureCoordinates -> glTexCoordPointer
  • Usage.Normal -> glNormalPointer

使用 ColorPacked 属性将调用 glColorPointer 的“类型”参数从 GL10.GL_FLOAT 更改为 GL11.GL_UNSIGNED_BYTE。颜色需要 4 个字节,因此您需要将“numComponents”参数设置为 4。

来源:VertexArray.bind()glColorPointer

于 2011-10-27T14:15:55.780 回答
0

这是着色器中组件的数量,而不是 CPU 方面的计算。当您将位置向量传递给着色器时,4 表示,您的向量被编码到着色器中的 vec4 中。

颜色也是如此:您可以将打包的单个浮点值传递给着色器,但您将在着色器中使用 vec4 颜色向量。

于 2013-11-10T14:15:56.863 回答