Using drawArrays

We will call drawArrays when information about indices is not available. In most cases, drawArrays is used when the geometry is simple enough that defining indices is overkill  for instance, when we want to render a triangle or a rectangle. In that case, WebGL will create the geometry in the order in which the vertex coordinates are defined in the VBO. If you have contiguous triangles (as we did in the trapezoid example), you will have to repeat these coordinates in the VBO.

If you need to repeat many vertices to create the geometry, drawArrays is not the optimal method, because the more vertex data you duplicate, the more calls you will have on the vertex shader. This can reduce the overall performance, since the same vertices must go through the pipeline several times, one for each time that they are repeated in the respective VBO:

The signature for drawArrays is as follows:

gl.drawArrays(mode, first, count)

Where:

  • mode: Represents the type of primitive that we are going to render. The possible values for mode are gl.POINTSgl.LINE_STRIPgl.LINE_LOOPgl.LINESgl.TRIANGLE_STRIPgl.TRIANGLE_FAN, and gl.TRIANGLES.
  • first: Specifies the starting element in the enabled arrays.
  • count: The number of elements to be rendered.
WebGL drawArrays Specification

When drawArrays is called, it uses count sequential elements from each enabled array to construct a sequence of geometric primitives, beginning with the element first. Mode specifies what kinds of primitives are constructed and how the array elements construct those primitives.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.25.74