Difference between revisions of "Primitive"
m (Link fix.) 
m (Alfonse moved page Primitives to Primitive over redirect: Should be singular.) 
(No difference)

Revision as of 16:09, 1 September 2012
This article is missing information. Further details can be found on the talk page. 
Primitives are ways that OpenGL interprets vertex streams, converting them from vertices into triangles, lines, points, and so forth.
Contents
Vertex Stream
A vertex stream is an ordered list of vertices sent by an OpenGL drawing command. These get processed by a Vertex Shader into posttransform vertices.
Primitives define how OpenGL interprets a vertex stream. OpenGL can draw a vertex stream as points, lines or triangles. The primitive used to interpret a stream is given by the command used to render that stream.
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 are squares or circles of a given size. The size can be given in two methods: by the program or by the context's state. To switch between them, use glEnableglDisable(GL_PROGRAM_POINT_SIZE). If GL_PROGRAM_POINT_SIZE is enabled, then the point size comes from a builtin 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.
Regardless of how the point size is defined, it must be greater than 0, or else undefined behavior results. There is an implementationdefined 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 its granularity as needed.
Point Fragment Inputs
Because a single point can generate multiple fragments, it is useful to know exactly where on the point a particular fragment is. A Fragment Shader gets the same input values for all userdefined inputs (gl_FragCoord will change, per the fragment's lcoation); therefore, the fragment shader must do its own interpolation.
To assist in this, the fragment shader can use the builtin 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 bottomleft coordinate. While if it is GL_UPPER_LEFT, then (0, 0) is the topleft coordinate.
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. The user must specify an even number of vertices in the stream to use this.
 GL_LINE_STRIP: The adjacent vertices are considered lines. Thus, if you pass n vertices, you will get n1 lines.
 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.
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. The vertex stream must be a number of vertices divisible by 3 to work.
 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. A vertex stream of n length will generate n2 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 n2 triangles.
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}
Quads
Warning: This article 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 03 form a quad, vertices 47 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 03 are a quad, 26 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
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.
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.