# Primitive

The term **Primitive** in OpenGL is used to refer to two similar but separate concepts. The first is the interpretive scheme used by OpenGL to determine what a stream of vertices represents when being rendered. Such sequences of vertices can be arbitrarily long.

The other meaning of "Primitive" is as the *result* of the interpretation of a vertex stream, as part of Primitive Assembly. Therefore, processing a vertex stream by one of these primitive interpretations results in an ordered sequence of primitives. The individual primitives are sometimes called "base primitives".

## Vertex stream

A vertex stream is an ordered list of vertices. Where this stream comes from depends on when Primitive Assembly is taking place and which stages of the rendering pipeline are involved. Vertex streams can come from:

- Vertex Rendering commands, defined via Vertex Specification and processed by a Vertex Shader. The primitive type is the type specified by the rendering command.
- Tessellation, after the execution of the Tessellation Evaluation Shader. The primitive type is defined by the abstract patch type, specified in the TES.
- Geometry Shader primitive outputs. The primitive type is specified by the GS.

The primitive associated with the stream defines how that stream is broken down into an ordered sequence of base primitives: points, lines, or triangles.

## Point primitives

There is only one kind of point primitive: GL_POINTS. This will cause OpenGL to interpret each individual vertex in the stream as a point. Points that have a Texture mapped onto them are often called "point sprites".

Points are rasterized as screen-aligned squares of a given window-space size. The size can be given in two methods: by the last active vertex processing shader stage or by the context's state. To set the point size from a shader, enable the glEnable with argument (GL_PROGRAM_POINT_SIZE) to set the point size from the program. If GL_PROGRAM_POINT_SIZE is enabled, then the point size comes from the output variable float gl_PointSize. If it is disabled, the point size is constant for all points in a primitive, and is set by the glPointSize function.

The size defines the number of window pixels that each side of the point's square takes up. The point's position defines the *center* of that square.

Regardless of how the point size is defined, it must be greater than 0, or else undefined behavior results. There is an implementation-defined range for point sizes, and the size given by either method is clamped to that range. You can query the range with GL_POINT_SIZE_RANGE (returns 2 floats). There is also a point granularity that you can query with GL_POINT_SIZE_GRANULARITY; the implementation will clamp sizes to the nearest multiple of the granularity.

### Point fragment inputs

Points are defined by only a single vertex. Therefore, every Fragment generated by rasterizing that point is given the same user-defined input values. However, it is useful to know exactly where within the point a particular Fragment Shader invocation is. While the specific position of the fragment, gl_FragCoord will change, per the fragment's location, it would be more useful to know where one is relative to the point primitive itself.

To assist in this, the fragment shader can use the built-in input vec2 gl_PointCoord. This gives coordinates on the range [0, 1]. The location of (0, 0) depends on the point parameter setting for GL_POINT_SPRITE_COORD_ORIGIN: if it is set to GL_LOWER_LEFT, then (0, 0) is the bottom-left coordinate. While if it is GL_UPPER_LEFT, then (0, 0) is the top-left coordinate. The default is GL_LOWER_LEFT, which fits with OpenGL's usual right-handed coordinate systems.

### Multisampling and fading

TODO: This section needs to be filled in. |

When using Multisample rendering and drawing points, the fragment shader output variables can have their alphas adjusted to represent the point "fading" away.

## Line primitives

There are 3 kinds of line primitives, based on different interpretations of a vertex stream.

- GL_LINES: Vertices 0 and 1 are considered a line. Vertices 2 and 3 are considered a line. And so on. If the user specifies a non-even number of vertices, then the extra vertex is ignored.
- GL_LINE_STRIP: The adjacent vertices are considered lines. Thus, if you pass
*n*vertices, you will get*n*-1 lines. If the user only specifies 1 vertex, the drawing command is ignored. - GL_LINE_LOOP: As line strips, except that the first and last vertices are also used as a line. Thus, you get
*n*lines for*n*input vertices. If the user only specifies 1 vertex, the drawing command is ignored. The line between the first and last vertices happens after all of the previous lines in the sequence.

### Line width

TODO: This section needs to be filled in. |

Lines are rasterized as a screen-aligned quad of uniform width.

## Triangle primitives

A triangle is a primitive formed by 3 vertices. It is the 2D shape with the smallest number of vertices, so renderers are typically designed to render them. Since it is created from only 3 vertices, it is also guaranteed to be planar.

There are 3 kinds of triangle primitives, based again on different interpretations of the vertex stream:

- GL_TRIANGLES: Vertices 0, 1, and 2 form a triangle. Vertices 3, 4, and 5 form a triangle. And so on.
- GL_TRIANGLE_STRIP: Every group of 3 adjacent vertices forms a triangle. The face direction of the strip is determined by the winding of the first triangle. Each successive triangle will have its effective face order reversed, so the system compensates for that by testing it in the opposite way. A vertex stream of
*n*length will generate*n*-2 triangles. - GL_TRIANGLE_FAN: The first vertex is always held fixed. From there on, every group of 2 adjacent vertices form a triangle with the first. So with a vertex stream, you get a list of triangles like so: (0, 1, 2) (0, 2, 3), (0, 3, 4), etc. A vertex stream of
*n*length will generate*n*-2 triangles.

Any incomplete primitives will be ignored. For example, using GL_TRIANGLES with a number of vertices not divisible by 3 will ignore the incomplete primitive at the end. Rendering with less than 3 vertices will result in nothing rendering.

Here are some examples that can be more illustrative:

GL_TRIANGLES:

Indices: 0 1 2 3 4 5 ... Triangles: {0 1 2} {3 4 5}

GL_TRIANGLE_STRIP:

