Attribute Data

Understanding Attribute Data

This is probably the toughest part of the mesh creation to understand. Each of our vertices have a bunch of properties called attributes. There are 9 predefined attributes in AGK2. You can define your own custom attributes as well, as long as you create a shader to handle those attributes. The 9 predefined attributes are position, normal, tangent, binormal, color, uv, uv1, boneweights, and boneindices. For this part of the tutorial, we will only focus on 3; position, normal, and color.

The attribute section of our mesh begins at byte 24 in our memblock. The first four bytes are defined as follows

In our example here, we are going to pack all 4 bytes into a single integer. For the position attribute we will use the value 0x0c000300. In AGK2, the lowest byte of our integer will be written first, so the values will appear in memory as 0x00 0x03 0x00 0x0c. 0x00 denotes floating point values. 0x03 indicates there are 3 components which will be the x, y, z coordinates of our vertex. The next 0x00 means that the value will not be normalized when passed to the shader. Lastly 0x0c, or 12 in decimal, is the length of the attribute name "position", null terminated and rounded up.

After the 4 bytes defining our attribute, we then write the attribute name. The code will look like this:

SetMemBlockInt(memblock,24,0x0C000300) //float, 3 components, no normalizing, position

The normal attribute is virtually the same except that 0x0c will be replaced with 0x08 as the name is shorter. The offset for the normal attribute will be 24 + 4 + 12 = 40

The color attribute is a bit different. It looks like this 0x08010401, which will be written in the memblock as 0x01 0x04 0x01 0x08. 0x01 for unsigned byte, 0x04 for 4 components, red, green, blue, alpha. 0x01 normalize the bytes to the range of 0.0-1.0. Finally, 0x08 for the length of the attribute name "color". This data will be written at offset 52.

The entire block of code defining the attribute data looks like this:

//Attribute information, I'm combining all bytes into a single int.  
//   Little endiness means order is string length, normal flag, component count, data type (data will
//   be written to the MemBlock in reverse order).
SetMemBlockInt(memblock,24,0x0C000300) //float, 3 components, no normalizing, position
SetMemblockInt(memblock,40,0x08000300) //same as position, but for normals
SetMemblockInt(memblock,52,0x08010401) //For color we have byte, 4 components, normalize data

Created with Tutorial Markup (c)2018 James Chamblin