Drawing methods

OpenGL supports several drawing methods.

  • GL_POINTS - draws points at vertex positions
  • GL_LINES - draws lines between a pair of vertices
  • GL_LINE_STRIP - draws lines between all vertices except first vertex and last vertex
  • GL_LINE_LOOP - draws lines between all vertices
  • GL_TRIANGLES - draws triangles from each triple vertex
  • GL_TRIANGLE_STRIP - draws triangles that each share an edge with the next one
  • GL_TRIANGLE_FAN - draws triangles that each share a common corner (v0 on picture)
  • GL_PATCHES - draws 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.

In OpenGL v3.2 new methods was added: GL_LINE_STRIP_ADJACENCY, GL_LINES_ADJACENCY, GL_TRIANGLE_STRIP_ADJACENCY and GL_TRIANGLES_ADJACENCY.

Methods GL_QUADS and GL_QUAD_STRIP are removed from v3.1.

 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

As you can see, triangle fans and triangle strips require less data sent to the GPU vs if you wanted to draw the same thing using triangles. For example, to draw quad by a triangles you have to provide 6 vertices. To draw by triangle strips, you have to provide 4 vertices.

points

By default size of point is 1. It is possible to change point size.

glEnable(GL_POINT_SMOOTH);
glPointSize(15);
glDrawArrays(GL_POINTS, 0, 4);

There is glPointerParamterX(name, val)functions, that allow to specify parameters of points. Possible values of name:

  • GL_POINT_SIZE_MIN - the minimum point size. The default value is 0.0.
  • GL_POINT_SIZE_MAX - the maximum point size. The default value is 1.0.
  • GL_POINT_FADE_THRESHOLD_SIZE - the threshold value to which point sizes are clamped if they exceed the specified value. The default value is 1.0.
  • GL_POINT_DISTANCE_ATTENUATION - the coefficients used for scaling the computed point size. The default values are 1, 0, 0.
  • GL_POINT_SPRITE_COORD_ORIGIN - the point sprite texture coordinate origin, either GL_LOWER_LEFT or GL_UPPER_LEFT. The default value is GL_UPPER_LEFT.

You can get these values by glGetX() functions like glGetFloat().

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. Then the point size comes from the output variable float gl_PointSize.

In WebGL no glEnable functions, just set gl_PointSize in vertex shader.

#version 300 es
precision mediump float;
layout(location=0) in vec4 vCoord;
layout(location=1) in vec4 vColor;
layout(location=2) in vec4 vNormal;
layout(location=3) in vec4 vTexCoord;
out vec4 exColor; 
uniform mat4 matrix;
void main() {
    gl_Position = matrix  * vCoord;
    gl_PointSize = 15.0;
    exColor=vColor;
}

lines

By default width of line is 1. It is possible to change line width.

glEnable(GL_LINE_SMOOTH);
glLineWidth(15);
glDrawArrays(GL_LINES, 0, 4);

polygons

The order of the vertices in a triangle, when combined with their visual orientation, can be used to determine whether the triangle is being seen from the front or the back side.

By default counter-clockwise orientation is front face. You can specify which orientation is a face by the glFrontFace() function: By default counterclockwise orientation is front face. You can specify the orientation of the face with the glFrontFace() function:

  • GL_CW - clockwise
  • GL_CCW - counter-clockwise

The glPolygonMode(face, mode) select a polygon rasterization mode.

The face parameter can be:

  • GL_FRONT - front side
  • GL_BACK - back side
  • GL_FRONT_AND_BACK - for both sides

The mode parameter can be:

  • GL_POINT - polygon vertices that are marked as the start of a boundary edge are drawn as points. Point attributes such as GL_POINT_SIZE and GL_POINT_SMOOTH control the rasterization of the points. Polygon rasterization attributes other than GL_POLYGON_MODE have no effect.
  • GL_LINE - boundary edges of the polygon are drawn as line segments. Line attributes such as GL_LINE_WIDTH and GL_LINE_SMOOTH control the rasterization of the lines. Polygon rasterization attributes other than GL_POLYGON_MODE have no effect.
  • GL_FILL - the interior of the polygon is filled. Polygon attributes such as GL_POLYGON_SMOOTH control the rasterization of the polygon.

For performance reasons you can specify which a side to cull while rendering. For example you draw cube and interior of cube does not visible to the user.

glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)