Indices: 0 1 2 3 4 5 ... Triangles: {0 1 2} {1 2 3} drawing order is (2 1 3) to maintain proper winding {2 3 4} {3 4 5} drawing order is (4 3 5) to maintain proper winding

GL_TRIANGLE_FAN:

Indices: 0 1 2 3 4 5 ... Triangles: {0 1 2} {0} {2 3} {0} {3 4} {0} {4 5}

### Triangle facing

The Winding Order determines the facing of a triangle. The triangle's facing can be used to cull faces based on whether it is the front or back face. Even if face culling is not used, the triangle's face can be used in Stencil Tests, and part of a Fragment's data is a boolean value that says whether the fragment was generated from the front or back face of the triangle. This allows the Fragment Shader to do different processing based on the triangle's facing.

Facing only matters for triangle primitive rasterization. All non-triangle primitive types are considered to rasterize the front face, and face culling only works on triangles.

## Quads

Warning: This section describes legacy OpenGL APIs that have been removed from core OpenGL 3.1 and above (they are only deprecated in OpenGL 3.0). It is recommended that you not use this functionality in your programs. |

A quad is a 4 vertex quadrilateral primitive. The four vertices are expected to be coplanar; failure to do so can lead to undefined results.

A quad is typically rasterized as a pair of triangles. This is not defined by the GL spec, but it is allowed by it. This can lead to some artifacts due to how vertex/geometry shader outputs are interpolated over the 2 generated triangles.

- GL_QUADS: Vertices 0-3 form a quad, vertices 4-7 form another, and so on. The vertex stream must be a number of vertices divisible by 4 to work.
- GL_QUAD_STRIP: Similar to triangle strips, a quad strip uses adjacent edges to form the next quad. In the case of quads, the third and fourth vertices of one quad are used as the edge of the next quad. So vertices 0-3 are a quad, 2-5 are a quad, and so on. A vertex stream of
*n*length will generate (*n*- 2) / 2 quads. As with triangle strips, the winding order of quads is changed for every other quad.

## Adjacency primitives

TODO: This section needs to be filled in. |

These are special primitives that are expected to be used specifically with Geometry Shaders (GS). These primitives give the geometry shader more vertices to work with for each input primitive. Normally, when using any of the above primitives, the GS gets merely one of the base type. If you use a GS with a GL_TRIANGLE_STRIP, each execution of the GS will only see the 3 vertices of one particular triangle. These special primitive modes allow the GS to access vertex data for adjacent triangles.

## Patches

The GL_PATCH primitive type can only be used when Tessellation is active. It is a primitive with a user-defined number of vertices, which is then tessellated based on the control and evaluation shaders into regular points, lines, or triangles, depending on the TES's settings.

The number of vertices per patch is defined by calling glPatchParameteri with GL_PATCH_VERTICES and some number of vertices, which must be less than GL_MAX_PATCH_VERTICES. If the number of vertices in a patch is v, then the system will interpret every v vertices as a separate patch, much like GL_LINES and GL_TRIANGLES. So if you want strip-like behavior, you will need to use indices.

As with other primitive types that take multiple values from the stream, incomplete patches are ignored. So if you render with a number of vertices not divisible by v, then those last vertices are ignored.

## Provoking vertex

One of the vertices in an output primitive is designated the "provoking vertex". This vertex has special meanings for the primitive. For example, when using flat-shading on output variables, only outputs from the provoking vertex are used; every fragment generated by that primitive gets it's input from the output of the provoking vertex.

Each primitive type defines which index in the vertex stream defines the provoking vertex for a particular output primitive. There is a way to change the provoking vertex convention (primarily for D3D Compatibility):

void glProvokingVertex(GLenum provokeMode);

provokeMode can be one of the two enumerators listed in the following table; the default is GL_LAST_VERTEX_CONVENTION. This table defines which vertex index (using **one-based indices**) is the provoking vertex for a particular primitive type. The i represents the **one-based index** of the primitive being drawn. For example, if you draw GL_TRIANGLE_FANS with 5 vertices, that will yield 3 primitives, so i will range over [1, 3]. Thus, when using last vertex conventions, the provoking vertex of primitive index 2 will be the vertex index 4 (the zero-based index for the vertex is 3).

**Again, this table uses one-based indices.**

Primitive type | GL_FIRST_VERTEX_CONVENTION | GL_LAST_VERTEX_CONVENTION |
---|---|---|

GL_POINTS | i | i |

GL_LINES | 2i - 1 | 2i |

GL_LINE_LOOP | i | i + 1, if i < the number of vertices.
1 if i is equal to the number of vertices. |

GL_LINE_STRIP | i | i + 1 |

GL_TRIANGLES | 3i - 2 | 3i |

GL_TRIANGLE_STRIP | i | i + 2 |

GL_TRIANGLE_FAN | i + 1 | i + 2 |

GL_LINES_ADJACENCY | 4i - 2 | 4i - 1 |

GL_LINE_STRIP_ADJACENCY | i + 1 | i + 2 |

GL_TRIANGLES_ADJACENCY | 6i - 5 | 6i - 1 |

GL_TRIANGLE_STRIP_ADJACENCY | 2i - 1 | 2i + 3 |

Patches do not have a provoking vertex, since GL_PATCH can only be used with Tessellation. This process transforms path primitives into other kinds of primitives, so by the time the rasterizer sees it, it will not be a patch anymore. The output primitives from the TES do have provoking vertices, but which vertex is the provoking one for a particular line/triangle is implementation-defined, and therefore cannot be relied upon. So using tessellation with flat interpolation is a dubious prospect if different primitives need different values.

## Primitive restart

Setting a primitive restart index will cause the interpretation of the primitive to be reset when that index is reached in the vertex stream. The vertex data at that index will *not* be processed, nor will a vertex be inserted into the vertex stream for that index value.