P.T.
P.T.

Reputation: 25177

What is integer parameter to the VertexAttribute constructor in libgdx?

In libgdx Mesh, Color, Texture tutorial a Mesh representing a simple triangle, with color and texture information for each vertex is created by:

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

In the VertexAttribute constructors what is the integer argument? The doc says its the "number of components" needed to encode the information.

I read that as the number of entries in the 'vertex' array (an array of floats) that each entry uses. So, for the first VertexAttribute that is 3, which makes sense (one each for x, for y, and for z). However, the ColorPacked attribute has 4 but but since the color data gets encoded into a single float, shouldn't this be 1? Finally the texture coordinates are added (that gets 2, which matches the two float u,v coordinates needed for each vertex).

The javadoc for the VertexAttribute constructor says this parameter is:

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

Note an older question, Whats the 3rd argument in VertexAttribute() used for in libgdx?, covers the 3rd argument to this constructor, so we just need one more SO question to cover the first. :)

Upvotes: 2

Views: 1857

Answers (2)

TheWhiteLlama
TheWhiteLlama

Reputation: 1286

It's the number of components in your shader, not the cpu-sided calculations. When you pass a position-vector to the shader, 4 says, that your vector is encoded into a vec4 in your shader.

Same is with colors: You may pass a packed single float value to shader, but you'll be working with a vec4 color vector in your shader.

Upvotes: 0

Doran
Doran

Reputation: 4090

The numComponents integer parameter to the VertextAttribute constructor is the "size" argument to gl*Pointer

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

Using the ColorPacked attribute changes the "type" argument of the call to glColorPointer from GL10.GL_FLOAT to GL11.GL_UNSIGNED_BYTE. The color needs 4 bytes so you need to set the "numComponents" argument to 4.

Source: VertexArray.bind() and glColorPointer

Upvotes: 7

Related Questions