Real-Time 3D Graphics with WebGL 2
上QQ阅读APP看书,第一时间看更新

Pointing an Attribute to the Currently-Bound VBO

The majority of the WebGL API is about setting up state to supply data to our GLSL programs. In this case, the only input to our GLSL program is aVertexPosition, which is an attribute. In Chapter 3, Lights, we will learn how to define and reference vertex and fragment shader attributes. For now, let's assume that we have the aVertexPosition attribute, which describes the vertex coordinates in the shader.

The WebGL function that allows pointing attributes to the currently-bound VBOs is vertexAttribPointer. The following is its signature:

gl.vertexAttribPointer(index, size, type, normalize, stride, offset);

Let’s describe each parameter individually:

  • Index: An attribute's index that we are going to map the currently-bound buffer to.
  • Size: Indicates the number of values per vertex that are stored in the currently-bound buffer.
  • Type: Specifies the data type of the values stored in the current buffer. It is one of the following constants: FIXEDBYTEUNSIGNED_BYTEFLOATSHORT, or UNSIGNED_SHORT.
  • Normalize: This parameter can be set to true or false. It handles numeric conversions that are beyond the scope of this introductory guide. For our purposes, we will set this parameter to false.
  • Stride: If stride is 0, then we are indicating that elements are stored sequentially in the buffer.
  • Offset: The position in the buffer from which we will start reading values for the corresponding attribute. It is usually set to 0 to indicate that we will start reading values from the first element of the buffer.
Buffer Pointer

vertexAttribPointer defines a pointer for reading information  fro m the currently-bound buffer. Remember that an error will be generated if there is no VBO currently bound.