# Module pyglet.graphics.vertexattribute

Access byte arrays as arrays of vertex attributes.

Use create_attribute to create an attribute accessor given a simple format string. Alternatively, the classes may be constructed directly.

# Attribute format strings

An attribute format string specifies the format of a vertex attribute. Format strings are accepted by the create_attribute function as well as most methods in the pyglet.graphics module.

Format strings have the following (BNF) syntax:

    attribute ::= ( name | index 'g' 'n'? ) count type

`name` describes the vertex attribute, and is one of the following constants for the predefined attributes:

`c`
  Vertex color
`e`
  Edge flag
`f`
  Fog coordinate
`n`
  Normal vector
`s`
  Secondary color
`t`
  Texture coordinate
`v`
  Vertex coordinate

You can alternatively create a generic indexed vertex attribute by specifying its index in decimal followed by the constant `g`. For example, `0g` specifies the generic vertex attribute with index 0. If the optional constant `n` is present after the `g`, the attribute is normalised to the range `[0, 1]` or `[-1, 1]` within the range of the data type.

`count` gives the number of data components in the attribute. For example, a 3D vertex position has a count of 3. Some attributes constrain the possible counts that can be used; for example, a normal vector must have a count of 3.

`type` gives the data type of each component of the attribute. The following types can be used:

`b`
  `GLbyte`
`B`
  `GLubyte`
`s`
  `GLshort`
`S`
  `GLushort`
`i`
  `GLint`
`I`
  `GLuint`
`f`
  `GLfloat`
`d`
  `GLdouble`

Some attributes constrain the possible data types; for example, normal vectors must use one of the signed data types. The use of some data types, while not illegal, may have severe performance concerns. For example, the use of `GLdouble` is discouraged, and colours should be specified with `GLubyte`.

Whitespace is prohibited within the format string.

Some examples follow:

`v3f`
  3-float vertex position
`c4b`
  4-byte colour
`1eb`
  Edge flag
`0g3f`
  3-float generic vertex attribute 0
`1gn1i`
  Integer generic vertex attribute 1, normalized to [-1, 1]
`2gn4B`
  4-byte generic vertex attribute 2, normalized to [0, 1] (because the type is unsigned)

## Classes

| AbstractAttribute Abstract accessor for an attribute in a mapped buffer.
| ColorAttribute Color vertex attribute.
| EdgeFlagAttribute Edge flag attribute.
| FogCoordAttribute Fog coordinate attribute.
| NormalAttribute Normal vector attribute.
| SecondaryColorAttribute Secondary color attribute.
| TexCoordAttribute Texture coordinate attribute.
| VertexAttribute Vertex coordinate attribute.
| GenericAttribute Generic vertex attribute, used by shader programs.

## Functions

| interleave_attributes(attributes) Interleave attribute offsets.
| serialize_attributes(count, attributes) Serialize attribute offsets.
AbstractAttribute | create_attribute(format) Create a vertex attribute description from a format string.

## Variables

| __package__ = ``'``pyglet.graphics``'``

## Function Details

### interleave_attributes

interleave_attributes(attributes)

Interleave attribute offsets.

Adjusts the offsets and strides of the given attributes so that they are interleaved. Alignment constraints are respected.

Parameters:
  attributes : sequence of AbstractAttribute
    Attributes to interleave in-place.

### serialize_attributes

serialize_attributes(count, attributes)

Serialize attribute offsets.

Adjust the offsets of the given attributes so that they are packed serially against each other for `count` vertices.

Parameters:
  count : int
    Number of vertices.
  attributes : sequence of AbstractAttribute
    Attributes to serialize in-place.

### create_attribute

create_attribute(format)

Create a vertex attribute description from a format string.

The initial stride and offset of the attribute will be 0.

Parameters:
  format : str
    Attribute format string. See the module summary for details.
Returns: AbstractAttribute